Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<conio.h>
- #define sf scanf_s
- #define pf printf
- typedef struct node
- {
- int data;
- struct node *left,*right;
- }node;
- typedef struct
- {
- struct node *root;
- }head;
- void insert(head *t,int ele)
- {
- node *p,*q;
- p = (node*)malloc(sizeof(node));
- p->data = ele;
- p->left=p->right=NULL;
- if(t->root==NULL)
- {
- t->root = p;
- return;
- }
- q = t->root;
- while(q!=NULL)
- {
- if(ele<=q->data)
- {
- if(q->left==NULL)
- {
- q->left = p;
- return;
- }
- else q=q->left;
- }
- else
- {
- if(q->right==NULL)
- {
- q->right=p;
- return;
- }
- else q = q->right;
- }
- }
- }
- void inorder(node *x)
- {
- if(x!=NULL)
- {
- inorder(x->left);
- printf("%d ",x->data);
- inorder(x->right);
- }
- }
- void preorder(node *x)
- {
- if(x!=NULL)
- {
- printf("%d ",x->data);
- preorder(x->left);
- preorder(x->right);
- }
- }
- void postorder(node *x)
- {
- if(x!=NULL)
- {
- postorder(x->left);
- postorder(x->right);
- printf("%d ",x->data);
- }
- }
- void asc(node *x)
- {
- if(x!=NULL)
- {
- asc(x->left);
- printf("%d ",x->data);
- asc(x->right);
- }
- }
- void des(node *x)
- {
- if(x!=NULL)
- {
- des(x->right);
- printf("%d ",x->data);
- des(x->left);
- }
- }
- int count(node *x)
- {
- int c =1;
- if(x==NULL)
- return 0;
- else
- {
- c = c + count(x->left);
- c = c + count(x->right);
- return c;
- }
- }
- node *father(head *x,node *p)
- {
- node *q;
- if(x->root==p)
- {
- return NULL;
- }
- q=x->root;
- while(q!=NULL)
- {
- if(q->left==p||q->right==p)
- return q;
- if(p->data<=q->data)
- q=q->left;
- else
- q=q->right;
- }
- return NULL;
- }
- node *findmax(node *x)
- {
- if(x->right==NULL)
- {
- return x;
- }
- else return (findmax(x->right));
- }
- void delete1(head *t,int ele)
- {
- node *q;
- if(t->root==NULL)
- {
- printf("Bst empty");
- return;
- }
- q=t->root;
- while(q!=NULL)
- {
- if(q->data==ele)
- break;
- if(ele<q->data)
- q=q->left;
- else q=q->right;
- }
- if(q==NULL)
- {
- printf("\nElement not found");
- return;
- }
- if(q->left==NULL&&q->right==NULL)
- {
- if(q==t->root)
- {
- t->root = NULL;
- return;
- }
- if(father(t,q)->left==q)
- father(t,q)->left=NULL;
- else father(t,q)->right=NULL;
- return;
- }
- if(q->left!=NULL)
- {
- node *poin;
- int val;
- poin = findmax(q->left);
- val = poin->data;
- delete1(t,poin->data);
- q->data = val;
- return;
- }
- if(q==t->root)
- {
- t->root=t->root->right;
- return;
- }
- if(father(t,q)->left=q)
- father(t,q)->left=q->right;
- else father(t,q)->right=q->right;
- }
- int main()
- {
- head x;
- int ch,ele,z;
- x.root=NULL;
- pf("IMPLEMENTATION OF BST ");
- while(1)
- {
- pf("\nenter choice\n");
- pf("1.Insert 2.Delete 3.Preorder 4.Inorder 5.Postorder 6.Ascending order 7.Descending order 8.Count 9.Exit");
- sf("%d",&ch);
- if(ch==9)
- break;
- else
- {
- switch(ch)
- {
- case 1:pf("\nEnter element to Insert\n");
- sf("%d",&ele);
- insert(&x,ele);
- break;
- case 2:pf("\nEnter element to be deleted\n");
- sf("%d",&ele);
- delete1(&x,ele);
- break;
- case 3:pf("\n following is Preorder traversal of tree\n");
- preorder(x.root);
- break;
- case 4:pf("\n following is Inorder traversal of tree\n");
- inorder(x.root);
- break;
- case 5:pf("\n following is Postorder traversal of tree\n");
- postorder(x.root);
- break;
- case 6:pf("\n following is Ascending order of elements of tree\n");
- asc(x.root);
- break;
- case 7:pf("\n following is Descending order of elements of tree\n");
- des(x.root);
- break;
- case 8:z=count(x.root);
- pf("\nTotal no. element in Bst is %d",z);
- break;
- default:pf("\nInvalid input");
- }
- }
- }
- _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement