BINARY SEARCH TREE OPERATIONS */
#include
#include
#define NULL 0
struct tree
{
int info;
struct tree *left;
struct tree *right;
};
struct tree *p;
struct tree *search(struct tree *,int);
struct tree *insert(struct tree *i,int);
void inorder(struct tree *);
void delete(struct tree *);
main()
{
int l,ch,key,x,y;
struct tree *z;
clrscr();
do
{
printf(" BINARY SEARCH TREE OPERATIONS ");
printf(" 1.INSERTING ELEMENT");
printf(" 2.DELETING ELEMENT");
printf(" 3.SEARCH FOR ELEMENT");
printf(" 4.DISPLAY");
printf(" 5.EXIT");
printf("  ENTER CHOICE:");
scanf("%d",&ch);
switch(ch)
{
case 1:printf(" enter element to insertat end 0 ");
scanf("%d",&x);
while(x!=0)
{
p=insert(p,x);
printf(" enter element to insert:");
scanf("%d",&x);
}
break;
case 2:printf(" enter node to delete");
scanf("%d",&key);
z=search(p,key);
if(z!=NULL)
{
delete(z);
printf(" given key node is deleted ");
}
else
printf(" the given key node not found");
break;
case 3:printf(" enter node to be search");
scanf("%d",&y);
p=search(p,y);
if(p!=NULL)
printf(" the key element is :%d",p->info);
else
printf(" the key is not found");
break;
case 4:printf(" tree elements in inorder :");
inorder(p);
case 5:exit(0);
break;
}
}
while(ch>=1||ch<=5);
}
struct tree *insert(struct tree *s,int x)
{
struct tree *trail,*p,*q;
q=(struct tree *)malloc(sizeof(struct tree));
q->info=x;
q->left=NULL;
q->right=NULL;
p=s;
trail=NULL;
while(p!=NULL)
{
trail=p;
if(p->info>x)
p=p->left;
else
p=p->right;
}
if(trail==NULL)
{
s=q;
return(s);
}
if(trail->info>x)
trail->left=q;
else
trail->right=q;
return(s);
}
struct tree *search(struct tree *p,int x)
{
while(p!=NULL && p->info!=x)
{
if(p->info>x)
p=p->left;
else
p=p->right;
}
return(p);
}
void delete(struct tree *p)
{
struct tree *temp;
if(p==NULL)
printf(" trying to delete a noexistent node  ");
else if(p->left==NULL)
{
temp=p;
p=p->right;
free(temp);
}
else if(p->right==NULL)
{
temp=p;
p=p->left;
free(temp);
}
else if((p->left!=NULL) && (p->right!=NULL))
{
temp=p->right;
while(temp->left!=NULL)
temp=temp->left;
temp->left=p->left;
temp=p;
p=p->right;
free(temp);
}
}
void inorder(struct tree *p)
{
if(p!=NULL)
{
inorder(p->left);
printf("%dt",p->info);
inorder(p->right);
}
}
Solution
BINARY SEARCH TREE OPERATIONS */
#include
#include
#define NULL 0
struct tree
{
int info;
struct tree *left;
struct tree *right;
};
struct tree *p;
struct tree *search(struct tree *,int);
struct tree *insert(struct tree *i,int);
void inorder(struct tree *);
void delete(struct tree *);
main()
{
int l,ch,key,x,y;
struct tree *z;
clrscr();
do
{
printf(" BINARY SEARCH TREE OPERATIONS ");
printf(" 1.INSERTING ELEMENT");
printf(" 2.DELETING ELEMENT");
printf(" 3.SEARCH FOR ELEMENT");
printf(" 4.DISPLAY");
printf(" 5.EXIT");
printf("  ENTER CHOICE:");
scanf("%d",&ch);
switch(ch)
{
case 1:printf(" enter element to insertat end 0 ");
scanf("%d",&x);
while(x!=0)
{
p=insert(p,x);
printf(" enter element to insert:");
scanf("%d",&x);
}
break;
case 2:printf(" enter node to delete");
scanf("%d",&key);
z=search(p,key);
if(z!=NULL)
{
delete(z);
printf(" given key node is deleted ");
}
else
printf(" the given key node not found");
break;
case 3:printf(" enter node to be search");
scanf("%d",&y);
p=search(p,y);
if(p!=NULL)
printf(" the key element is :%d",p->info);
else
printf(" the key is not found");
break;
case 4:printf(" tree elements in inorder :");
inorder(p);
case 5:exit(0);
break;
}
}
while(ch>=1||ch<=5);
}
struct tree *insert(struct tree *s,int x)
{
struct tree *trail,*p,*q;
q=(struct tree *)malloc(sizeof(struct tree));
q->info=x;
q->left=NULL;
q->right=NULL;
p=s;
trail=NULL;
while(p!=NULL)
{
trail=p;
if(p->info>x)
p=p->left;
else
p=p->right;
}
if(trail==NULL)
{
s=q;
return(s);
}
if(trail->info>x)
trail->left=q;
else
trail->right=q;
return(s);
}
struct tree *search(struct tree *p,int x)
{
while(p!=NULL && p->info!=x)
{
if(p->info>x)
p=p->left;
else
p=p->right;
}
return(p);
}
void delete(struct tree *p)
{
struct tree *temp;
if(p==NULL)
printf(" trying to delete a noexistent node  ");
else if(p->left==NULL)
{
temp=p;
p=p->right;
free(temp);
}
else if(p->right==NULL)
{
temp=p;
p=p->left;
free(temp);
}
else if((p->left!=NULL) && (p->right!=NULL))
{
temp=p->right;
while(temp->left!=NULL)
temp=temp->left;
temp->left=p->left;
temp=p;
p=p->right;
free(temp);
}
}
void inorder(struct tree *p)
{
if(p!=NULL)
{
inorder(p->left);
printf("%dt",p->info);
inorder(p->right);
}
}

BINARY SEARCH TREE OPERATIONS #includestdio.h#includestdlib.pdf

  • 1.
    BINARY SEARCH TREEOPERATIONS */ #include #include #define NULL 0 struct tree { int info; struct tree *left; struct tree *right; }; struct tree *p; struct tree *search(struct tree *,int); struct tree *insert(struct tree *i,int); void inorder(struct tree *); void delete(struct tree *); main() { int l,ch,key,x,y; struct tree *z; clrscr(); do { printf(" BINARY SEARCH TREE OPERATIONS "); printf(" 1.INSERTING ELEMENT"); printf(" 2.DELETING ELEMENT"); printf(" 3.SEARCH FOR ELEMENT"); printf(" 4.DISPLAY"); printf(" 5.EXIT"); printf(" ENTER CHOICE:"); scanf("%d",&ch); switch(ch) { case 1:printf(" enter element to insertat end 0 "); scanf("%d",&x); while(x!=0)
  • 2.
    { p=insert(p,x); printf(" enter elementto insert:"); scanf("%d",&x); } break; case 2:printf(" enter node to delete"); scanf("%d",&key); z=search(p,key); if(z!=NULL) { delete(z); printf(" given key node is deleted "); } else printf(" the given key node not found"); break; case 3:printf(" enter node to be search"); scanf("%d",&y); p=search(p,y); if(p!=NULL) printf(" the key element is :%d",p->info); else printf(" the key is not found"); break; case 4:printf(" tree elements in inorder :"); inorder(p); case 5:exit(0); break; } } while(ch>=1||ch<=5); } struct tree *insert(struct tree *s,int x) { struct tree *trail,*p,*q;
  • 3.
    q=(struct tree *)malloc(sizeof(structtree)); q->info=x; q->left=NULL; q->right=NULL; p=s; trail=NULL; while(p!=NULL) { trail=p; if(p->info>x) p=p->left; else p=p->right; } if(trail==NULL) { s=q; return(s); } if(trail->info>x) trail->left=q; else trail->right=q; return(s); } struct tree *search(struct tree *p,int x) { while(p!=NULL && p->info!=x) { if(p->info>x) p=p->left; else p=p->right; } return(p); }
  • 4.
    void delete(struct tree*p) { struct tree *temp; if(p==NULL) printf(" trying to delete a noexistent node "); else if(p->left==NULL) { temp=p; p=p->right; free(temp); } else if(p->right==NULL) { temp=p; p=p->left; free(temp); } else if((p->left!=NULL) && (p->right!=NULL)) { temp=p->right; while(temp->left!=NULL) temp=temp->left; temp->left=p->left; temp=p; p=p->right; free(temp); } } void inorder(struct tree *p) { if(p!=NULL) { inorder(p->left); printf("%dt",p->info); inorder(p->right); }
  • 5.
    } Solution BINARY SEARCH TREEOPERATIONS */ #include #include #define NULL 0 struct tree { int info; struct tree *left; struct tree *right; }; struct tree *p; struct tree *search(struct tree *,int); struct tree *insert(struct tree *i,int); void inorder(struct tree *); void delete(struct tree *); main() { int l,ch,key,x,y; struct tree *z; clrscr(); do { printf(" BINARY SEARCH TREE OPERATIONS "); printf(" 1.INSERTING ELEMENT"); printf(" 2.DELETING ELEMENT"); printf(" 3.SEARCH FOR ELEMENT"); printf(" 4.DISPLAY"); printf(" 5.EXIT"); printf(" ENTER CHOICE:"); scanf("%d",&ch); switch(ch) {
  • 6.
    case 1:printf(" enterelement to insertat end 0 "); scanf("%d",&x); while(x!=0) { p=insert(p,x); printf(" enter element to insert:"); scanf("%d",&x); } break; case 2:printf(" enter node to delete"); scanf("%d",&key); z=search(p,key); if(z!=NULL) { delete(z); printf(" given key node is deleted "); } else printf(" the given key node not found"); break; case 3:printf(" enter node to be search"); scanf("%d",&y); p=search(p,y); if(p!=NULL) printf(" the key element is :%d",p->info); else printf(" the key is not found"); break; case 4:printf(" tree elements in inorder :"); inorder(p); case 5:exit(0); break; } } while(ch>=1||ch<=5); }
  • 7.
    struct tree *insert(structtree *s,int x) { struct tree *trail,*p,*q; q=(struct tree *)malloc(sizeof(struct tree)); q->info=x; q->left=NULL; q->right=NULL; p=s; trail=NULL; while(p!=NULL) { trail=p; if(p->info>x) p=p->left; else p=p->right; } if(trail==NULL) { s=q; return(s); } if(trail->info>x) trail->left=q; else trail->right=q; return(s); } struct tree *search(struct tree *p,int x) { while(p!=NULL && p->info!=x) { if(p->info>x) p=p->left; else p=p->right;
  • 8.
    } return(p); } void delete(struct tree*p) { struct tree *temp; if(p==NULL) printf(" trying to delete a noexistent node "); else if(p->left==NULL) { temp=p; p=p->right; free(temp); } else if(p->right==NULL) { temp=p; p=p->left; free(temp); } else if((p->left!=NULL) && (p->right!=NULL)) { temp=p->right; while(temp->left!=NULL) temp=temp->left; temp->left=p->left; temp=p; p=p->right; free(temp); } } void inorder(struct tree *p) { if(p!=NULL) { inorder(p->left);
  • 9.