Tuesday, August 25, 2009

binary search tree implementation

//
binary search tree implementation.
#include
#include
#include
typedef struct node
{
int data; struct bstree *left; struct bstree *right;
}tree;
tree *getnode();
void displaymenu();
tree *createbtree();
tree *insertnode(tree *btree,tree *temp);
tree *deletenode(int digit,tree *btree);
tree *searchnode(tree *btree,int key);
void view(tree *btree,int level);
tree *find(tree *btree,int item,tree **par);
tree *delnochild(tree *btree,tree *par,tree *loc);
tree *delonechild(tree *btree,tree *par,tree *loc);
tree *deltwochild(tree *btree,tree *par,tree *loc);

void main()
{
int choice,key;
tree *btree=NULL,*temp,*par,*loc;
clrscr();
while(1)
{
//clrscr();
displaymenu();
printf("\n\n ENTER U R CHOICE");
scanf("%d",&choice);
switch(choice)
{ case 1: btree=NULL;
printf("\n Create a Binary tree ");
btree=createbtree();
break;
case 2:
printf("\n\n Insert the NODE in the tree ");
temp=getnode();
//readnode(temp);
btree=insertnode(btree,temp);
break;
case 3:
if(btree==NULL)
printf("Binary tree is empty...");
else
{ printf("\n Delete the node from the tree ");
printf("\n\n Enter the Element for Deleting the node : ");
scanf("%d",&key);
btree=deletenode(key,btree);
}
getch();
break;
case 4:
if(btree==NULL)
printf("Binary tree is empty...");
else { printf("\n search the node in the tree ");
printf("\n\n Enter the searching Element: ");
scanf("%d",&key);
temp=searchnode(btree,key);
if(temp==NULL)
printf("Search Element %d is not found",key);
else printf("\n Search Element %d is found",temp->data);
}
getch();
break;
case 5:
if(btree ==NULL)
printf("\n\n Binary tree is empty....");
else { printf("\n\n Binary search tree is ..");
view(btree,1);
}
getch();
break;
case 6:
printf("\n\n <----------End of your program--------->");
free(btree);
exit(0);
}//end of switch case
}//end of while
}void displaymenu()
{
printf("\n\n BINARY SEARCH TREE ->OPERATION");
printf("\n\n 1. Create a tree ");
printf("\n\n 2. Insert a node");
printf("\n\n 3. Delete a node");
printf("\n\n 4. Search a node");
printf("\n\n 5. Display the tree ");
printf("\n\n 6. EXIT");
}
tree *getnode() { tree *newnode;
newnode=(tree *)malloc(sizeof(tree));
printf("\n\n Enter the Data : ");
scanf("%d",&newnode->data);
newnode->left=newnode->right=NULL; r
eturn(newnode);
}
tree *createbtree()
{
char ch; tree *btree=NULL,*temp;
do
{
temp=getnode();
btree=insertnode(btree,temp);
fflush(stdin);
printf("\n\n Do you wish to add data in the tree[Y/N]: ");
scanf("%c",&ch);
}
while((ch=='Y')(ch=='y'));
return btree;
}
tree *find(tree *btree,int data,tree **par)
{
if(btree ==NULL)
return NULL;
else if (datadata)
{ *par=btree; return find(btree->left,data,par); }
else if (data>btree->data)
{
*par=btree; return find(btree->right,data,par);
}
else if(data==btree->data) return btree;
}
tree *insertnode(tree *btree,tree *temp)
{
tree *par=NULL,*loc=NULL;
loc=find(btree,temp->data,&par);
if(loc!=NULL) { printf("\n Data is already existing");
return btree;
}
if(btree==NULL) return temp;
else if(temp->datadata) par->left=temp;
else par->right=temp; return btree;
}
tree *deletenode(int key,tree *btree)
{
tree *par=NULL,*loc=NULL;
loc=find(btree,key,&par);
if(loc==NULL)
{
printf("\n\n Item is Not present");
return btree;
}
if(loc->left==NULL&&loc->right==NULL)
btree=delnochild(btree,par,loc);
if((loc->left!=NULL&&loc->right==NULL)(loc->left==NULL&&loc->right!=NULL))
btree=delonechild(btree,par,loc);
if(loc->left!=NULL&&loc->right!=NULL)
btree=deltwochild(btree,par,loc);
free(loc);
return btree;
}
tree *delnochild(tree *btree,tree *par,tree *loc)
{
if(par==loc)
return NULL;
else if(loc==par->left)
par->left=NULL;
else if(loc==par->right)
par->right=NULL; return btree;
}
tree *delonechild(tree *btree,tree *par,tree *loc)
{
tree *temp;
if(loc->left!=NULL)
temp=loc->left;
else temp=loc->right;
if(par==NULL)
btree=temp;
else if(loc==par->left)
par->left=temp;
else if(loc==par->right)
par->right=temp;
return btree;
}
tree *deltwochild(tree *btree,tree *par,tree *loc)
{
tree *suc,*parsuc;
parsuc=loc;
for(suc=loc->right;suc->left!=NULL;suc=suc->left)
parsuc=suc;
if(suc->left==NULL&&suc->right==NULL)
delnochild(btree,parsuc,suc);
else
delonechild(btree,parsuc,suc);
if(par==NULL)
btree=suc;
else if(loc==par->left)
par->left=suc;
else if(loc==par->right)
par->right=suc; suc->left=loc->left;
suc->right=loc->right; return btree;
}
tree *searchnode(tree *btree,int key)
{
if(btree==NULL)
return NULL;
else if(keydata)
return searchnode(btree->left,key);
else if(key>btree->data)
return searchnode(btree->right,key);
else if(key==btree->data) return btree;
}
void view(tree *btree,int level)
{
int k;
if(btree==NULL)
return;
view(btree->right,level+1);
printf("\n");
for(k=0;kprintf(" \t");
printf("%d",btree->data);
view(btree->left,level+1);
}

No comments:

Post a Comment