Advertisement
d1i2p3a4k5

16.BST with every function (DS)

Nov 1st, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.65 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #define sf scanf_s
  4. #define pf printf
  5. typedef struct node
  6. {
  7.     int data;
  8.     struct node *left,*right;
  9. }node;
  10. typedef struct
  11. {
  12.     struct node *root;
  13. }head;
  14. void insert(head *t,int ele)
  15. {
  16.     node *p,*q;
  17.     p = (node*)malloc(sizeof(node));
  18.     p->data = ele;
  19.     p->left=p->right=NULL;
  20.     if(t->root==NULL)
  21.     {
  22.         t->root = p;
  23.         return;
  24.     }
  25.     q = t->root;
  26.     while(q!=NULL)
  27.     {
  28.         if(ele<=q->data)
  29.         {
  30.             if(q->left==NULL)
  31.             {
  32.                 q->left = p;
  33.                 return;
  34.             }
  35.             else q=q->left;
  36.         }
  37.         else
  38.         {
  39.             if(q->right==NULL)
  40.             {
  41.                 q->right=p;
  42.                 return;
  43.             }
  44.             else q = q->right;
  45.         }
  46.     }
  47. }
  48. void inorder(node *x)
  49. {
  50.     if(x!=NULL)
  51.     {
  52.         inorder(x->left);
  53.         printf("%d ",x->data);
  54.         inorder(x->right);
  55.     }
  56.  
  57. }
  58. void preorder(node *x)
  59. {
  60.     if(x!=NULL)
  61.     {
  62.         printf("%d ",x->data);
  63.         preorder(x->left);
  64.         preorder(x->right);
  65.     }
  66. }
  67. void postorder(node *x)
  68. {
  69.     if(x!=NULL)
  70.     {
  71.         postorder(x->left);
  72.         postorder(x->right);
  73.         printf("%d ",x->data);
  74.     }
  75. }
  76. void asc(node *x)
  77. {
  78.     if(x!=NULL)
  79.     {
  80.         asc(x->left);
  81.         printf("%d ",x->data);
  82.         asc(x->right);
  83.     }
  84. }
  85. void des(node *x)
  86. {
  87.     if(x!=NULL)
  88.     {
  89.         des(x->right);
  90.         printf("%d ",x->data);
  91.         des(x->left);
  92.     }
  93. }
  94. int count(node *x)
  95. {
  96.     int c =1;
  97.     if(x==NULL)
  98.         return 0;
  99.     else
  100.     {
  101.         c = c + count(x->left);
  102.         c = c + count(x->right);
  103.         return c;
  104.     }
  105. }
  106. node *father(head *x,node *p)
  107. {
  108.     node *q;
  109.     if(x->root==p)
  110.     {
  111.         return NULL;
  112.     }
  113.     q=x->root;
  114.     while(q!=NULL)
  115.     {
  116.         if(q->left==p||q->right==p)
  117.             return q;
  118.         if(p->data<=q->data)
  119.             q=q->left;
  120.         else
  121.             q=q->right;
  122.     }
  123.     return NULL;
  124. }
  125. node *findmax(node *x)
  126. {
  127.     if(x->right==NULL)
  128.     {
  129.         return x;
  130.     }
  131.     else return (findmax(x->right));
  132. }
  133. void delete1(head *t,int ele)
  134. {
  135.     node *q;
  136.     if(t->root==NULL)
  137.     {
  138.         printf("Bst empty");
  139.         return;
  140.     }
  141.     q=t->root;
  142.     while(q!=NULL)
  143.     {
  144.         if(q->data==ele)
  145.             break;
  146.         if(ele<q->data)
  147.             q=q->left;
  148.         else q=q->right;
  149.     }
  150.     if(q==NULL)
  151.     {
  152.         printf("\nElement not found");
  153.         return;
  154.     }
  155.     if(q->left==NULL&&q->right==NULL)
  156.     {
  157.         if(q==t->root)
  158.         {
  159.             t->root = NULL;
  160.             return;
  161.         }
  162.         if(father(t,q)->left==q)
  163.             father(t,q)->left=NULL;
  164.         else father(t,q)->right=NULL;
  165.         return;
  166.     }
  167.     if(q->left!=NULL)
  168.     {
  169.         node *poin;
  170.         int val;
  171.         poin = findmax(q->left);
  172.         val = poin->data;
  173.         delete1(t,poin->data);
  174.         q->data = val;
  175.         return;
  176.     }
  177.     if(q==t->root)
  178.     {
  179.         t->root=t->root->right;
  180.         return;
  181.     }
  182.     if(father(t,q)->left=q)
  183.         father(t,q)->left=q->right;
  184.     else father(t,q)->right=q->right;
  185. }
  186. int main()
  187. {
  188.     head x;
  189.     int ch,ele,z;
  190.     x.root=NULL;
  191.     pf("IMPLEMENTATION OF BST ");
  192.     while(1)
  193.     {
  194.         pf("\nenter choice\n");
  195.         pf("1.Insert 2.Delete 3.Preorder 4.Inorder 5.Postorder 6.Ascending order 7.Descending order 8.Count 9.Exit");
  196.         sf("%d",&ch);
  197.         if(ch==9)
  198.         break;
  199.         else
  200.         {
  201.             switch(ch)
  202.             {
  203.             case 1:pf("\nEnter element to Insert\n");
  204.                 sf("%d",&ele);
  205.                 insert(&x,ele);
  206.                 break;
  207.             case 2:pf("\nEnter element to be deleted\n");
  208.                 sf("%d",&ele);
  209.                 delete1(&x,ele);
  210.                 break;
  211.             case 3:pf("\n following is Preorder traversal of tree\n");
  212.                 preorder(x.root);
  213.                 break;
  214.             case 4:pf("\n following is Inorder traversal of tree\n");
  215.                 inorder(x.root);
  216.                 break;
  217.             case 5:pf("\n following is Postorder traversal of tree\n");
  218.                 postorder(x.root);
  219.                 break;
  220.             case 6:pf("\n following is Ascending order of elements of tree\n");
  221.                 asc(x.root);
  222.                 break;
  223.             case 7:pf("\n following is Descending order of elements of tree\n");
  224.                 des(x.root);
  225.                 break;
  226.             case 8:z=count(x.root);
  227.                 pf("\nTotal no. element in Bst is %d",z);
  228.                 break;
  229.             default:pf("\nInvalid input");
  230.             }
  231.         }
  232.     }
  233.     _getch();
  234.     return 0;
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement