Advertisement
ailuro

Untitled

Dec 15th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.06 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.     int sum = 0;
  94.  
  95.     if(root == NULL)
  96.         return;
  97.     else
  98.     {
  99.  
  100.         display(root->left_child);
  101.  
  102.  
  103.         printf("%d ", root->data);
  104.         sum+= root->data;
  105.  
  106.  
  107.  
  108.         display(root->right_child);
  109.  
  110.  
  111.  
  112.     }
  113.     printf("sum: %d\n",sum);
  114.  
  115.  
  116. }
  117. int count_height(struct node *root)
  118. {
  119.     if(root!=NULL)
  120.     {
  121.         int a = count_height(root->left_child);
  122.         int b = count_height(root->right_child);
  123.         if (a>b)
  124.  
  125.             return a+1;
  126.         else
  127.             return b+1;
  128.     }
  129.     else
  130.         return 0;
  131.  
  132. }
  133.  
  134. void count_sum(struct node *root, int sum, int level, int num_levels)
  135. {
  136.     sum = 0;
  137.     if(root == NULL)
  138.        return;
  139.  
  140.     else {
  141.         sum += root->data;
  142.  
  143.  
  144.  
  145.         count_sum(root->left_child, sum, level+1, num_levels);
  146.         count_sum(root->right_child, sum, level+1, num_levels);
  147.  
  148.  
  149.  
  150.  
  151.     }
  152.     printf("lvl%d, sum: %d\n", level, sum);
  153.     return;
  154.  
  155. }
  156.  
  157.  
  158.  
  159. int main()
  160. {
  161.  
  162.     struct node *root;
  163.     int y, level=0, sum = 0, num_levels;
  164.  
  165.     printf("Введите вершину дерева\n");
  166.     scanf("%d", &y);
  167.     root = new_node(y);
  168.  
  169.     int choice = 10;
  170.  
  171.  
  172.  
  173.  
  174.     while(choice!=5)
  175.     {
  176.         printf("\n1.Добавить элемент\n2.Удалить элемент\n3.Показать элементы очереди\n4.Показать сумму элементов на уровнях\n5.Выход\n");
  177.         scanf("%d", &choice);
  178.         switch (choice)
  179.         {
  180.         case 1:
  181.             printf("Введите элемент\n");
  182.  
  183.             scanf("%d", &y);
  184.             insert(root,y);
  185.             break;
  186.  
  187.         case 2:
  188.             printf("Введите элемент\n");
  189.  
  190.             scanf("%d", &y);
  191.             delete(root, y);
  192.             break;
  193.  
  194.         case 3:
  195.             printf("элементы: ");
  196.  
  197.             display(root);
  198.             break;
  199.         case 4:
  200.             num_levels = count_height(root);
  201.             count_sum(root, sum, level, num_levels);
  202.  
  203.  
  204.  
  205.             break;
  206.  
  207.         case 5:
  208.             exit(-1);
  209.         default:
  210.             printf("\nПовторите ввод\n");
  211.         }
  212.  
  213.     }
  214.  
  215.  
  216.  
  217.     return 0;
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement