C Program to Perform Deletion in Binary Search Tree

This is a C Program to perform deletion in binary search tree. The important thing to note is, inorder successor is needed only when right child is not empty. In this particular case, inorder successor can be obtained by finding the minimum value in right child of the node.

Here is source code of the C Program to Perform Deletion in a BST. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. // C program to demonstrate delete operation in binary search tree
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4.  
  5. struct node {
  6.     int key;
  7.     struct node *left, *right;
  8. };
  9.  
  10. // A utility function to create a new BST node
  11. struct node *newNode(int item) {
  12.     struct node *temp = (struct node *) malloc(sizeof(struct node));
  13.     temp->key = item;
  14.     temp->left = temp->right = NULL;
  15.     return temp;
  16. }
  17.  
  18. // A utility function to do inorder traversal of BST
  19. void inorder(struct node *root) {
  20.     if (root != NULL) {
  21.         inorder(root->left);
  22.         printf("%d ", root->key);
  23.         inorder(root->right);
  24.     }
  25. }
  26.  
  27. /* A utility function to insert a new node with given key in BST */
  28. struct node* insert(struct node* node, int key) {
  29.     /* If the tree is empty, return a new node */
  30.     if (node == NULL)
  31.         return newNode(key);
  32.  
  33.     /* Otherwise, recur down the tree */
  34.     if (key < node->key)
  35.         node->left = insert(node->left, key);
  36.     else
  37.         node->right = insert(node->right, key);
  38.  
  39.     /* return the (unchanged) node pointer */
  40.     return node;
  41. }
  42.  
  43. /* Given a non-empty binary search tree, return the node with minimum
  44.  key value found in that tree. Note that the entire tree does not
  45.  need to be searched. */
  46. struct node * minValueNode(struct node* node) {
  47.     struct node* current = node;
  48.  
  49.     /* loop down to find the leftmost leaf */
  50.     while (current->left != NULL)
  51.         current = current->left;
  52.  
  53.     return current;
  54. }
  55.  
  56. /* Given a binary search tree and a key, this function deletes the key
  57.  and returns the new root */
  58. struct node* deleteNode(struct node* root, int key) {
  59.     // base case
  60.     if (root == NULL)
  61.         return root;
  62.  
  63.     // If the key to be deleted is smaller than the root's key,
  64.     // then it lies in left subtree
  65.     if (key < root->key)
  66.         root->left = deleteNode(root->left, key);
  67.  
  68.     // If the key to be deleted is greater than the root's key,
  69.     // then it lies in right subtree
  70.     else if (key > root->key)
  71.         root->right = deleteNode(root->right, key);
  72.  
  73.     // if key is same as root's key, then This is the node
  74.     // to be deleted
  75.     else {
  76.         // node with only one child or no child
  77.         if (root->left == NULL) {
  78.             struct node *temp = root->right;
  79.             free(root);
  80.             return temp;
  81.         } else if (root->right == NULL) {
  82.             struct node *temp = root->left;
  83.             free(root);
  84.             return temp;
  85.         }
  86.  
  87.         // node with two children: Get the inorder successor (smallest
  88.         // in the right subtree)
  89.         struct node* temp = minValueNode(root->right);
  90.  
  91.         // Copy the inorder successor's content to this node
  92.         root->key = temp->key;
  93.  
  94.         // Delete the inorder successor
  95.         root->right = deleteNode(root->right, temp->key);
  96.     }
  97.     return root;
  98. }
  99.  
  100. // Driver Program to test above functions
  101. int main() {
  102.     /* Let us create following BST
  103.        50
  104.      /     \
  105.    30      70
  106.   /  \    /  \
  107.  20   40  60   80 */
  108.     struct node *root = NULL;
  109.     root = insert(root, 50);
  110.     root = insert(root, 30);
  111.     root = insert(root, 20);
  112.     root = insert(root, 40);
  113.     root = insert(root, 70);
  114.     root = insert(root, 60);
  115.     root = insert(root, 80);
  116.  
  117.     printf("Inorder traversal of the given tree \n");
  118.     inorder(root);
  119.  
  120.     printf("\nDelete 20\n");
  121.     root = deleteNode(root, 20);
  122.     printf("Inorder traversal of the modified tree \n");
  123.     inorder(root);
  124.  
  125.     printf("\nDelete 30\n");
  126.     root = deleteNode(root, 30);
  127.     printf("Inorder traversal of the modified tree \n");
  128.     inorder(root);
  129.  
  130.     printf("\nDelete 50\n");
  131.     root = deleteNode(root, 50);
  132.     printf("Inorder traversal of the modified tree \n");
  133.     inorder(root);
  134.  
  135.     return 0;
  136. }

Output:

$ gcc DeleteBST.c
$ ./a.out
 
Inorder traversal of the given tree 
20 30 40 50 60 70 80 
Delete 20
Inorder traversal of the modified tree 
30 40 50 60 70 80 
Delete 30
Inorder traversal of the modified tree 
40 50 60 70 80 
Delete 50
Inorder traversal of the modified tree 
40 60 70 80

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.