Advertisement
konopleva_karina

Untitled

Apr 10th, 2023
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <random>
  3.  
  4. struct Node {
  5. int priority;
  6. int value;
  7. Node* left;
  8. Node* right;
  9.  
  10. Node(int value, std::mt19937& gen) : value(value), left(nullptr), right(nullptr) {
  11. priority = gen();
  12. }
  13. };
  14.  
  15. struct SplitResult {
  16. Node* left;
  17. Node* right;
  18. };
  19.  
  20. class Treap {
  21. Node* Merge(Node* left, Node* right);
  22. SplitResult Split(Node* tree, int value);
  23. Node* FindMin(Node* root, int);
  24. Node* Find(Node* root, int key);
  25. public:
  26. Node* Insert(Node* tree, Node* to_insert);
  27. void Erase(Node* tree, int to_erase);
  28. void Delete(Node* tree);
  29. void Print(Node* tree);
  30. };
  31.  
  32. Node* Treap::Find(Node* root, int key) {
  33. if (root->value == key) return root;
  34. if (root->value > key) return Find(root->left, key);
  35. return Find(root->right, key);
  36. }
  37.  
  38. Node* Treap::FindMin(Node* root, int key) {
  39. if (root->left == nullptr) return root;
  40. if (root->left->value > key) {
  41. FindMin(root->left, key);
  42. }
  43. return root;
  44. }
  45.  
  46. Node* Treap::Merge(Node* left, Node* right) {
  47. if (right == nullptr) {
  48. return left;
  49. }
  50. if (left == nullptr) {
  51. return right;
  52. }
  53. if (left->priority > right->priority) {
  54. left->right = Merge(left->right, right);
  55. return left;
  56. }
  57. right->left = Merge(left, right->left);
  58. return right;
  59. }
  60.  
  61. SplitResult Treap::Split(Node* tree, int value) {
  62. if (tree == nullptr) {
  63. return SplitResult{nullptr, nullptr};
  64. }
  65. if (value > tree->value) {
  66. SplitResult result = Split(tree->right, value);
  67. tree->right = result.left;
  68. return {tree, result.right};
  69. }
  70. SplitResult result = Split(tree->left, value);
  71. tree->left = result.right;
  72. return {result.left, tree};
  73. }
  74.  
  75. Node* Treap::Insert(Node* tree, Node* to_insert) {
  76. SplitResult result = Split(tree, to_insert->value);
  77. Node* tmp = Merge(result.left, to_insert);
  78. Node* tmp2 = Merge(tmp, result.right);
  79. return tmp2;
  80. }
  81.  
  82. void Treap::Erase(Node* tree, int key) {
  83. Node* to_erase = Find(tree, key);
  84. if (to_erase == nullptr) return;
  85. SplitResult result = Split(tree, to_erase->value);
  86. Node* parent = FindMin(result.right, to_erase->value);
  87. SplitResult result_right = Split(result.right, parent->value);
  88. delete result_right.left;
  89. Merge(result.left, result_right.right);
  90. }
  91.  
  92. void Treap::Delete(Node* tree) {
  93. if(tree != nullptr) {
  94. Delete(tree->left);
  95. Delete(tree->right);
  96. delete tree;
  97. }
  98. }
  99.  
  100. void Treap::Print(Node* tree) {
  101. if(tree != nullptr) {
  102. Print(tree->left);
  103. std::cout << "value: " << tree->value << "pr: " << tree->priority << '\n';
  104. Print(tree->right);
  105. }
  106. }
  107.  
  108. int main() {
  109. Treap treap;
  110. std::mt19937 gen;
  111.  
  112. Node* root = nullptr;
  113.  
  114. int value;
  115. int node_value = 5;
  116.  
  117. // 3 5 4 2 1
  118. // 3: -795755684
  119. // 5: 581869302
  120. // 4: -404620562
  121. // 2: -708652711
  122. // 1: 545404204
  123. while (node_value-- > 0) {
  124. std::cin >> value;
  125. Node* new_node = new Node(value, gen);
  126. root = treap.Insert(root, new_node);
  127. }
  128.  
  129. treap.Erase(root, 5);
  130.  
  131. std::cout << root->value << '\n';
  132.  
  133. treap.Print(root);
  134.  
  135. treap.Delete(root);
  136. }
  137.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement