Advertisement
pasholnahuy

Бинарное дерево (вставка, поиск, обход)

May 28th, 2023
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <tuple>
  6. #include <set>
  7.  
  8. using namespace std;
  9.  
  10. class BST {
  11. struct Node {
  12. int key;
  13. int depth;
  14. Node *l = nullptr, *r = nullptr;
  15.  
  16. explicit Node(int key) : key(key) {}
  17. }
  18. *root = nullptr;
  19.  
  20. bool contains(Node *n, int key) const {
  21. if (!n) {
  22. return false;
  23. } else if (key == n->key) {
  24. return true;
  25. } else if (key > n->key) {
  26. return contains(n->r, key);
  27. } else {
  28. return contains(n->l, key);
  29. }
  30. }
  31.  
  32. void insert(Node *n, int key) {
  33. if (!root) {
  34. root = new Node(key);
  35. return;
  36. }
  37. if (key == n->key) {
  38. return;
  39. }
  40. if (key < n->key) {
  41. if (n->l) {
  42. insert(n->l, key);
  43. } else {
  44. n->l = new Node(key);
  45. n->l->depth = n->depth + 1;
  46. }
  47.  
  48. } else {
  49. if (n->r) {
  50. insert(n->r, key);
  51. } else {
  52. n->r = new Node(key);
  53. n->r->depth = n->depth + 1;
  54. }
  55. }
  56. }
  57.  
  58. void MakeDepth(Node *n, int cur_depth) {
  59. if (n == nullptr) {
  60. return;
  61. }
  62. n->depth = cur_depth;
  63. MakeDepth(n->l, cur_depth + 1);
  64. MakeDepth(n->r, cur_depth + 1);
  65. }
  66.  
  67. int depth(Node *n) {
  68. if (n == nullptr) return 0;
  69. return max(depth(n->l), depth(n->r)) + 1;
  70. }
  71.  
  72. bool Search(Node *cur_node, int val) {
  73. if (cur_node == nullptr) {
  74. return false;
  75. }
  76. if (cur_node->key == val) {
  77. return true;
  78. }
  79. if (cur_node->key < val) {
  80. return Search(cur_node->r, val);
  81. }
  82. return Search(cur_node->l, val);
  83. }
  84.  
  85. void order(Node *n) {
  86. if (n->l) {
  87. order(n->l);
  88. }
  89. for (size_t i = 0; i < n->depth; ++i){
  90. cout << '.';
  91. }
  92. cout << n->key << '\n';
  93. if (n->r) {
  94. order(n->r);
  95. }
  96. }
  97.  
  98. public:
  99. bool contains(int key) const {
  100. return contains(root, key);
  101. }
  102.  
  103. void insert(int key) {
  104. return insert(root, key);
  105. }
  106.  
  107. int depth() {
  108. return depth(root);
  109. }
  110.  
  111. void MakeDepth(){
  112. return MakeDepth(root, 1);
  113. }
  114.  
  115. void order() {
  116. return order(root);
  117. }
  118.  
  119. bool Search(int n) {
  120. return Search(root, n);
  121. }
  122. };
  123.  
  124. int main() {
  125. BST tree;
  126. string query;
  127. while (cin >> query) {
  128. if (query == "ADD") {
  129. int n;
  130. cin >> n;
  131. if (tree.contains(n)) {
  132. cout << "ALREADY\n";
  133. } else {
  134. tree.insert(n);
  135. cout << "DONE\n";
  136. }
  137. } else if (query == "SEARCH") {
  138. int n;
  139. cin >> n;
  140. if (tree.Search(n)) {
  141. cout << "YES\n";
  142. } else {
  143. cout << "NO\n";
  144. }
  145. } else {
  146. tree.order();
  147. }
  148. }
  149. return 0;
  150. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement