Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct BST
- {
- int a;struct BST *r,*l;
- }*root,*p,*q,*t,*s,*temp,*temp2,*nt,*w;
- int find(int data)
- {temp=root;temp2=NULL;
- while(temp->a!=data)
- {if(temp==NULL)
- {printf("not found");
- return 0;}
- temp2=temp;
- if(data < temp->a)
- temp=temp->l;
- if(data > temp->a)
- temp=temp->r;}
- return 1;
- }
- void delete()
- {int v;
- printf("nenter which you want to delete:");
- scanf("%d",&v);
- if(root==NULL)
- printf("tree is empty");
- if(find(v))
- {if(temp==root && temp->l==NULL && temp->r==NULL)
- {free(temp);
- printf("there are no node avalable now");
- root=NULL;}
- else
- {if(temp->r==NULL)
- nt=temp->l;
- else
- {
- nt=temp->r;
- w=nt;
- while(w->l!=NULL)
- w=w->l;
- w->l=temp->l;
- }
- if(temp==root)
- {
- root=nt;
- }
- else
- {
- if(temp2->l==temp)
- temp2->l=nt;
- else
- temp2->r=nt;
- }
- }
- }
- }
- void inorder(struct BST *x)
- {
- if(root)
- {
- if(x->l!=NULL)inorder(x->l);
- printf(" %d",x->a);
- if(x->r!=NULL) inorder(x->r);
- }
- else
- printf("tree is empty");
- }
- void preorder(struct BST *y)
- {
- if(root)
- {
- printf(" %d",y->a);
- if(y->l!=NULL) preorder(y->l);
- if(y->r!=NULL) preorder(y->r);
- }
- else
- printf("tree is empty");
- }
- void postorder(struct BST *z)
- {
- if(root)
- {
- if(z->l!=NULL) postorder(z->l);
- if(z->r!=NULL) postorder(z->r);
- printf(" %d",z->a);
- }
- else
- printf("tree is empty");
- }
- void main()
- {
- root=NULL;
- printf("enter your choice:n1.enter n2.inordern3.preordern4.postordern5.deleatn6.exit");
- while(1)
- {
- int ch,fl=0;
- printf("n enter your choice:");
- scanf("%d",&ch);
- switch(ch)
- {
- case 1:
- p=(struct BST*)malloc(sizeof (struct BST));
- printf("nenter the node to enter:");
- scanf("%d",&p->a);
- p->r=p->l=NULL;
- if(root==NULL)
- root=p;
- else
- {
- q=root;
- while(q!=NULL)
- {
- if(p->a==q->a)
- {
- printf("Not possible to enter");
- fl=1;
- break;
- }
- t=q;
- if(p->a < q->a)
- {
- q=q->l;
- }
- else
- q=q->r;
- }
- if(fl==0)
- {
- if(p->a < t->a) t->l=p;
- else t->r=p;
- }
- break;
- case 2:s=root;
- printf("nthe Inorder representation is :");
- inorder(s);
- break;
- case 3:s=root;
- printf("nthe Preorder representation is :");
- preorder(s);
- break;
- case 4:s=root;
- printf("nthe Postorder representation is :");
- postorder(s);
- break;
- case 5:
- delete();
- printf("deleted sucessfully");
- break;
- case 6:exit(0);
- default :printf("wrong choice");
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment