Advertisement
plsnever

Untitled

Apr 10th, 2020
557
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct tree
  5. {
  6.     int inf;
  7.     tree* left;
  8.     tree* right;
  9. };
  10.  
  11. tree* addtree(tree* root, int inf)
  12. {
  13.     if (root == NULL)
  14.     {
  15.         root = new tree;
  16.         root->inf = inf;  
  17.         root->left = NULL;
  18.         root->right = NULL;
  19.     }
  20.     else  if (inf < root->inf)  
  21.         root->left = addtree(root->left,inf);
  22.     else  
  23.         root->right = addtree(root->right,inf);
  24.     return root;
  25. }
  26.  
  27. tree* addtree_b(int left, int right, int arr[])   //функция создания нового дерева
  28. {
  29.     tree* root;
  30.     int i;
  31.     if (left > right)return NULL;
  32.  
  33.     i = (left + right) / 2;
  34.     root = new tree;
  35.     root->inf = arr[i];
  36.     root->left = addtree_b(left, i - 1, arr);
  37.     root->right = addtree_b(i + 1, right, arr);
  38.     return root;
  39. }
  40.  
  41.  
  42. void pre_showtree(tree* root)
  43. {
  44.     if (root)
  45.     {
  46.         cout << root->inf << endl;
  47.         pre_showtree(root->left);
  48.         pre_showtree(root->right);
  49.     }
  50. }
  51.  
  52. void insertsort(int arr[], int size)
  53. {
  54.     int i, j;
  55.     int temp;
  56.     for (i = 1; i < size; ++i)
  57.     {
  58.         temp = arr[i];
  59.         for (j = i - 1; j >= 0 && temp < arr[j]; j--)
  60.         {
  61.             arr[j + 1] = arr[j];
  62.  
  63.         }
  64.         arr[j + 1] = temp;
  65.     }
  66. }
  67.  
  68. void searchmax(tree* root)
  69. {
  70.     if (root->left == NULL)
  71.     {
  72.         cout << root->inf;
  73.         return;
  74.     }
  75.     root = root->left;
  76.    
  77.  
  78.     while (root)
  79.     {
  80.         if (root->left == NULL && root->right == NULL)
  81.         {
  82.            
  83.             delete root;
  84.             root = NULL;
  85.             return ;
  86.         }
  87.         root = root->right;
  88.     }
  89.  
  90.    
  91. }
  92.  
  93.  
  94.  
  95.  
  96. int main()
  97. {
  98.     tree* root = NULL;
  99.     tree* rmax = NULL;
  100.     bool ques=false;
  101.     char q;
  102.     int size;
  103.     cout << "enter the size: ";
  104.     cin >> size;
  105.     int* arr = new int[size];
  106.     for (int i = 0; i < size; ++i)
  107.     {
  108.         cout << "\n enter arr[" << i << "]: ";
  109.         cin >> arr[i];
  110.     }
  111.     cout << endl << " balanced tree (y/n)";
  112.  
  113.     cin >> q;
  114.     while (!ques)
  115.     {
  116.         if (q == 'y')
  117.         {
  118.             insertsort(arr, size);
  119.             for (int i = 0; i < size; ++i)
  120.             {
  121.                 cout << arr[i] << " ";
  122.  
  123.             }
  124.             root = addtree_b(0, size - 1, arr);
  125.             ques = true;
  126.         }
  127.         else if (q == 'n')
  128.         {
  129.             int inf;
  130.             for (int i = 0; i < size; ++i)
  131.             {
  132.                 inf = arr[i];
  133.                 root=addtree(root,inf);
  134.                 cout << arr[i] << " ";
  135.  
  136.             }
  137.             ques = true;
  138.  
  139.         }
  140.         else
  141.         {
  142.             cout << "enter  y or n";
  143.             cin >> q;
  144.         }
  145.     }
  146.     cout << endl;
  147.     cout << "its ur tree(pre-order): ";
  148.     pre_showtree(root);
  149.     cout << endl;
  150.     cout << "answer :";
  151.     searchmax(root);   
  152.     cout << "its ur tree(pre-order): ";
  153.     pre_showtree(root);
  154.  
  155.  
  156.     return 0;
  157.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement