Advertisement
palenda21

15A/B

Jun 3rd, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.81 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. struct tree
  5. {
  6.     int D;
  7.     tree* l;
  8.     tree* r;
  9. };
  10.  
  11. void add(tree*& top, int element)
  12. {
  13.     if (top == NULL)
  14.     {
  15.         top = new tree();
  16.         top->D = element;
  17.         top->l = NULL;
  18.         top->r = NULL;
  19.     }
  20.     else if (element < top->D) add(top->l, element);
  21.     else add(top->r, element);
  22. }
  23.  
  24. void print_pramoi(tree* top)
  25. {
  26.     if (top != NULL)
  27.     {
  28.         cout << "  " << top->D;
  29.         print_pramoi(top->l);
  30.         print_pramoi(top->r);
  31.     }
  32. }
  33.  
  34. void print_simetr(tree* top)
  35. {
  36.     if (top != NULL)
  37.     {
  38.         print_simetr(top->l);
  39.         cout << "  " << top->D;
  40.         print_simetr(top->r);
  41.     }
  42. }
  43.  
  44.  
  45. void print_obr(tree* top)
  46. {
  47.     if (top != NULL)
  48.     {
  49.         print_obr(top->l);
  50.         print_obr(top->r);
  51.         cout << "  " << top->D;
  52.     }
  53. }
  54.  
  55. void delet(tree* top)
  56. {
  57.     if (top != NULL)
  58.     {
  59.         delet(top->l);
  60.         delet(top->r);
  61.         delete top;
  62.         top = NULL;
  63.     }
  64. }
  65.  
  66. void part_A(tree* top,int &kolvo)
  67. {
  68.     if (top != NULL  )
  69.     {
  70.         part_A(top->l,kolvo);
  71.         if (top->l != NULL && top->r == NULL)
  72.             if (top->l->l == NULL && top->l->r == NULL)
  73.                 kolvo++;
  74.         part_A(top->r,kolvo);
  75.     }
  76. }
  77.  
  78. tree *found_before_max_in_Right_tree(tree*top)
  79. {
  80.     while (top->r->r != NULL)
  81.         top = top->r;
  82.  
  83.     return top;
  84. }
  85.  
  86. void part_B_change_elements(tree*&top_main)
  87. {
  88.     if(top_main->r==NULL){
  89.         cout<<"Right podderevo is empty! Replace is unreal";
  90.         exit(1);
  91.     }
  92.    
  93.     if(top_main==NULL){
  94.         cout<<"Tree is empty!";
  95.         exit(1);
  96.     }
  97.  
  98.     if(top_main->r->r==NULL)
  99.     {
  100.         tree* max=top_main->r;
  101.         tree* max_l=max->l;
  102.        
  103.         tree* top=top_main;
  104.         tree* top_l=top_main->l;
  105.        
  106.         top_main=max;
  107.         max->l=top_l;
  108.         max->r=top;
  109.         top->l=max_l;
  110.         top->r=NULL;
  111.         return;
  112.     }
  113.    
  114.     tree* top=top_main;
  115.     tree* before_max=found_before_max_in_Right_tree(top);
  116.     tree* max=before_max->r;
  117.  
  118.     tree* top_l=top_main->l;
  119.     tree* top_r=top_main->r;
  120.    
  121.     tree* max_l=max->l;
  122.     tree* max_r=max->r;
  123.    
  124.     top=top_main;
  125.     top_main=max;
  126.     max->l=top_l;
  127.     max->r=top_r;
  128.    
  129.     before_max->r=top;
  130.     top->r=max_r;
  131.     top->l=max_l;
  132.    
  133.    
  134. }
  135.  
  136. int main()
  137. {
  138.     tree* top = NULL;
  139.     cout << "Write number of elements: "; int n; cin >> n;
  140.     int element;
  141.  
  142.     for (int i = 0; i < n; i++)
  143.     {
  144.         cin >> element;
  145.         add(top, element);
  146.     }
  147.  
  148.     print_simetr(top); cout << endl;
  149.     cout << "Part A: ";
  150.     int kolvo;
  151.     part_A(top,kolvo); cout << kolvo << endl;
  152.    
  153.     part_B_change_elements(top);
  154.    
  155.     print_simetr(top);
  156.    
  157.     cout << endl;
  158.    
  159.     delet(top);
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement