Skip to main content

Binary Search Tree Operations

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.  
    2.  typedef struct node
    3.  {
    4.     int info;
    5.     struct node *left, *right;
    6.  }NODE;

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

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

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

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

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

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

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

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

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

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

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


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


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


    290. //SMALLEST
    291. int smallest(NODE * root) 
    292. {
    293. NODE *temp = root;
    294. while (temp->left != NULL) 
    295. {
    296. temp = temp->left;
    297. }
    298. return(temp->info);
    299. }

    300. //LARGEST
    301. int largest(NODE * root) 
    302. {
    303. NODE *temp = root;
    304. while (temp->right != NULL) 
    305. {
    306. temp = temp->right;
    307. }
    308. return(temp->info);
    309. }

    310. //COMPARE
    311. int comparebst(NODE *root1, NODE *root2)
    312. {
    313. if(root1==NULL && root2==NULL)
    314. return 1;
    315. if(root1 != NULL && root2 != NULL)
    316. {
    317. return
    318. (
    319. root1->info == root2->info &&
    320. comparebst(root1->left, root2->left) &&
    321. comparebst(root1->right, root2->right)
    322. );
    323. }
    324. return 0;
    325. }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            Comments