vadimk772336

WA56

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