Advertisement
ailuro

10lablast

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