C Program to Construct a Binary Search Tree and Perform Deletion and Inorder Traversal

This C Program constructs binary search tree and perform deletion, inorder traversal on it.

Here is source code of the C Program to construct a binary search tree and perform deletion, inorder traversal on it. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /* 
  2.  * C Program to Construct a Binary Search Tree and perform deletion, inorder traversal on it
  3.  */ 
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. struct btnode
  8. {
  9.     int value;
  10.     struct btnode *l;
  11.     struct btnode *r;
  12. }*root = NULL, *temp = NULL, *t2, *t1;
  13.  
  14. void delete1();
  15. void insert();
  16. void delete();
  17. void inorder(struct btnode *t);
  18. void create();
  19. void search(struct btnode *t);
  20. void preorder(struct btnode *t);
  21. void postorder(struct btnode *t);
  22. void search1(struct btnode *t,int data);
  23. int smallest(struct btnode *t);
  24. int largest(struct btnode *t);
  25.  
  26. int flag = 1;
  27.  
  28. void main()
  29. {
  30.     int ch;
  31.  
  32.     printf("\nOPERATIONS ---");
  33.     printf("\n1 - Insert an element into tree\n");
  34.     printf("2 - Delete an element from the tree\n");
  35.     printf("3 - Inorder Traversal\n");
  36.     printf("4 - Preorder Traversal\n");
  37.     printf("5 - Postorder Traversal\n");
  38.     printf("6 - Exit\n");
  39.     while(1)
  40.     {
  41.         printf("\nEnter your choice : ");
  42.         scanf("%d", &ch);
  43.         switch (ch)
  44.         {
  45.         case 1:    
  46.             insert();
  47.             break;
  48.         case 2:    
  49.             delete();
  50.             break;
  51.         case 3:    
  52.             inorder(root);
  53.             break;
  54.         case 4:    
  55.             preorder(root);
  56.             break;
  57.         case 5:    
  58.             postorder(root);
  59.             break;
  60.         case 6:    
  61.             exit(0);
  62.         default :     
  63.             printf("Wrong choice, Please enter correct choice  ");
  64.             break;    
  65.         }
  66.     }
  67. }
  68.  
  69. /* To insert a node in the tree */
  70. void insert()
  71. {
  72.     create();
  73.     if (root == NULL) 
  74.         root = temp;
  75.     else    
  76.         search(root);    
  77. }
  78.  
  79. /* To create a node */
  80. void create()
  81. {
  82.     int data;
  83.  
  84.     printf("Enter data of node to be inserted : ");
  85.     scanf("%d", &data);
  86.     temp = (struct btnode *)malloc(1*sizeof(struct btnode));
  87.     temp->value = data;
  88.     temp->l = temp->r = NULL;
  89. }
  90.  
  91. /* Function to search the appropriate position to insert the new node */
  92. void search(struct btnode *t)
  93. {
  94.     if ((temp->value > t->value) && (t->r != NULL))      /* value more than root node value insert at right */
  95.         search(t->r);
  96.     else if ((temp->value > t->value) && (t->r == NULL))
  97.         t->r = temp;
  98.     else if ((temp->value < t->value) && (t->l != NULL))    /* value less than root node value insert at left */
  99.         search(t->l);
  100.     else if ((temp->value < t->value) && (t->l == NULL))
  101.         t->l = temp;
  102. }
  103.  
  104. /* recursive function to perform inorder traversal of tree */
  105. void inorder(struct btnode *t)
  106. {
  107.     if (root == NULL)
  108.     {
  109.         printf("No elements in a tree to display");
  110.         return;
  111.     }
  112.     if (t->l != NULL)    
  113.         inorder(t->l);
  114.     printf("%d -> ", t->value);
  115.     if (t->r != NULL)    
  116.         inorder(t->r);
  117. }
  118.  
  119. /* To check for the deleted node */
  120. void delete()
  121. {
  122.     int data;
  123.  
  124.     if (root == NULL)
  125.     {
  126.         printf("No elements in a tree to delete");
  127.         return;
  128.     }
  129.     printf("Enter the data to be deleted : ");
  130.     scanf("%d", &data);
  131.     t1 = root;
  132.     t2 = root;
  133.     search1(root, data);
  134. }
  135.  
  136. /* To find the preorder traversal */
  137. void preorder(struct btnode *t)
  138. {
  139.     if (root == NULL)
  140.     {
  141.         printf("No elements in a tree to display");
  142.         return;
  143.     }
  144.     printf("%d -> ", t->value);
  145.     if (t->l != NULL)    
  146.         preorder(t->l);
  147.     if (t->r != NULL)    
  148.         preorder(t->r);
  149. }
  150.  
  151. /* To find the postorder traversal */
  152. void postorder(struct btnode *t)
  153. {
  154.     if (root == NULL)
  155.     {
  156.         printf("No elements in a tree to display ");
  157.         return;
  158.     }
  159.     if (t->l != NULL) 
  160.         postorder(t->l);
  161.     if (t->r != NULL) 
  162.         postorder(t->r);
  163.     printf("%d -> ", t->value);
  164. }
  165.  
  166. /* Search for the appropriate position to insert the new node */
  167. void search1(struct btnode *t, int data)
  168. {
  169.     if ((data>t->value))
  170.     {
  171.         t1 = t;
  172.         search1(t->r, data);
  173.     }
  174.     else if ((data < t->value))
  175.     {
  176.         t1 = t;
  177.         search1(t->l, data);
  178.     }
  179.     else if ((data==t->value))
  180.     {
  181.         delete1(t);
  182.     }
  183. }
  184.  
  185. /* To delete a node */
  186. void delete1(struct btnode *t)
  187. {
  188.     int k;
  189.  
  190.     /* To delete leaf node */
  191.     if ((t->l == NULL) && (t->r == NULL))
  192.     {
  193.         if (t1->l == t)
  194.         {
  195.             t1->l = NULL;
  196.         }
  197.         else
  198.         {
  199.             t1->r = NULL;
  200.         }
  201.         t = NULL;
  202.         free(t);
  203.         return;
  204.     }
  205.  
  206.     /* To delete node having one left hand child */
  207.     else if ((t->r == NULL))
  208.     {
  209.         if (t1 == t)
  210.         {
  211.             root = t->l;
  212.             t1 = root;
  213.         }
  214.         else if (t1->l == t)
  215.         {
  216.             t1->l = t->l;
  217.  
  218.         }
  219.         else
  220.         {
  221.             t1->r = t->l;
  222.         }
  223.         t = NULL;
  224.         free(t);
  225.         return;
  226.     }
  227.  
  228.     /* To delete node having right hand child */
  229.     else if (t->l == NULL)
  230.     {
  231.         if (t1 == t)
  232.         {
  233.             root = t->r;
  234.             t1 = root;
  235.         }
  236.         else if (t1->r == t)
  237.             t1->r = t->r;
  238.         else
  239.             t1->l = t->r;
  240.         t == NULL;
  241.         free(t);
  242.         return;
  243.     }
  244.  
  245.     /* To delete node having two child */
  246.     else if ((t->l != NULL) && (t->r != NULL))  
  247.     {
  248.         t2 = root;
  249.         if (t->r != NULL)
  250.         {
  251.             k = smallest(t->r);
  252.             flag = 1;
  253.         }
  254.         else
  255.         {
  256.             k =largest(t->l);
  257.             flag = 2;
  258.         }
  259.         search1(root, k);
  260.         t->value = k;
  261.     }
  262.  
  263. }
  264.  
  265. /* To find the smallest element in the right sub tree */
  266. int smallest(struct btnode *t)
  267. {
  268.     t2 = t;
  269.     if (t->l != NULL)
  270.     {
  271.         t2 = t;
  272.         return(smallest(t->l));
  273.     }
  274.     else    
  275.         return (t->value);
  276. }
  277.  
  278. /* To find the largest element in the left sub tree */
  279. int largest(struct btnode *t)
  280. {
  281.     if (t->r != NULL)
  282.     {
  283.         t2 = t;
  284.         return(largest(t->r));
  285.     }
  286.     else    
  287.         return(t->value);
  288. }

$ cc tree43.c
$ a.out
OPERATIONS ---
1 - Insert an element into tree
2 - Delete an element from the tree
3 - Inorder Traversal
4 - Preorder Traversal
5 - Postorder Traversal
6 - Exit
 
Enter your choice : 1
Enter data of node to be inserted : 40
 
Enter your choice : 1
Enter data of node to be inserted : 20
 
Enter your choice : 1
Enter data of node to be inserted : 10
 
Enter your choice : 1
Enter data of node to be inserted : 30
 
Enter your choice : 1
Enter data of node to be inserted : 60
 
Enter your choice : 1
Enter data of node to be inserted : 80
 
Enter your choice : 1
Enter data of node to be inserted : 90
 
Enter your choice : 3
10 -> 20 -> 30 -> 40 -> 60 -> 80 -> 90 ->
 
            40
            /\
           /  \
         20    60
         / \    \
       10  30   80
                  \
                  90

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 wish to look at programming examples on all topics, go to C Programming Examples.

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.