A C program to implement , Various tree traversal techniques & Various operations on binary search tree

Leave a Comment
//Below a C program to implement
//Various tree traversals
//Operations on binary search tree


#include<stdio.h>                         //for standard input/output functions #include<stdlib.h>                        //for standard library functions
#include<conio.h>                        //for console input/output functions
#include<string.h>                       //for string related functions
 

struct node                                   //node structure is being defined, with character array of 15 length 
{
char data[15];
struct node *left,*right;            
//self referential structure 
 };

void insert(struct node *r, struct node *p) 
//Function to insert nodal element in the tree
 {                                                                      
 if((r->right==NULL)&&(strcmpi(p->data,r->data)>0))
r->right=p;
else if((r->right!=NULL)&&(strcmpi(p->data,r->data)>0))
insert(r->right,p);


if((r->left==NULL)&&(strcmpi(p->data,r->data)< 0))
r->left=p;
else if((r->left!=NULL)&&(strcmpi(p->data,r->data)< 0))
insert(r->left,p);
}

void tree(struct node *r, int c)                     
//Function to create a tree traversal
 {
int top,flag;
struct node *w,*stack[20];
if(r!=NULL)
{
if(c!=4)
{
if(c == 1)
printf(" %s ",r->data);
tree(r->left,c);
if(c == 2)
printf(" %s ",r->data);
tree(r->right,c);
if(c == 3)
printf(" %s ",r->data);
}
}
if(c == 4)
{
top = 0;
w = r;
flag = 0;
while((top != -1)&&(w!=NULL))
{
while((flag == 0) && (w->left!=NULL))
{
stack[top] = w;
top++;
w = w->left;
}
printf(" %s ",w->data);
if(w->right != NULL)
{
w = w->right;
flag = 0;
}
else
{
top--;
w = stack[top];
flag = 1;
}
}
}
}


int main()
{

clrscr();                                                       //For clearing the screen 
int choice,c,i,flag;
char temp='N',temp1[15];
struct node *s,*root,*r,*q;
root = NULL;                                            
//Setting the initial node value to NULL
 do
{                                                                 
//Asking user the choice of operation 
 printf("\n 1. Enter");
printf("\n 2. Delete ");
printf("\n 3. Search ");
printf("\n 4. Display");
printf("\n 5. Exit");                                
//user will enter '5' for exiting the program
 printf("\nEnter Your Choice : ");
scanf("%d",&choice);
switch(choice)                                          
//The switch case will look up the user's choice
 {
case 1:printf("***** Data Entry ***** ");
do
{
s=(struct node*)malloc(sizeof(struct node));
   //allocating the tree node a memory space 
s->left=NULL;
s->right=NULL;
printf("\nEnter Data : ");
scanf("%s",&s->data);
if(root==NULL)
root=s;
else
insert(root,s);                                                    
//insert() function called
 printf("\nEnter Your Elements[y/n] : ");
scanf("%c",&temp);
}
while(temp=='y');
break;

case 2:printf("****** Delete Operation *******\n");
do
{
printf("\nEnter Element To Be Deleted : ");
scanf("%s",&temp1);
s=root;i=0;flag=0;
do
{
if(strcmpi(s->data,temp1)>0)
{
r=s;
s=s->left;
i=2;
}
if(strcmpi(s->data,temp1)==0)
{
flag=1;
if(i==0)
{
if(root->right!=NULL)
{
q=root->left;
root=root->right;
insert(root,q);
}
if(root->right==NULL)
root=root->left;
}
else
{
if(i==1)
{
q=s->left;
r->right=s->right;
if(s->left!=NULL)
insert(r,q);
}
if(i==2)
{
q=s->right;
r->left=s->left;
if(s->right!=NULL)
insert(r,q);
}
}
}
}
while(flag==0&&s!=NULL);
printf("\n Delete Any More[Y/N] : ");
scanf("%c",&temp);
}
while(temp=='y');
break;


case 3:printf("****** Search Operation *******\n");
do
{
printf("\n Enter Name To Be Searched");
scanf("%s",temp1);
i=0;
s=root;
while(s!=NULL&&i==0)
{
if(strcmpi(s->data,temp1)< 0)
s=s->right;
if(strcmpi(s->data,temp1)>0)
s=s->left;
if(strcmpi(s->data,temp1)==0)
i=1;
}
if(i==0)
printf("\nElement Not Found\n");
else
printf("\nElement Found\n");
printf("\nEnter More Elements[Y/N] : ");
scanf("%c",&temp);
}
while(temp=='y');
break;
 

case 4:
do
{
printf("\n 1. Preorder\n 2. Inorder \n 3. Postorder \n 4. Non Recursion \n 5. Exit");
printf("\nEnter Your Choice : ");
scanf("%d",&c);
if(root==NULL)
printf("Tree Not Started Yet");
else
tree(root,c);
                                                                               //Tree function called
 printf("\n Press Any Key To Continue......");
getch();
}
while(c!=5);
break;
}
}
while(choice!=5);
}

//End of program

0 comments: