Advertisement
AsimKPrasad

Operations on BST

Nov 23rd, 2014
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.09 KB | None | 0 0
  1. /*
  2.  
  3.     Author :    Asim Krishna Prasad
  4.  
  5.     Program to perform the following operatios on a binary-search-tree :
  6.     * insertion
  7.     * pre-, in-, post- & level- order traversals
  8.     * deletion
  9.  
  10. */
  11.  
  12. #include<stdio.h>
  13. #include<stdlib.h>
  14.  
  15. #define scan(a)     scanf("%d", &a)
  16. #define print(a)    printf("%d", a)
  17. #define nline       printf("\n")
  18. #define fl(i,a,b)   for(i=a; i<b; i++)
  19. #define rev(i,a,b)  for(i=a; i>=b; i--)
  20. #define sspace      printf(" ")
  21.  
  22. typedef struct node
  23. {
  24.     int data;
  25.     struct node* left, *right;
  26. }node;
  27.  
  28. node* root;
  29.  
  30. node* insert(node* curr, int ndata)
  31. {
  32.     if(curr==NULL)
  33.     {
  34.         node *temp;
  35.         temp=(node *)(malloc(sizeof(node)));
  36.         temp->data=ndata;
  37.         temp->left=temp->right=NULL;
  38.         curr=temp;
  39.         return curr;
  40.     }
  41.     else if(ndata>curr->data)
  42.     {
  43.         curr->right=insert(curr->right,ndata);
  44.     }
  45.     else
  46.     {
  47.         curr->left=insert(curr->left,ndata);
  48.     }
  49.     return curr;
  50. }
  51.  
  52. node* getminnode(node* curr)
  53. {
  54.     if(curr->left==NULL)
  55.         return curr;
  56.     getminnode(curr->left);
  57. }
  58.  
  59. node* delete(node* curr, int key)
  60. {
  61.     if(curr==NULL)
  62.         return curr;
  63.  
  64.     if(key<curr->data)
  65.         curr->left=delete(curr->left,key);
  66.     else if(key>curr->data)
  67.         curr->right=delete(curr->right,key);
  68.     else
  69.     {
  70.         if(curr->left==NULL)
  71.         {
  72.             node* temp=curr->right;
  73.             free(curr);
  74.             return temp;
  75.         }
  76.         if(curr->right==NULL)
  77.         {
  78.             node* temp=curr->left;
  79.             free(curr);
  80.             return temp;
  81.         }
  82.  
  83.         node* temp=getminnode(curr->right);
  84.         curr->data = temp->data;
  85.         curr->right = delete(curr->right, temp->data);
  86.     }
  87.     return curr;
  88. }
  89.  
  90. void inorder(node* curr)
  91. {
  92.     if(curr==NULL)
  93.         return;
  94.     inorder(curr->left);
  95.     print(curr->data); sspace;
  96.     inorder(curr->right);
  97.     return;
  98. }
  99.  
  100. void postorder(node* curr)
  101. {
  102.     if(curr==NULL)
  103.         return;
  104.     postorder(curr->left);
  105.     postorder(curr->right);
  106.     print(curr->data); sspace;
  107.     return;
  108. }
  109.  
  110. void preorder(node* curr)
  111. {
  112.     if(curr==NULL)
  113.         return;
  114.     print(curr->data); sspace;
  115.     preorder(curr->left);
  116.     preorder(curr->right);
  117.     return;
  118. }
  119.  
  120. int max(int a, int b)
  121. {
  122.     if(a>b)
  123.         return a;
  124.     return b;
  125. }
  126.  
  127. int getheight(node* curr)
  128. {
  129.     if(curr==NULL)
  130.         return 0;
  131.     return max(getheight(curr->left)+1, getheight(curr->right)+1);
  132. }
  133.  
  134. void levelorder(node* curr, int level, int target)
  135. {
  136.     if(level>target || curr==NULL)
  137.         return;
  138.     if(level==target)
  139.         print(curr->data);
  140.     levelorder(curr->left,level+1,target);
  141.     levelorder(curr->right,level+1,target);
  142.     return;
  143. }
  144.  
  145. int main()
  146. {
  147.     int i, j, k, n, temp;
  148.     int ndata;
  149.     printf("Enter the number of elements to be inserted : ");
  150.     scan(n);
  151.     printf("Enter the elements to be inserted, one-by-one : ")
  152.     fl(i,0,n)
  153.     {
  154.         scan(ndata);
  155.         root=insert(root,ndata);
  156.     }
  157.     print(root->data); nline;
  158.     printf("Traversals :\n");
  159.     inorder(root); nline;
  160.     preorder(root); nline;
  161.     postorder(root); nline;
  162.     int hlimit=getheight(root);
  163.     print(hlimit);
  164.     fl(i,0,hlimit)
  165.     {
  166.         levelorder(root,0,i);
  167.     }
  168.     nline;
  169.     printf("Enter the number of nodes you want to delete : ");
  170.     scan(n);
  171.     fl(i,0,n)
  172.     {
  173.         printf("Enter the key to be deleted : ");
  174.         scan(k);
  175.         delete(root,k);
  176.         printf("Inorder traversal after deletion : ");
  177.         inorder(root); nline;
  178.     }
  179.     nline;
  180.     return 0;
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement