vadimk772336

убрал лишнии перем

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