Skip to main content

Binary Search Tree Operations(Program)

Binary Search Tree Operations


MAIN FILE:-
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include "tree.h"

  4. NODE * create(NODE *root);
  5. NODE * insert(NODE *root,int n);
  6. NODE *search(NODE * root, int n);
  7. void display();
  8. void preorder(NODE * root);
  9. void inorder(NODE * root);
  10. void postorder(NODE * root);
  11. NODE * deletebst(NODE *root, int n);

  12. int Node_count(NODE *root);
  13. int Leaf_count(NODE *root);
  14. NODE *treecopy(NODE *root);
  15. int comparebst(NODE *root, NODE *root1);
  16. int sumOdd(NODE *node);
  17. int sumEven(NODE *node1);
  18. void mirror(NODE *root);
  19. int smallest(NODE * root);
  20. int largest(NODE * root);

  21. void main()
  22. {
  23. NODE *root=NULL, *root1=NULL;
  24.     int n, num, osum, esum, choice;
  25. printf("\t\t>>Operations of Binary Search Tree<<\n\n");
  26.     printf("\t\t\t>>First Create BST<<\n");
  27.        
  28.     //CREATE
  29.     root = create(root);
  30.     root1 = create(root1);
  31. do
  32. {
  33. printf("\n\n1.INSERT");
  34. printf("\n2.SEARCH");
  35. printf("\n3.DISPLAY");
  36. printf("\n4.PERODER");
  37. printf("\n5.INODER");
  38. printf("\n6.POSTODER");
  39. printf("\n7.DELETE");
  40. printf("\n8.COUNT OF TOTAL NODES");
  41. printf("\n9.COUNT OF LEAF NODES");
  42. printf("\n10.COPY BST");
  43. printf("\n11.MIRROR OF BST");
  44. printf("\n12.SUM OF ODD NUMBERS");
  45. printf("\n13.SUM OF EVEN NUMBERS");
  46. printf("\n14.COMPARE BST");
  47. printf("\n15.SMALLEST NUMBER IN BST");
  48. printf("\n16.LARGEST NUMBER IN BST");
  49.         printf("\n17.Exit\n");
  50. printf("\n Enter your Choice:\t");
  51.         scanf("%d",&choice);
  52.         
  53. switch(choice)
  54. {
  55. case 1:  //INSERT
  56. printf("\n\nEnter Element you want to insert:\t");
  57.     scanf("%d",&n);
  58. root = insert(root,n);
  59. display(root);
  60. break;
  61. case 2: //SEARCH
  62. printf("Enter Element to be search:\t");
  63. scanf("%d", &n);
  64. search(root, n);
  65. break;
  66. case 3: //DISPLAY
  67. display(root);
  68. break;

  69. case 4:    //PREORDER
  70. printf("\nPerorder traversal is:\t"); 
  71.         preorder(root);
  72. break;

  73. case 5:  //INORDER
  74. printf("\nInorder traversal is:\t");
  75.         inorder(root);
  76. break;

  77. case 6: //POSTORDER
  78. printf("\nPostorder traversal is:\t");
  79.         postorder(root);
  80. break;

  81. case 7:  //DELETE
  82. printf("\n\nEnter Element you want to delete:\t");
  83.     scanf("%d",&n);
  84. root = deletebst(root,n);
  85. display(root);
  86. break;
  87. case 8:  //TOTAL NODE COUNT
  88. num=Node_count(root);
  89. printf("\nTotal Number of Nodes In BST:\t%d",num);
  90. break;
  91. case 9: //LEAF NODE
  92. num=Leaf_count(root);
  93. printf("\n Total Number of Leaf Nodes In BST:\t%d",num);
  94. break;
  95. case 10: //COPY BST
  96. treecopy(root);
  97. display(root);
  98. break;
  99. case 11: //MIRROR OF BST
  100. mirror(root);
  101. display(root);
  102. break;
  103. case 12: //SUM OF ODD NUMBERS
  104. osum=sumOdd(root);
  105. printf("\n Sum of odd numbers is :\t%d",osum);
  106. break;
  107. case 13: //SUM OF EVEN NUMBERS
  108. esum=sumEven(root);
  109. printf("\n Sum of even numbers is :\t%d",esum);
  110. break;
  111. case 14 : //COMPARE
  112. if(comparebst(root, root1))
  113. printf("\n\t>>BOTH TREE ARE EQUAL<<");
  114. else
  115. printf("\n\t>>BOTH TREE ARE NOT EQUAL<<");
  116. break;
  117. case 15: //SMALLEST ELEMENT IN BST
  118. printf("\n\n\t >>Smallest element in BST is:\t %d", smallest(root));
  119. break;
  120. case 16: //LARGEST ELEMENT IN BST
  121. printf("\n\t>>Largest element in BST is:\t %d", largest(root));
  122. break;
  123. case 17 : //EXIT
  124. printf("\t\t>>BYE<<");
  125. break;
  126. default: printf("\t>>ENTER RIGHT CHOICE<<");
  127.            
  128. }
  129. }while(choice!=17);
  130. }

Header File:-
  1.  typedef struct node
  2.  {
  3.     int info;
  4.     struct node *left, *right;
  5.  }NODE;

  6. //CREATE
  7.  NODE * create(NODE *root)
  8.  {
  9.     NODE *nn, *temp;
  10.     int i, n, num;
  11.     printf("\nHow many nodes you want to inserted...?\t ");
  12.     scanf("%d",&n );
  13.     for(i=1; i<=n; i++)
  14.     {
  15.     nn=(NODE*)malloc(sizeof(NODE));
  16.     printf("Enter %d Element:\t",n);
  17.     scanf("%d",&num);
  18.     nn->info = num;
  19.     nn->left=nn->right=NULL;
  20.     if(root==NULL)
  21.     root=nn;
  22.     else
  23.     {
  24.     temp=root;
  25.     while(1)
  26.     {
  27.     if(nn->info < temp->info)
  28.     if(temp->left == NULL)
  29.     {
  30.     temp->left=nn;
  31.     break;
  32.     }
  33.     else
  34.     temp=temp->left;
  35.     else
  36.     if(temp->right == NULL)
  37.     {
  38.     temp->right=nn;
  39.     break;
  40.     }
  41.     else
  42.     temp=temp->right;
  43.     }//while
  44.     }//else
  45.     }//for
  46.     return(root);
  47.  }
  48.  
  49. //DISPLAY
  50. void display(NODE *root)
  51. {
  52. NODE *temp=root;
  53. if(temp != NULL)
  54. {
  55. printf("\t%d",temp->info);
  56. display(temp->left);
  57. display(temp->right);
  58. }
  59. }

  60. //INSERT 
  61. NODE * insert(NODE *root, int n)
  62. {
  63.     NODE *temp, *nn;
  64.     nn = (NODE*)malloc(sizeof(NODE));
  65.     nn->left=nn->right=NULL;
  66.     nn->info=n;

  67.     if(root==NULL)
  68.     {
  69.     root=nn;
  70.     return root;
  71.     }
  72.     else
  73.     {
  74.     temp=root;
  75.     while(1)
  76.     {
  77.     if(n < temp->info)
  78.     {
  79.     if(temp->left==NULL)
  80.     {
  81.     temp->left=nn;
  82.     break;
  83.     }
  84.     else
  85.     temp=temp->left;
  86.     }
  87.     else
  88.     if(n > temp->info)
  89.     {
  90.     if(temp->right==NULL)
  91.     {
  92.     temp->right=nn;
  93.     break;
  94.     }
  95.     else
  96.     temp=temp->right;
  97.     }//if
  98.     }//while
  99.     }//else
  100.     return(root);
  101. }

  102. //PERODER
  103. void preorder(NODE * root)
  104. {
  105.     NODE *temp = root;
  106.     if(temp != NULL)
  107.     {
  108. printf("%d\t", temp->info);
  109.     preorder(temp->left);
  110.     preorder(temp->right);
  111. }
  112. }

  113. //INORDER
  114. void inorder(NODE * root)
  115. {
  116.     NODE *temp = root;
  117.     if(temp != NULL)
  118.     {
  119.     inorder(temp->left);
  120.     printf("%d\t", temp->info);
  121.     inorder(temp->right);
  122.     }
  123. }

  124. //POSTORDER
  125. void postorder(NODE * root)
  126.  {
  127.     NODE *temp = root;
  128.     if(temp != NULL)
  129.     {
  130.     postorder(temp->left);
  131.         postorder(temp->right);
  132.         printf("%d\t", temp->info);
  133.     }
  134.  }

  135.  //Delete 
  136.  NODE * deletebst(NODE *root, int n)
  137.  {
  138.     NODE * temp = root;
  139. NODE * prev = NULL;
  140. while(temp != NULL && temp->info != n)
  141. {
  142. prev = temp;
  143. if(n < temp->info)
  144. temp = temp->left;
  145. else
  146. temp = temp->right;
  147. }
  148. if(temp==NULL)
  149. {
  150. printf("\t'%d' Not Found In BST\n",n);
  151. return root;
  152. }
  153. if(temp->left == NULL || temp->right == NULL)
  154. {
  155. NODE *nn;
  156. if(temp->left == NULL)
  157. nn = temp->right;
  158. else
  159. nn = temp->left;
  160. if(prev == NULL)
  161. return nn;
  162. if(temp == prev->left)
  163. prev->left = nn;
  164. else
  165. prev->right = nn;
  166. free(temp);
  167. }
  168. else
  169. {
  170. NODE * p = NULL;
  171. NODE * temp1;
  172. temp1 = temp->right;
  173. while(temp1->left != NULL)
  174. {
  175. p = temp1;
  176. temp1 = temp1->left;
  177. }
  178. if(p != NULL)
  179. p->left = temp1->right;
  180. else
  181. temp->right = temp1->right;
  182. temp->info = temp1->info;
  183. free(temp1);
  184. }
  185. return root;
  186. }
  187.     
  188. //Search
  189. NODE *search(NODE * root, int n)
  190. {
  191. NODE *temp = root;
  192. while(temp != NULL)
  193. {
  194. if(temp->info==n)
  195. {
  196. printf("\t'%d'  Is Present In BST", n);
  197. return temp;
  198. }
  199. if(n < temp->info)
  200. temp=temp->left;
  201. else
  202. temp=temp->right;
  203. }
  204. printf("\t>>'%d' IS Not Present In BST<<",n);
  205. }

  206. //Count Total Nodes
  207. int Node_count(NODE *root)
  208. {
  209. static int count=0;
  210. NODE  *temp=root;
  211. if(temp != NULL)
  212. {
  213. count++;
  214. Node_count(temp->left);
  215. Node_count(temp->right);
  216. }
  217. return (count);
  218. }

  219. //Count Leaf Node
  220. int Leaf_count(NODE *root)
  221. {
  222. static int count=0;
  223. NODE *temp=root;
  224. if(temp != NULL)
  225. {
  226. if(temp->left==NULL && temp->right==NULL)
  227. count++;
  228. Leaf_count(temp->left);
  229. Leaf_count(temp->right);
  230. }
  231. return (count);
  232. }

  233. //COPY TREE
  234. NODE *treecopy(NODE *root)
  235. {
  236. NODE *nn;
  237. if(root != NULL)
  238. {
  239. nn=(NODE*)malloc(sizeof(NODE));
  240. nn->info=root->info;
  241. nn->left = treecopy(root->left);
  242. nn->right = treecopy(root->right);
  243. return (nn);
  244. }
  245. else
  246. return NULL;
  247. }

  248. //MIRROR
  249. void mirror(NODE *root)
  250. {
  251. NODE *temp;
  252. if(root != NULL)
  253. {
  254. if(root->left)
  255. mirror(root->left);
  256. if(root->right)
  257. mirror(root->left);
  258. temp=root->left;
  259. root->left = root->right;
  260. root->right = temp;
  261. }
  262. }


  263. //SUM OF ODD NUMBERS
  264. int sumOdd(NODE *node)
  265. {
  266. int osum=0;
  267. if(node != NULL)
  268. {
  269. if((node->info % 2 )!= 0)
  270. osum += node->info;
  271. osum += sumOdd(node->left);
  272. osum += sumOdd(node->right);
  273. }
  274. return osum;
  275. }


  276. //SUM OF EVEN NUMBERS
  277. int sumEven(NODE *node1)
  278. {
  279. int esum=0;
  280. if(node1 != NULL)
  281. {
  282. if((node1->info % 2 )== 0)
  283. esum += node1->info;
  284. esum += sumEven(node1->left);
  285. esum += sumEven(node1->right);
  286. }
  287. return esum;
  288. }


  289. //COMPARE
  290. int comparebst(NODE *root, NODE *root1)
  291. {
  292. if(root==NULL && root1==NULL)
  293. return 1;
  294. if(root != NULL && root1 != NULL)
  295. {
  296. return
  297. (
  298. root->info == root1->info &&
  299. comparebst(root->left, root1->left) &&
  300. comparebst(root->right, root1->right)
  301. );
  302. }
  303. return 0;
  304. }

  305. //SMALLEST
  306. int smallest(NODE * root) 
  307. {
  308. NODE *temp = root;
  309. while (temp->left != NULL) 
  310. {
  311. temp = temp->left;
  312. }
  313. return(temp->info);
  314. }

  315. //largest
  316. int largest(NODE * root) 
  317. {
  318. NODE *temp = root;
  319. while (temp->right != NULL) 
  320. {
  321. temp = temp->right;
  322. }
  323. return(temp->info);
  324. }






                                           
                                                                                                                             //ThE ProFessoR

Comments