Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. ///minalataZADACHI
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <string>
  5.  
  6. using namespace std;
  7. struct Node {
  8. int value;
  9. Node* left = nullptr;
  10. Node* right = nullptr;
  11. Node(int value) {
  12. this->value = value;
  13. }
  14. };
  15. class BST {
  16. Node* root = nullptr;
  17. Node* _add(int value, Node* current) {
  18. if (!current) {
  19. return new Node(value);
  20. }
  21. if (current->value < value) {
  22. current->right = _add(value, current->right);
  23. }
  24. else if(current->value > value){
  25. current->left = _add(value, current->left);
  26. }
  27. return current;
  28. }
  29. void _print(Node* current) const {
  30. if (current != nullptr) {
  31. cout << current->value << " ";
  32. _print(current->left);
  33. _print(current->right);
  34. }
  35. }
  36. Node* _remove(int value, Node* current) {
  37. if (current == nullptr) {
  38. return nullptr;
  39. }
  40. if (current->value > value) {
  41. current->left = _remove(value, current->left);
  42. }
  43. else if (current->value < value) {
  44. current->right = _remove(value, current->right);
  45. }
  46. else {
  47. if (current->left == nullptr && current->right == nullptr) {
  48. free(current);
  49. return nullptr;
  50. }
  51. else if (current->left == nullptr) {
  52. Node* temp = current->right;
  53. free(current);
  54. return temp;
  55. }
  56. else if(current->right == nullptr){
  57. Node* temp = current->left;
  58. free(current);
  59. return temp;
  60. }
  61. else {
  62. Node* temp = current->right;
  63. while (temp->left != nullptr) {
  64. temp = temp->left;
  65. }
  66. current->value = temp->value;
  67. current->right = _remove(current->value, current->right);
  68. }
  69. }
  70. return current;
  71. }
  72. void _print_odd_layers(Node* current, bool isOdd) {
  73. if (current == nullptr) return;
  74. if (isOdd == true) {
  75. cout << current->value<<" ";
  76. }
  77. _print_odd_layers(current->left, !isOdd);
  78. _print_odd_layers(current->right, !isOdd);
  79. }
  80.  
  81. public:
  82. BST() {
  83. root = nullptr;
  84. }
  85. void add(int value) {
  86. root = _add(value, root);
  87. }
  88. void print() const {
  89. _print(root);
  90. }
  91. void remove(int value) {
  92. root = _remove(value, root);
  93. }
  94. void print_odd_layers() {
  95. _print_odd_layers(root, true);
  96. }
  97. };
  98. int main() {
  99. int operations;
  100. cin >> operations;
  101. BST bst;
  102. for (int i = 0; i < operations; i++) {
  103. string input;
  104. cin >> input;
  105. if (input == "add") {
  106. int current;
  107. cin >> current;
  108. bst.add(current);
  109. }
  110. else if (input == "print") {
  111. bst.print();
  112. }
  113. else if (input == "print_odd_layers") {
  114. bst.print_odd_layers();
  115. }
  116. else if (input == "remove") {
  117. int value;
  118. cin >> value;
  119. bst.remove(value);
  120. }
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement