vadimk772336

без принтов

Nov 22nd, 2021
858
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.22 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Node
  5. {
  6.     int key;
  7.     int idx;
  8.     Node* left;
  9.     Node* right;
  10.     Node* parent;
  11. };
  12.  
  13. void find_parent(
  14.     int idx_left, int idx_right, int idx, Node* tree, int max_l, int max_r, int* l, int* r)
  15. {
  16.  
  17.     bool is_greater = tree[0].key <= tree[idx].key;
  18.     Node* curr;
  19.  
  20.     is_greater ? curr = &tree[idx_right] : curr = &tree[idx_left];
  21.  
  22.     if (curr->key <= tree[idx].key && curr->right == NULL)
  23.     {
  24.         tree[idx].parent = curr;
  25.         curr->right = &tree[idx];
  26.  
  27.         if (is_greater & tree[idx].key > max_r)
  28.             *r = idx;
  29.  
  30.         if (!is_greater & tree[idx].key > max_l)
  31.             *l = idx;
  32.     }
  33.  
  34.     else if (curr->key > tree[idx].key)
  35.     {
  36.         is_greater ? idx_right = tree[idx_right].left->idx : idx_left = tree[idx_left].left->idx;
  37.         find_parent(idx_left, idx_right, idx, tree, max_l, max_r, l, r);
  38.     }
  39.  
  40.     else if (curr->right != NULL)
  41.     {
  42.         is_greater ? idx_right = tree[idx_right].right->idx : idx_left = tree[idx_left].right->idx;
  43.         find_parent(idx_left, idx_right, idx, tree, max_l, max_r, l, r);
  44.     }
  45.  
  46.     return;
  47. }
  48.  
  49.  
  50. void f(int* idx_left, int* idx_right, int k, Node* tree, int n)
  51. {
  52.  
  53.     int max_l, max_r;
  54.  
  55.     (*idx_left == 0) ? max_l = -1 : max_l = tree[*idx_left].key;
  56.     (*idx_right == 0) ? max_r = -1 : max_r = tree[*idx_right].key;
  57.  
  58.  
  59.     while (k < n - 1 & tree[k + 1].key < tree[k].key)
  60.     {
  61.         tree[k].left = &tree[k + 1];
  62.         tree[k + 1].parent = &tree[k];
  63.         k++;
  64.     }
  65.  
  66.     if (k + 1 < n)
  67.     {
  68.         find_parent(*idx_left, *idx_right, k + 1, tree, max_l, max_r, idx_left, idx_right);
  69.     }
  70.  
  71.     if (k + 2 < n)
  72.     {
  73.         f(idx_left, idx_right, k + 1, tree, n);
  74.     }
  75. }
  76.  
  77. void preorderTraversal(Node* x)
  78. {
  79.     if (x != NULL)
  80.     {
  81.         std::cout << x->key << " ";
  82.         preorderTraversal(x->left);
  83.         preorderTraversal(x->right);
  84.     }
  85.     return;
  86. }
  87.  
  88. void inorderTraversal(Node* x)
  89. {
  90.     if (x != NULL)
  91.     {
  92.         inorderTraversal(x->left);
  93.         std::cout << x->key << " ";
  94.         inorderTraversal(x->right);
  95.     }
  96.     return;
  97. }
  98.  
  99. void postorderTraversal(Node* x)
  100. {
  101.     if (x != NULL)
  102.     {
  103.         postorderTraversal(x->left);
  104.         postorderTraversal(x->right);
  105.         std::cout << x->key << " ";
  106.     }
  107.     return;
  108. }
  109.  
  110. void print_tree(Node* tree, int n)
  111. {
  112.     std::cout << std::endl;
  113.     for (int i = 0; i < n; ++i)
  114.     {
  115.         std::cout << "i= " << tree[i].key << ": ";
  116.         if (tree[i].left != NULL)
  117.             std::cout << tree[i].left->key << " ";
  118.         if (tree[i].right != NULL)
  119.             std::cout << tree[i].right->key << " ;";
  120.         std::cout << std::endl;
  121.     }
  122.     std::cout << std::endl;
  123. }
  124.  
  125. int main()
  126. {
  127.     int n;
  128.     std::cin >> n;
  129.  
  130.     int idx_left = 0, idx_right = 0;
  131.     Node tree[n];
  132.  
  133.     for (int i = 0; i < n; ++i)
  134.     {
  135.         tree[i].right = NULL;
  136.         tree[i].left = NULL;
  137.         tree[i].parent = NULL;
  138.         tree[i].idx = i;
  139.         std::cin >> tree[i].key;
  140.     }
  141.  
  142.  
  143.     f(&idx_left, &idx_right, 0, tree, n);
  144.  
  145.     print_tree(tree, n);
  146.     postorderTraversal(&tree[0]);
  147.     std::cout << std::endl;
  148.     inorderTraversal(&tree[0]);
  149.  
  150.     return 0;
  151. }
  152.  
Advertisement
Add Comment
Please, Sign In to add comment