Advertisement
GerONSo

Untitled

Nov 15th, 2021
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <utility>
  7. #include <random>
  8.  
  9. void doOnStart() {
  10. std::ios::sync_with_stdio(0);
  11. std::cin.tie(nullptr);
  12. std::cout.tie(nullptr);
  13. #ifdef _offline
  14. freopen("input.txt", "r", stdin);
  15. freopen("output.txt", "w", stdout);
  16. #endif
  17. }
  18.  
  19. struct Node {
  20. public:
  21. int key;
  22. int maxHeight = 0;
  23. Node* left = nullptr;
  24. Node* right = nullptr;
  25. Node* parent = nullptr;
  26.  
  27. Node() = default;
  28.  
  29. explicit Node(int key) : key(key) {
  30. }
  31.  
  32. ~Node() {
  33. if (left != nullptr) {
  34. delete left;
  35. }
  36. if (right != nullptr) {
  37. delete right;
  38. }
  39. }
  40. };
  41.  
  42. class SplayTree {
  43. public:
  44. SplayTree() = default;
  45.  
  46. ~SplayTree() {
  47. delete root_;
  48. }
  49.  
  50. void insert(int key) {
  51. if (root_ == nullptr) {
  52. root_ = new Node(key);
  53. root_->maxHeight = 1;
  54. return;
  55. }
  56. if (find(key) == nullptr) {
  57. std::pair<Node*, Node*> trees = split(key);
  58. Node* new_node = new Node(key);
  59. new_node->left = trees.first;
  60. new_node->right = trees.second;
  61. new_node->maxHeight = max(new_node->left, new_node->right) + 1;
  62. if (new_node->left != nullptr) {
  63. new_node->left->parent = new_node;
  64. }
  65. if (new_node->right != nullptr) {
  66. new_node->right->parent = new_node;
  67. }
  68. root_ = new_node;
  69. }
  70. }
  71.  
  72. Node* find(int key) {
  73. Node* result = goDown(root_, key);
  74. splay(result);
  75. return result->key == key ? result : nullptr;
  76. }
  77.  
  78. int splay(Node* node) {
  79. int count = 0;
  80. while (node->parent != nullptr) {
  81. Node* parent = node->parent;
  82. if (parent->left == node) {
  83. if (parent->parent == nullptr) {
  84. zig(node->parent);
  85. } else if (parent->parent->left == parent) {
  86. zig(parent->parent);
  87. zig(parent);
  88. } else {
  89. zig(parent);
  90. zag(parent);
  91. }
  92. } else {
  93. if (parent->parent == nullptr) {
  94. zag(node->parent);
  95. } else if (parent->parent->left == parent) {
  96. zag(parent->parent);
  97. zag(parent);
  98. } else {
  99. zag(parent);
  100. zig(parent);
  101. }
  102. }
  103. count++;
  104. }
  105. return count;
  106. }
  107.  
  108. int getHeight() const {
  109. return root_->maxHeight;
  110. }
  111.  
  112. private:
  113. Node* root_;
  114.  
  115. void zag(Node* node) {
  116. Node* parent = node->parent;
  117. Node* right = node->right;
  118. if (right == nullptr) {
  119. return;
  120. }
  121. if (parent != nullptr) {
  122. if (parent->left == node) {
  123. parent->left = right;
  124. } else {
  125. parent->right = right;
  126. }
  127. }
  128. Node* tmp = right->left;
  129. right->left = node;
  130. node->right = tmp;
  131.  
  132. right->parent = parent;
  133. node->parent = right;
  134. if (tmp != nullptr) {
  135. tmp->parent = node;
  136. }
  137.  
  138. node->maxHeight = max(node->left, node->right) + 1;
  139. right->maxHeight = max(right->left, right->right) + 1;
  140. }
  141.  
  142. void zig(Node* node) {
  143. Node* parent = node->parent;
  144. Node* left = node->left;
  145. if (left == nullptr) {
  146. return;
  147. }
  148. if (parent != nullptr) {
  149. if (parent->left == node) {
  150. parent->left = left;
  151. } else {
  152. parent->right = left;
  153. }
  154. }
  155. Node* tmp = left->right;
  156. left->right = node;
  157. node->left = tmp;
  158.  
  159. left->parent = parent;
  160. node->parent = left;
  161. if (tmp != nullptr) {
  162. tmp->parent = node;
  163. }
  164.  
  165. node->maxHeight = max(node->left, node->right) + 1;
  166. left->maxHeight = max(left->left, left->right) + 1;
  167. }
  168.  
  169. Node* goDown(Node* current, int key) const {
  170. if (current == nullptr) {
  171. return nullptr;
  172. }
  173. if (current->key == key || (current->left == nullptr && current->right == nullptr)) {
  174. return current;
  175. }
  176. if (key < current->key) {
  177. return goDown(current->left, key);
  178. } else {
  179. return goDown(current->right, key);
  180. }
  181. }
  182.  
  183. std::pair<Node*, Node*> split(int key) {
  184. splay(goDown(root_, key));
  185. if (key <= root_->key) {
  186. Node* left = root_->left;
  187. root_->left = nullptr;
  188. return {left, root_};
  189. } else {
  190. Node* right = root_->right;
  191. root_->right = nullptr;
  192. return {root_, right};
  193. }
  194. }
  195.  
  196. void merge(Node* left, Node* right) {
  197. Node* max_left = maxNode(left);
  198. splay(max_left);
  199. max_left->right = right;
  200. }
  201.  
  202. Node* maxNode(Node* current) {
  203. if (current->right == nullptr) {
  204. return current;
  205. }
  206. return maxNode(current->right);
  207. }
  208.  
  209. int max(Node* node_left, Node* node_right) {
  210. if (node_left == nullptr) {
  211. if (node_right == nullptr) {
  212. return 0;
  213. } else {
  214. return node_right->maxHeight;
  215. }
  216. } else {
  217. if (node_right == nullptr) {
  218. return node_left->maxHeight;
  219. } else {
  220. return std::max(node_left->maxHeight, node_right->maxHeight);
  221. }
  222. }
  223. }
  224. };
  225.  
  226. signed main() {
  227. doOnStart();
  228. SplayTree tree = SplayTree();
  229. tree.insert(1);
  230. tree.insert(4);
  231. Node* e = tree.find(1);
  232. std::cout << e->maxHeight << '\n';
  233. return 0;
  234. }
  235.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement