Advertisement
Tobiahao

Kolokwium2_Drzewo_BST

Jun 7th, 2017
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.21 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4.  
  5. struct tree_node{
  6.     int key;
  7.     char value;
  8.     struct tree_node *left_child, *right_child;
  9. };
  10.  
  11. void add_node_recursive(struct tree_node **root, int key, char value)
  12. {
  13.     if(*root == NULL){
  14.         *root = (struct tree_node*)malloc(sizeof(struct tree_node));
  15.         if(*root){
  16.             (*root)->key = key;
  17.             (*root)->value = value;
  18.             (*root)->left_child = (*root)->right_child = NULL;
  19.         }
  20.     }
  21.     else{
  22.         if((*root)->key >= key)
  23.             add_node_recursive(&(*root)->left_child, key, value);
  24.         else
  25.             add_node_recursive(&(*root)->right_child, key, value);
  26.     }
  27. }
  28.  
  29. void add_node_with_iteration(struct tree_node **root, int key, int value)
  30. {
  31.     while(*root != NULL){
  32.         if((*root)->key >= key)
  33.             root = &(*root)->left_child;
  34.         else
  35.             root = &(*root)->right_child;
  36.     }
  37.     *root = (struct tree_node*)malloc(sizeof(struct tree_node));
  38.  
  39.     if(*root){
  40.         (*root)->key = key;
  41.         (*root)->value = value;
  42.         (*root)->left_child = (*root)->right_child = NULL;
  43.     }
  44. }
  45.  
  46. struct tree_node *isolate_predecessor(struct tree_node **root)
  47. {
  48.     while(*root && (*root)->left_child)
  49.         root = &(*root)->right_child;
  50.     struct tree_node *predececssor = *root;
  51.     if(*root)
  52.         *root = (*root)->left_child;
  53.     return predececssor;
  54. };
  55.  
  56. void delete_node(struct tree_node **root, int key)
  57. {
  58.     while(*root && (*root)->key != key){
  59.         if((*root)->key > key)
  60.             root = &(*root)->left_child;
  61.         if((*root)->key < key)
  62.             root = &(*root)->right_child;
  63.     }
  64.     if(*root){
  65.         struct tree_node *node = *root;
  66.         if(!node->left_child)
  67.             *root = (*root)->right_child;
  68.         else if(!node->right_child)
  69.             *root = (*root)->left_child;
  70.         else{
  71.             node = isolate_predecessor(&(*root)->left_child);
  72.             (*root)->key = node->key;
  73.             (*root)->value = node->value;
  74.         }
  75.         free(node);
  76.     }
  77. }
  78.  
  79. void print_preorder(struct tree_node *root)
  80. {
  81.     if(root){
  82.         printf("%d->%c  ", root->key, root->value);
  83.         print_preorder(root->left_child);
  84.         print_preorder(root->right_child);
  85.     }
  86. }
  87.  
  88. void print_inorder(struct tree_node *root)
  89. {
  90.     if(root){
  91.         print_inorder(root->left_child);
  92.         printf("%d->%c  ", root->key, root->value);
  93.         print_inorder(root->right_child);
  94.     }
  95. }
  96.  
  97. void print_postorder(struct tree_node *root)
  98. {
  99.     if(root){
  100.         print_postorder(root->left_child);
  101.         print_postorder(root->right_child);
  102.         printf("%d->%c  ", root->key, root->value);
  103.     }
  104. }
  105.  
  106. struct tree_node *find_minimum_recursive(struct tree_node *root)
  107. {
  108.     if(root && root->left_child)
  109.         return find_minimum_recursive(root->left_child);
  110.     else
  111.         return root;
  112. }
  113.  
  114. struct tree_node *find_minimum_with_iteration(struct tree_node *root)
  115. {
  116.     while(root && root->left_child)
  117.         root = root->left_child;
  118.     return root;
  119. };
  120.  
  121. struct tree_node *find_maximum_recursive(struct tree_node *root)
  122. {
  123.     if(root && root->right_child)
  124.         return find_maximum_recursive(root->right_child);
  125.     else
  126.         return root;
  127. }
  128.  
  129. struct tree_node *find_maximum_with_iteration(struct tree_node *root)
  130. {
  131.     while(root && root->right_child)
  132.         root = root->right_child;
  133.     return root;
  134. };
  135.  
  136. struct tree_node *find_node_recursive(struct tree_node *root, int key)
  137. {
  138.     if(root && root->key > key)
  139.         return find_node_recursive(root->left_child, key);
  140.     else if(root && root->key < key)
  141.         return find_node_recursive(root->right_child, key);
  142.     else
  143.         return root;
  144. }
  145.  
  146. struct tree_node *find_node_with_iteration(struct tree_node *root, int key)
  147. {
  148.     while(root){
  149.         if(root->key == key)
  150.             return root;
  151.         if(root->key > key)
  152.             root = root->left_child;
  153.         else
  154.             root = root->right_child;
  155.     }
  156.     return root;
  157. };
  158.  
  159. void remove_tree_nodes(struct tree_node **root)
  160. {
  161.     if(*root){
  162.         remove_tree_nodes(&(*root)->left_child);
  163.         remove_tree_nodes(&(*root)->right_child);
  164.         free(*root);
  165.         *root = NULL;
  166.     }
  167. }
  168.  
  169. void external_or_internal(struct tree_node *root, int key)
  170. {
  171.     root = find_node_recursive(root, key);
  172.     if(root->left_child || root->right_child)
  173.         printf("Wezel o kluczu %d jest wezlem wewnetrznym\n", key);
  174.     else
  175.         printf("Wezel o kluczu %d jest wezlem zewnetrznym\n", key);
  176. }
  177.  
  178. unsigned int height(struct tree_node *root)
  179. {
  180.     if(root == NULL)
  181.         return 0;
  182.  
  183.     unsigned int left_height = height(root->left_child);
  184.     unsigned int right_height = height(root->right_child);
  185.  
  186.     if(left_height > right_height)
  187.         return left_height+1;
  188.     else
  189.         return right_height+1;
  190. }
  191.  
  192. bool full_tree(struct tree_node *root)
  193. {
  194.     if(root == NULL)
  195.         return true;
  196.     if(root->left_child == NULL && root->right_child == NULL)
  197.         return true;
  198.     if(root->left_child && root->right_child)
  199.         return (full_tree(root->left_child) && full_tree(root->right_child));
  200.     return false;
  201. }
  202.  
  203. int main(void)
  204. {
  205.     struct tree_node *root = NULL;
  206.  
  207.     add_node_recursive(&root, 3, 'A');
  208.     add_node_with_iteration (&root, 1, 'B');
  209.     add_node_recursive(&root, 7, 'C');
  210.     add_node_with_iteration (&root, 8, 'D');
  211.  
  212.     puts("Preorder:");
  213.     print_preorder(root);
  214.     printf("\n");
  215.  
  216.     puts("Preorder:");
  217.     print_inorder(root);
  218.     printf("\n");
  219.  
  220.     puts("Postorder:");
  221.     print_postorder(root);
  222.     printf("\n\n");
  223.  
  224.     printf("Minimalny klucz: %d\n", find_minimum_recursive(root)->key);
  225.     printf("Maksymalny klucz: %d\n", find_maximum_with_iteration(root)->key);
  226.     printf("\n");
  227.  
  228.     external_or_internal(root, 3);
  229.     external_or_internal(root, 1);
  230.     printf("\n");
  231.  
  232.     printf("Wysokosc drzewa: %u\n", height(root));
  233.     printf("\n");
  234.  
  235.     if(full_tree(root))
  236.         puts("Drzewo jest pelne");
  237.     else
  238.         puts("Drzewo nie jest pelne");
  239.     printf("\n");
  240.  
  241.     puts("Usuwanie '1':");
  242.     delete_node(&root, 3);
  243.     print_inorder(root);
  244.     printf("\n");
  245.  
  246.     remove_tree_nodes(&root);
  247.     return 0;
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement