Advertisement
ailuro

lab10right

Dec 15th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5. #include <math.h>
  6. #include <locale.h>
  7. #define SIZE 10
  8.  
  9. struct node
  10. {
  11.     int data;
  12.     struct node *right_child;
  13.     struct node *left_child;
  14. };
  15.  
  16.  
  17.  
  18.  
  19. struct node* find_minimum(struct node *root)
  20. {
  21.     if(root == NULL)
  22.         return NULL;
  23.     else if(root->left_child != NULL)
  24.         return find_minimum(root->left_child);
  25.     return root;
  26. }
  27.  
  28.  
  29. struct node* new_node(int x)
  30. {
  31.     struct node *p;
  32.     p = malloc(sizeof(struct node));
  33.     p->data = x;
  34.     p->left_child = NULL;
  35.     p->right_child = NULL;
  36.  
  37.     return p;
  38. }
  39.  
  40. struct node* insert(struct node *root, int x)
  41. {
  42.  
  43.     if(root==NULL)
  44.         return new_node(x);
  45.     else if(x>root->data)
  46.         root->right_child = insert(root->right_child, x);
  47.     else
  48.         root->left_child = insert(root->left_child,x);
  49.     return root;
  50. }
  51.  
  52.  
  53. struct node* delete(struct node *root, int x)
  54. {
  55.  
  56.     if(root==NULL)
  57.         return NULL;
  58.     if (x>root->data)
  59.         root->right_child = delete(root->right_child, x);
  60.     else if(x<root->data)
  61.         root->left_child = delete(root->left_child, x);
  62.     else
  63.     {
  64.         if(root->left_child==NULL && root->right_child==NULL)
  65.         {
  66.             free(root);
  67.             return NULL;
  68.         }
  69.  
  70.         else if(root->left_child==NULL || root->right_child==NULL)
  71.         {
  72.             struct node *temp;
  73.             if(root->left_child==NULL)
  74.                 temp = root->right_child;
  75.             else
  76.                 temp = root->left_child;
  77.             free(root);
  78.             return temp;
  79.         }
  80.  
  81.         else
  82.         {
  83.             struct node *temp = find_minimum(root->right_child);
  84.             root->data = temp->data;
  85.             root->right_child = delete(root->right_child, temp->data);
  86.         }
  87.     }
  88.     return root;
  89. }
  90.  
  91. void display(struct node *root)
  92. {
  93.     if(root == NULL)
  94.         return;
  95.     else
  96.     {
  97.         display(root->left_child);
  98.  
  99.         printf("%d ", root->data);
  100.  
  101.         display(root->right_child);
  102.     }
  103.  
  104.  
  105.  
  106. }
  107. int count_height(struct node *root)
  108. {
  109.     if(root!=NULL)
  110.     {
  111.         int a = count_height(root->left_child);
  112.         int b = count_height(root->right_child);
  113.         if (a>b)
  114.  
  115.             return a+1;
  116.         else
  117.             return b+1;
  118.     }
  119.     else
  120.         return 0;
  121.  
  122. }
  123.  
  124. int count_sum(struct node *root,  int level, int i)
  125. {
  126.     int sum = 0;
  127.     if(root == NULL)
  128.         return 0;
  129.     if(i==level)
  130.         return root->data;
  131.     else {
  132.         sum += count_sum(root->left_child,  level, i+1);
  133.         sum += count_sum(root->right_child, level, i+1);
  134.     }
  135.     return sum;
  136.  
  137. }
  138.  
  139.  
  140.  
  141. int main()
  142. {
  143.  
  144.     struct node *root;
  145.     int y, level=0, sum = 0, num_levels;
  146.  
  147.     printf("Введите вершину дерева\n");
  148.     scanf("%d", &y);
  149.     root = new_node(y);
  150.  
  151.     int choice = 10;
  152.  
  153.  
  154.  
  155.  
  156.     while(choice!=5)
  157.     {
  158.         printf("\n1.Добавить элемент\n2.Удалить элемент\n3.Показать элементы очереди\n4.Показать сумму элементов на уровнях\n5.Выход\n");
  159.         scanf("%d", &choice);
  160.         switch (choice)
  161.         {
  162.         case 1:
  163.             printf("Введите элемент\n");
  164.  
  165.             scanf("%d", &y);
  166.             insert(root,y);
  167.             break;
  168.  
  169.         case 2:
  170.             printf("Введите элемент\n");
  171.  
  172.             scanf("%d", &y);
  173.             delete(root, y);
  174.             break;
  175.  
  176.         case 3:
  177.             printf("элементы: ");
  178.  
  179.             display(root);
  180.             break;
  181.         case 4:
  182.             num_levels = count_height(root);
  183.             for(int i = 0; i<count_height(root); i++)
  184.             {
  185.                 sum = 0;
  186.  
  187.                 printf("lvl%d, sum: %d\n", i, count_sum(root, i, 0));
  188.             }
  189.             break;
  190.  
  191.         case 5:
  192.             exit(-1);
  193.         default:
  194.             printf("\nПовторите ввод\n");
  195.         }
  196.  
  197.     }
  198.  
  199.  
  200.  
  201.     return 0;
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement