#include<stdio.h>
#include<conio.h>
typedef struct
tree
{
int data;
struct tree *left,*right;
}node;
int find(node
*t,int x)
{
node *t;
int x;
if(t==NULL)
return(0);
if(x==t->data)
return(t);
if(x<t->data)
return(find(t->left,x));
if(x>t->data)
return(find(t->right,x));
}
int insert(node
*t,int x)
{
if(t==NULL)
{
t=(node *)malloc(sizeof(node));
scanf("%d",&t->data);
t->left=t->right=NULL;
return(t);
}
if(x>t->data)
{
insert(t->right,x);
return(t);
}
if(x<t->data)
{
insert(t->left,x);
return(t);
}
}
int deletion(node
*t,int x)
{
node *temp;
if(t==NULL)
return(t);
if(x<t->data)
{
t->left=deletion(t->left,x);
return(t);
}
if(x>t->data)
{
t->right=deletion(t->right,x);
return(t);
}
if(t->left==NULL&&t->right==NULL)
{
temp =t;
free(temp);
return(NULL);
}
if(t->left==NULL)
{
temp=t;
t=t->right;
free(temp);
}
if(t->right==NULL)
{
temp=t;
t=t->left;
free(temp);
}
temp=find_min(t->right);
t->data=temp->data;
t->right=deletion(t->right,temp->data);
return(t);
}
int find_min(node
*t)
{
while(t->left!=NULL)
t=t->left;
return(t);
}
main()
{
int op,y;
node *t;
do
{
printf("\n1.Insert\n2.Delete\n3.Search\nExit");
printf("\nEnter option=");
scanf("%d",&op);
switch(op)
{
case 1:
printf("\nEnter the value to be
entered in the tree=");
scanf("%d",&y);
insert(t,y);
break;
case 2:
printf("\nEnter the data to be
deleted=");
scanf("%d",&y);
deletion(t,y);
break;
case 3:
printf("\nEntert he data to be
searched=");
scanf("%d",&y);
find(t,y);
break;
}
}while(op!=4);
getch();
}
No comments:
Post a Comment