Advertisement
coffeebeforecode

dsa-lab-fat-complete

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