Advertisement
satyaki30

binary search tree ops - complete

Oct 30th, 2014
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.89 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define None 32767
  5. #define True 1
  6. #define False 0
  7. typedef struct  BSTNode
  8. {
  9.     int data;
  10.     struct BSTNode *left;
  11.     struct BSTNode *right;
  12. } BSTNode;
  13.  
  14. BSTNode *root = NULL;
  15. BSTNode* init(int val)
  16. {
  17.     //initializer for the BST node
  18.     BSTNode *temp;
  19.     temp = (BSTNode*) malloc (sizeof(BSTNode));
  20.     temp -> data = val;
  21.     temp -> right = NULL;
  22.     temp -> left = NULL;
  23.     return temp;
  24. }
  25.  
  26. void insert_node(int val)
  27. {
  28.     //inserts the node with value val in the right place in the tree
  29.     BSTNode *new_tnode;
  30.     BSTNode *temp;
  31.     new_tnode = init(val);
  32.     if (root == NULL) //the tree is empty
  33.     {
  34.         root = new_tnode;
  35.         return;
  36.     }
  37.     temp = root;
  38.     while (1) //we need to descend to the second last level
  39.     {
  40.         if (val < temp -> data) //left subtree
  41.         {
  42.             if (temp -> left == NULL)
  43.             {
  44.                 temp -> left = new_tnode;
  45.                 break;
  46.             }
  47.             else
  48.                 temp = temp -> left;
  49.         }
  50.         else
  51.         {
  52.             if (temp -> right == NULL)
  53.             {
  54.                 temp -> right = new_tnode;
  55.                 break;
  56.             }
  57.             else
  58.                 temp = temp -> right;
  59.         }
  60.     }
  61. }
  62. int search(int val)
  63. {
  64.     //searches the BST for the node valued val
  65.     BSTNode *temp;
  66.     temp = root;
  67.     while (temp != NULL)
  68.     {
  69.         if (val > temp -> data)
  70.             temp = temp -> right;
  71.         else if (val < temp -> data)
  72.             temp = temp -> left;
  73.         else
  74.             return True;
  75.     }
  76.     return False;
  77. }
  78. BSTNode* get_node(int val)
  79. {
  80.     //returns the node in the tree having the value val
  81.     BSTNode *temp;
  82.     temp = root;
  83.     if (!search(val))
  84.     {
  85.         printf("\nThe node doesnt exist in the BST");
  86.         return NULL;
  87.     }
  88.     while (temp != NULL)
  89.     {
  90.         if (val > temp -> data)
  91.             temp = temp -> right;
  92.         else if (val < temp -> data)
  93.             temp = temp -> left;
  94.         else if (val == temp -> data)
  95.             return temp;
  96.     }
  97. }
  98. BSTNode* min_finder(BSTNode *tnode)
  99. {
  100.     //returns the node with the minimum value of the subtree rooted at tnode
  101.     if (tnode -> left != NULL) //if you can go further left, go left!
  102.         min_finder(tnode -> left);
  103.     else //return the leaf
  104.         return tnode;
  105. }
  106. BSTNode* max_finder(BSTNode *tnode)
  107. {
  108.     //returns the node with the minimum value of the subtree rooted at tnode
  109.     if (tnode -> right != NULL) //if you can go further right, go right
  110.         max_finder(tnode -> right);
  111.     else
  112.         return tnode;
  113. }
  114. BSTNode *get_inorder_predecessor(BSTNode *tnode)
  115. {
  116.     //returns the inorder predecessor of the tnode
  117.     //go left once, find the max of that subtree
  118.     if (tnode -> left == NULL)
  119.         return NULL;
  120.     else
  121.         return max_finder(tnode -> left);
  122. }
  123. BSTNode *get_inorder_successor(BSTNode *tnode)
  124. {
  125.     //returns the inorder successor of the tnode
  126.     //go right once, find the min of that subtree
  127.     if (tnode -> right == NULL)
  128.         return NULL;
  129.     else
  130.         return min_finder(tnode -> right);
  131. }
  132. void delete_leaf(BSTNode *tnode)
  133. {
  134.     //deletes the leaf node tnode from the BST - works
  135.     BSTNode *ptr, *pre_ptr;
  136.     if (tnode -> data == root -> data)
  137.     {
  138.         ptr = root;
  139.         root = NULL;
  140.         free(ptr);
  141.     }
  142.     else
  143.     {
  144.         ptr = root;
  145.         pre_ptr = root; //pre_ptr is the parent of the root
  146.         while (ptr -> data != tnode -> data)
  147.         {
  148.             pre_ptr = ptr;
  149.             //printf("\ntnode -> data = %d, ptr -> data =%d\n", tnode -> data, ptr -> data);
  150.             if (ptr -> data > tnode -> data)
  151.                 ptr = ptr -> left;
  152.  
  153.             else
  154.                 ptr = ptr -> right;
  155.         }
  156.         if (tnode -> data > pre_ptr -> data)
  157.             pre_ptr -> right = NULL;
  158.         else
  159.             pre_ptr -> left = NULL;
  160.     free(ptr);
  161.     }
  162. }
  163. void delete_right(BSTNode *tnode)
  164. {
  165.     //deletes the node tnode which has no left child
  166.     BSTNode *ptr, *pre_ptr;
  167.     if (tnode -> data == root -> data)
  168.     {
  169.         ptr = root;
  170.         root  = root -> right;
  171.         free(ptr);
  172.     }
  173.     else
  174.     {
  175.         ptr = root;
  176.         pre_ptr = root;
  177.         while (ptr -> data != tnode -> data)
  178.         {
  179.             pre_ptr = ptr;
  180.  
  181.             if (ptr -> data > tnode -> data)
  182.                 ptr = ptr -> left;
  183.            
  184.             else
  185.                 ptr = ptr -> right;
  186.         }
  187.         //pre_ptr is now the parent of tnode
  188.         if (ptr -> data > pre_ptr -> data)
  189.             pre_ptr -> right = ptr -> right;
  190.         else
  191.             pre_ptr -> left = ptr -> right;
  192.         ptr -> left = NULL;
  193.         //ptr -> right = NULL;
  194.         free(ptr);
  195.     }
  196. }
  197. void delete_left(BSTNode *tnode)
  198. {
  199.     //deletes the node tnode which has no right child
  200.     BSTNode *ptr, *pre_ptr;
  201.     if (tnode -> data == root -> data)
  202.     {
  203.         ptr = root;
  204.         root = root -> left;
  205.         free(ptr);
  206.     }
  207.     else
  208.     {
  209.         ptr = root;
  210.         pre_ptr = root;
  211.         while (ptr -> data != tnode -> data)
  212.         {
  213.             pre_ptr = ptr;
  214.             if (ptr -> data > tnode -> data)
  215.                 ptr = ptr -> left;
  216.             else
  217.                 ptr = ptr -> right;
  218.         }
  219.         //pre_ptr is now the parent of tnode
  220.         if (ptr -> data > pre_ptr -> data)
  221.             pre_ptr -> right = ptr -> left;
  222.         else
  223.             pre_ptr -> left = ptr -> left;
  224.  
  225.         //ptr -> left = NULL;
  226.         ptr -> right = NULL;
  227.         free(ptr);
  228.     }  
  229. }
  230. void super_delete(BSTNode *tnode)
  231. {
  232.     //deletes the node tnode with two children
  233.     BSTNode *replacement_node;
  234.     int data;
  235.     replacement_node = get_inorder_successor(tnode);
  236.     data = replacement_node -> data;
  237.     delete_left(replacement_node);  tnode -> data = data;
  238. }
  239. void master_delete(int val)
  240. {
  241.     //deletes the node with value val in the BST
  242.     //has 3 sub routines - 1. delete leaf, 2. delete nodes having one child, 3. delete nodes with two children
  243.     BSTNode *temp;
  244.     temp = get_node(val);
  245.     if (temp == NULL)
  246.         printf("\nUnable to delete %d from the BST\n", val);
  247.     else if ((temp -> left == NULL) && (temp -> right == NULL))
  248.     {
  249.         if (temp -> data == root -> data)
  250.         {
  251.             delete_leaf(temp);
  252.             root = NULL;
  253.         }
  254.         else
  255.             delete_leaf(temp);
  256.     }
  257.     else if ((temp -> left) == NULL)
  258.         delete_right(temp);
  259.     else if ((temp -> right == NULL))
  260.         delete_left(temp);
  261.     else
  262.         super_delete(temp);
  263. }
  264. void inorder(BSTNode *temp)
  265. {
  266.     if (temp != NULL)
  267.     {
  268.         inorder(temp -> left);
  269.         printf("%d ", temp -> data);
  270.         inorder(temp -> right);
  271.     }
  272.     else
  273.         return;
  274. }
  275. void preorder(BSTNode *temp)
  276. {
  277.     if (temp != NULL)
  278.     {
  279.         printf("%d ", temp -> data);
  280.         preorder(temp -> left);
  281.         preorder(temp -> right);
  282.     }
  283.     else
  284.         return;
  285. }
  286. void postorder(BSTNode *temp)
  287. {
  288.     if (temp != NULL)
  289.     {
  290.         postorder(temp -> left);
  291.         postorder(temp -> right);
  292.         printf("%d ", temp -> data);
  293.     }
  294.     else
  295.         return;
  296. }
  297. int main()
  298. {
  299.     int choice, val;
  300.     printf("\nBST operations!!");
  301.     while (1)
  302.     {
  303.         printf("\n1. insert\n2. delete\n3. print\n4. quit: ");
  304.         scanf("%d", &choice);
  305.         switch (choice)
  306.         {
  307.             case 1:
  308.                 //insert
  309.                 printf("\nEnter value of the node: ");
  310.                 scanf("%d", &val);
  311.                 printf("\n");
  312.                 insert_node(val);
  313.                 break;
  314.             case 2:
  315.                 //delete
  316.                 if (root == NULL)
  317.                     printf("\nEmpty BST!\n");
  318.                 else
  319.                 {
  320.                     printf("\nEnter node value to delete: ");
  321.                     scanf("%d", &val);
  322.                     master_delete(val);
  323.                 }
  324.                 printf("\n");
  325.                 break;
  326.             case 3:
  327.                 //display
  328.                 printf("\nPreorder traversal: ");
  329.                 preorder(root);
  330.                 printf("\n");
  331.                 printf("\nInorder traversal: ");
  332.                 inorder(root);
  333.                 printf("\n");
  334.                 printf("\nPostorder traversal: ");
  335.                 postorder(root);
  336.                 printf("\n");
  337.                 break;
  338.             default:
  339.                 exit(0);
  340.         }
  341.     }
  342.     return 0;
  343. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement