Advertisement
Vanya_Shestakov

Untitled

Nov 9th, 2021
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. class Node {
  2. public:
  3. int value;
  4. Node* left;
  5. Node* right;
  6.  
  7. Node(int value) {
  8. this->value = value;
  9. left = nullptr;
  10. right = nullptr;
  11. }
  12. };
  13.  
  14. class BinaryTree {
  15. private:
  16. Node* root;
  17. const int MAX_KEY = 999;
  18. public:
  19.  
  20. BinaryTree() {
  21. root = nullptr;
  22. }
  23.  
  24. void add(int value) {
  25. if (value < MAX_KEY) {
  26. Node *node = new Node(value);
  27. if (root != nullptr) {
  28. Node *current = root;
  29. Node *prev;
  30. bool posNotFound = true;
  31. while (posNotFound) {
  32. prev = current;
  33. if (value < current->value) {
  34. current = current->left;
  35. if (current == nullptr) {
  36. prev->left = node;
  37. posNotFound = false;
  38. }
  39. } else {
  40. current = current->right;
  41. if (current == nullptr) {
  42. prev->right = node;
  43. posNotFound = false;
  44. }
  45. }
  46. }
  47. } else {
  48. root = node;
  49. }
  50. }
  51. }
  52.  
  53. void deleteValue(int value) {
  54. root = deleteRecursively(root, value);
  55. }
  56.  
  57. Node* deleteRecursively(Node *current, int value) {
  58. if (current == nullptr) {
  59. return nullptr;
  60. }
  61. if (value < current->value) {
  62. current->left = deleteRecursively(current->left, value);
  63. return current;
  64. } else if (value > current->value) {
  65. current->right = deleteRecursively(current->right, value);
  66. return current;
  67. }
  68. if (current->left == nullptr) {
  69. return current->right;
  70. } else if (current->right == nullptr) {
  71. return current->left;
  72. } else {
  73. current->value = findSmallestKey(current->right);
  74. current->right = deleteRecursively(current->right, current->value);
  75. return current;
  76. }
  77. }
  78.  
  79. int findSmallestKey(Node *current) {
  80. return current->left == nullptr ? current->value : findSmallestKey(current->left);
  81. }
  82.  
  83. std::string toString() {
  84. std::string result = "";
  85. return recursiveDepthFirst(root, result);
  86. }
  87.  
  88. void printLeafs() {
  89. int counter = 0;
  90. recursivePrintLeafs(root, counter);
  91. std::cout << "amount:\n";
  92. std::cout << counter;
  93. }
  94.  
  95. void recursivePrintLeafs(Node *current, int &counter) {
  96. if (current != nullptr) {
  97. recursivePrintLeafs(current->left, counter);
  98. if (current->left == nullptr && current->right == nullptr) {
  99. std::cout << current->value << std::endl;
  100. counter++;
  101. }
  102. recursivePrintLeafs(current->right, counter);
  103. }
  104. }
  105.  
  106. std::string recursiveDepthFirst(Node *current, std::string &result) {
  107. if (current != nullptr) {
  108. recursiveDepthFirst(current->left, result);
  109. result += std::to_string(current->value) + " ";
  110. recursiveDepthFirst(current->right, result);
  111. }
  112. return result;
  113. }
  114.  
  115.  
  116.  
  117. std::string toStringBreadthFirst() {
  118. std::string result = "";
  119. if (root != nullptr) {
  120. std::queue<Node*> nodes;
  121. nodes.push(root);
  122. while (!nodes.empty()) {
  123. Node *current = nodes.front();
  124. nodes.pop();
  125. result += std::to_string(current->value) + " ";
  126. if (current->left != nullptr) {
  127. nodes.push(current->left);
  128. }
  129.  
  130. if (current->right != nullptr) {
  131. nodes.push(current->right);
  132. }
  133. }
  134. } else {
  135. result = "";
  136. }
  137. return result;
  138. }
  139.  
  140.  
  141. void clear() {
  142. while (root != nullptr) {
  143. deleteValue(root->value);
  144. }
  145. }
  146.  
  147. };
  148.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement