11/08/2017
BST::Insertion:Searching:print::
struct node
{
int n;
struct node *left;
struct node *right;
};
struct node *head=NULL;
void insert(int y);
int searche(struct node *r,int s);
//int delet(struct node*r, int key);
void printl(struct node *t);
void printr(struct node *head);
int main()
{
int x,y,s,key;
printf("*********Menu********\n");
printf("1.Print all elements of TREE\n");
printf("2.insert elemnts\n");
printf("3.Search elements\n");
//printf("4.Delete Elements\n");
printf("press 5 for Exit operation\n");
//printf("Enter your Choice\n");
printf("\n");
struct node *a,*b,*c,*d,*e,*f,*g,*h,*i;
a=(struct node *)malloc(sizeof(struct node));
b=(struct node *)malloc(sizeof(struct node));
c=(struct node *)malloc(sizeof(struct node));
d=(struct node *)malloc(sizeof(struct node));
e=(struct node *)malloc(sizeof(struct node));
f=(struct node *)malloc(sizeof(struct node));
g=(struct node *)malloc(sizeof(struct node));
h=(struct node *)malloc(sizeof(struct node));
i=(struct node *)malloc(sizeof(struct node));
head=a;
a->n=40;
a->left=b;
a->right=c;
b->n=35;
b->left=d;
b->right=e;
c->n=50;
c->left=f;
c->right=NULL;
d->n=30;
d->left=NULL;
d->right=NULL;
e->n=37;
e->left=g;
e->right=NULL;
f->n=45;
f->left=NULL;
f->right=h;
g->n=33;
g->left=NULL;
g->right=NULL;
h->n=48;
h->left=NULL;
h->right=i;
i->n=60;
i->left=NULL;
i->right=NULL;
while(1)
{
printf("Enter your choice from main menu\n");
scanf("%d",&x);
printf("\n");
switch(x)
{
case 1:printl(head);
break;
case 2:printf("Enter the element you want to insert\n");
scanf("%d",&y);
insert(y);
break;
case 3:printf("Enter the value you want to search\n");
scanf("%d",&s);
searche(head,s);
break;
case 5:exit(0);
}
}
}
void printl(struct node *t)
{
if(t!=NULL)
{
printl(t->left);
printf("%d\n",t->n);
printl(t->right);
}
}
void insert(int y)
{
struct node *temp,*p;
temp=(struct node *)malloc(sizeof(struct node));
p=(struct node *)malloc(sizeof(struct node));
temp->n=y;
temp->left=NULL;
temp->right=NULL;
if(head==NULL)
{
head=temp;
}
else
{
struct node *curr;
curr=(struct node *)malloc(sizeof(struct node));
curr=head;
while(curr)
{
p=curr;
if(temp->n>curr->n)
{
curr=curr->right;
}
else
{
curr=curr->left;
}
}
if(temp->n>p->n)
{
p->right=temp;
}
else
{
p->left=temp;
}
}
}
int searche(struct node *r,int s)
{
if(s==r->n)
{
printf("%d is found in the position of %d\n",s,&r->n);
}
else if(sn)
{
r->left=searche(r->left,s);
}
else if(s>=r->n)
{
r->right=searche(r->right,s);
}
}