Advertisement
kirilstanoev

main.c

Jul 4th, 2021
1,060
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.15 KB | None | 0 0
  1. //#include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. struct node_t
  5. {
  6.     int value;
  7.     int height;
  8.  
  9.     struct node_t* left;
  10.     struct node_t* right;
  11. };
  12.  
  13. // insert
  14. // contains
  15. // print
  16.  
  17. struct node_t* create_node(int value)
  18. {
  19.     struct node_t* new_node = malloc(sizeof(struct node_t));
  20.     new_node->value = value;
  21.     new_node->height = 1;
  22.  
  23.     new_node->left = NULL;
  24.     new_node->right = NULL;
  25. };
  26.  
  27. int max(int a, int b)
  28. {
  29.     return a > b ? a : b;
  30. }
  31.  
  32. int get_node_height(struct node_t* node)
  33. {
  34.     if (node == NULL)
  35.     {
  36.         return 0;
  37.     }
  38.  
  39.     return node->height;
  40. }
  41.  
  42. void update_node_height(struct node_t* node)
  43. {
  44.     node->height = 1 + max(get_node_height(node->left), get_node_height(node->right));
  45. }
  46.  
  47. struct node_t* insert(struct node_t* root, int value)
  48. {
  49.     if (root == NULL)
  50.     {
  51.         return create_node(value);
  52.     }
  53.  
  54.     if (value > root->value)
  55.     {
  56.         if (root->right == NULL)
  57.         {
  58.             root->right = create_node(value);
  59.         }
  60.         else
  61.         {
  62.             insert(root->right, value);
  63.         }
  64.     }
  65.     else if (value < root->value)
  66.     {
  67.         if (root->left == NULL)
  68.         {
  69.             root->left = create_node(value);
  70.         }
  71.         else
  72.         {
  73.             insert(root->left, value);
  74.         }
  75.     }
  76.     else
  77.     {
  78.         return root;
  79.     }
  80.  
  81.     update_node_height(root);
  82.  
  83.     return root;
  84. }
  85.  
  86. void print_bst_internal(struct node_t* root, int detailed, int level)
  87. {
  88.     if (root == NULL)
  89.     {
  90.         return;
  91.     }
  92.  
  93.     printf("%d at level %d with height: %d\n", root->value, level, root->height);
  94.     if (detailed)
  95.     {
  96.         if (root->left != NULL)
  97.         {
  98.             printf("%d at level %d has a left child %d\n", root->value, level, root->left->value);
  99.         }
  100.         else
  101.         {
  102.             printf("%d at level %d has no left child\n", root->value, level);
  103.         }
  104.         if (root->right != NULL)
  105.         {
  106.             printf("%d at level %d has a right child %d\n", root->value, level, root->right->value);
  107.         }
  108.         else
  109.         {
  110.             printf("%d at level %d has no right child\n", root->value, level);
  111.         }
  112.     }
  113.  
  114.     if (root->left != NULL)
  115.     {
  116.         print_bst_internal(root->left, detailed, level + 1);
  117.     }
  118.     if (root->right != NULL)
  119.     {
  120.         print_bst_internal(root->right, detailed, level + 1);
  121.     }
  122. }
  123.  
  124. void print_bst(struct node_t* root, int detailed)
  125. {
  126.     print_bst_internal(root, detailed, 0);
  127. }
  128.  
  129. int search(struct node_t* node, int search_value)
  130. {
  131.     if (node == NULL)
  132.     {
  133.         return 0;
  134.     }
  135.     if (node->value == search_value)
  136.     {
  137.         return 1;
  138.     }
  139.     if (search_value > node->value)
  140.     {
  141.         return search(node->right, search_value);
  142.     }
  143.     if (search_value < node->value)
  144.     {
  145.         return search(node->left, search_value);
  146.     }
  147. }
  148.  
  149. void visit(struct node_t* node)
  150. {
  151.     if (node == NULL)
  152.     {
  153.         return;
  154.     }
  155.  
  156.     // pre-order visit
  157.     printf(" %d ", node->value);
  158.     visit(node->left);
  159.     visit(node->right);
  160.  
  161.     // // in-order visit
  162.     // visit(node->left);
  163.     // printf(" %d ", node->value);
  164.     // visit(node->right);
  165.     //
  166.     // // post-order visit
  167.     // visit(node->left);
  168.     // visit(node->right);
  169.     // printf(" %d ", node->value);
  170. }
  171.  
  172. int main()
  173. {
  174.     // struct node_t root;
  175.     // root.value = 4;
  176.     // root.left = NULL;
  177.     // root.right = NULL;
  178.     struct node_t* root = insert(NULL, 4);
  179.  
  180.     insert(root, 2);
  181.     insert(root, 5);
  182.     insert(root, 1);
  183.     insert(root, 3);
  184.     insert(root, 7);
  185.  
  186.     visit(root);
  187.  
  188.     // search(root, 3);
  189.  
  190.      //print_bst(root, 0);
  191.  
  192.     return 0;
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement