Advertisement
coffeebeforecode

Untitled

Jun 22nd, 2021
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4.  
  5. struct node {
  6.     int data;
  7.     struct node *left_child, *right_child;
  8. };
  9.  
  10. struct node* newNode(int val);
  11. struct node* insert(struct node* root, int data);
  12.  
  13. struct node* minValueNode(struct node* root);
  14. struct node* deleteNode(struct node* root, int data);
  15.  
  16. void printLevelOrder(struct node* root);
  17. void printGivenLevel(struct node* root, int level);
  18. int height(struct node* root);
  19.  
  20. void printPreOrder(struct node* root);
  21. void printInOrder(struct node* root);
  22. void printPostOrder(struct node* root);
  23.  
  24.  
  25. int countNodes(struct node* root);
  26. bool checkCompleteBinaryTree (struct node* root, int index, int number_nodes);
  27.  
  28. void printMaxHeight(int val);
  29.  
  30. int main(){
  31.     int ch;
  32.     struct node* root = NULL;
  33.    
  34.     while (1){
  35.         printf("\n\tMENU\n");
  36.         printf("\n1. Insert into tree");
  37.         printf("\n2. Delete from tree");
  38.         printf("\n3. Print binary search tree Level order");
  39.         printf("\n4. Print binary search tree Preorder");
  40.         printf("\n5. Print binary search tree Inorder");
  41.         printf("\n6. Print binary search tree Postorder");
  42.         printf("\n7. Check if binary search tree is complete");
  43.         printf("\n8. Print BST at height");
  44.         printf("\n9. Exit\n>> ");
  45.         scanf("%d", &ch);
  46.         int val, flag;
  47.         switch (ch){
  48.             case 1:
  49.                 printf("\nEnter value: ");
  50.                 scanf("%d", &val);
  51.                 root = insert(root, val);
  52.                 printf("\nInserted\n");
  53.                 printf("\nInOrder: ");
  54.                 printInOrder(root);
  55.                 break;
  56.             case 2:
  57.                 printf("\nEnter value to delete: ");
  58.                 scanf("%d", &val);
  59.                 root = deleteNode(root, val);
  60.                 printf("\nInOrder: ");
  61.                 printInOrder(root);
  62.                 break;
  63.             case 3:
  64.                 printf("\nLevel Order: ");
  65.                 printLevelOrder(root);
  66.                 printf("\n");
  67.                 break;
  68.             case 4:
  69.                 printf("\nPreOrder: ");
  70.                 printPreOrder(root);
  71.                 printf("\n");
  72.                 break;
  73.             case 5:
  74.                 printf("\nInOrder: ");
  75.                 printInOrder(root);
  76.                 printf("\n");
  77.                 break;
  78.             case 6:
  79.                 printf("\nPostOrder: ");
  80.                 printPostOrder(root);
  81.                 printf("\n");
  82.                 break;
  83.             case 7:
  84.                 if (checkCompleteBinaryTree(root, 0, countNodes(root))){
  85.                     printf("The Binary Tree is complete\n");
  86.                 }
  87.                 else{
  88.                     printf("The Binary Tree is not complete\n");
  89.                 }
  90.                 break;
  91.             case 8:
  92.                 printf("\nEnter the height: ");
  93.                 scanf("%d", &val);
  94.                 printMaxHeight(val);
  95.                 break;
  96.             case 9:
  97.                 exit(0);
  98.             default:
  99.                 printf("\nInvalid option\n");
  100.         }
  101.     }
  102.     return 0;
  103. }
  104.  
  105. struct node* newNode(int val){
  106.     struct node* temp = (struct node*)malloc(sizeof(struct node));
  107.     temp->data = val;
  108.     temp->left_child = temp->right_child = NULL;
  109.     return temp;
  110. }
  111.  
  112. struct node* insert(struct node* root, int data){
  113.     if (root == NULL){
  114.         return newNode(data);
  115.     }
  116.    
  117.     if (data < root->data){
  118.         root->left_child = insert(root->left_child, data);
  119.     }
  120.     else{
  121.         root->right_child = insert(root->right_child, data);
  122.     }
  123.     return root;
  124. }
  125.  
  126. struct node* minValueNode(struct node* root){
  127.     struct node* current = root;
  128.  
  129.     while (current && current->left_child != NULL){
  130.         current = current->left_child;
  131.     }
  132.     return current;
  133. }
  134.  
  135. struct node* deleteNode(struct node* root, int data){
  136.    
  137.     if (root == NULL){
  138.         return root;
  139.     }
  140.    
  141.     if (data < root->data){
  142.         root->left_child = deleteNode(root->left_child, data);
  143.     }
  144.     else if (data > root->data){
  145.         root->right_child = deleteNode(root->right_child, data);
  146.     }
  147.     else {
  148.         if (root->left_child == NULL) {
  149.             struct node* temp = root->right_child;
  150.             free(root);
  151.             return temp;
  152.         }
  153.         else if (root->right_child == NULL) {
  154.             struct node* temp = root->left_child;
  155.             free(root);
  156.             return temp;
  157.         }
  158.  
  159.         struct node* temp = minValueNode(root->right_child);
  160.         root->data = temp->data;
  161.         root->right_child = deleteNode(root->right_child, temp->data);
  162.     }
  163.     return root;
  164. }
  165.  
  166.  
  167. int height(struct node* root){
  168.     if (root == NULL){
  169.         return 0;
  170.     }
  171.     else{
  172.         int lheight = height(root->left_child);
  173.         int rheight = height(root->right_child);
  174.  
  175.         if (lheight > rheight){
  176.             return (lheight + 1);
  177.         }
  178.         else{
  179.             return (rheight  + 1);
  180.         }
  181.     }
  182. }
  183.  
  184. void printGivenLevel(struct node* root, int level){
  185.     if (root == NULL){
  186.         return ;
  187.     }
  188.     if (level == 1){
  189.         printf("%d ", root->data);
  190.     }
  191.     else if (level > 1){
  192.         printGivenLevel(root->left_child, level-1);
  193.         printGivenLevel(root->right_child, level-1);
  194.     }
  195. }
  196.  
  197. void printLevelOrder(struct node* root){
  198.     int h = height(root);
  199.     int i;
  200.     for (i = 1; i <= h; i++){
  201.         printGivenLevel(root, i);
  202.     }
  203. }
  204.  
  205. void printPreOrder(struct node* root){
  206.     // root, left_child, right_child
  207.     if (root != NULL){
  208.         printf("%d ", root->data);
  209.         printPreOrder(root->left_child);
  210.         printPreOrder(root->right_child);
  211.     }
  212. }
  213.  
  214. void printInOrder(struct node* root){
  215.     // left_child, root, right_child
  216.     if (root != NULL){
  217.         printInOrder(root->left_child);
  218.         printf("%d ", root->data);
  219.         printInOrder(root->right_child);
  220.     }
  221. }
  222.  
  223. void printPostOrder(struct node* root){
  224.     // left_child, right_child, root
  225.     if (root != NULL){
  226.         printPostOrder(root->left_child);
  227.         printPostOrder(root->right_child);
  228.         printf("%d ", root->data);
  229.     }
  230. }
  231.  
  232. void printMaxHeight(int val){
  233.     int max_height = val - 1;
  234.     printf("\nThe max height is %d", max_height);
  235. }
  236.  
  237. int countNodes(struct node* root)
  238. {
  239.     if (root == NULL)
  240.         return (0);
  241.     return (1 + countNodes(root->left_child) + countNodes(root->right_child));
  242. }
  243.  
  244. bool checkCompleteBinaryTree (struct node* root, int index, int number_nodes)
  245. {
  246.     // An empty tree is complete
  247.     if (root == NULL)
  248.         return (true);
  249.  
  250.     // If index assigned to current node is more than
  251.     // number of nodes in tree, then tree is not complete
  252.     if (index >= number_nodes)
  253.         return (false);
  254.  
  255.     // Recur for left and right subtrees
  256.     return (checkCompleteBinaryTree(root->left_child, 2*index + 1, number_nodes) &&
  257.     checkCompleteBinaryTree(root->right_child, 2*index + 2, number_nodes));
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement