Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. #include <queue>
  2. #include <iostream>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. struct Node
  7. {
  8. int data;
  9. Node *left, *right;
  10. Node(int data) : data(data), left(nullptr), right(nullptr) {}
  11. };
  12.  
  13. class BST
  14. {
  15. private:
  16.  
  17. Node* root;
  18.  
  19. Node* insertRec(int val, Node* root)
  20. {
  21. if (root == nullptr)
  22. return new Node(val);
  23.  
  24. if (root->data > val)
  25. root->left = insertRec(val, root->left);
  26. else if (root->data < val)
  27. root->right = insertRec(val, root->right);
  28. return root;
  29. }
  30.  
  31. Node* removeRec(int val, Node* root)
  32. {
  33. if (root == nullptr)
  34. return nullptr;
  35.  
  36. if (root->data > val)
  37. root->left = removeRec(val, root->left);
  38. else if (root->data < val)
  39. root->right = removeRec(val, root->right);
  40. else
  41. {
  42. if (root->right == nullptr)
  43. {
  44. Node* tmp = root->left;
  45. delete root;
  46. return tmp;
  47. }
  48. else if (root->left == nullptr)
  49. {
  50. Node* tmp = root->right;
  51. delete root;
  52. return tmp;
  53. }
  54.  
  55. Node* tmp = root->right;
  56. while (tmp->left != nullptr)
  57. tmp = tmp->left;
  58.  
  59. root->data = tmp->data;
  60. root->right = removeRec(tmp->data, root->right);
  61. }
  62.  
  63. return root;
  64. }
  65.  
  66. void printRec(Node* root)
  67. {
  68. if (root)
  69. {
  70. cout << root->data << " ";
  71. printRec(root->left);
  72. printRec(root->right);
  73. }
  74. }
  75.  
  76. public:
  77.  
  78. BST()
  79. {
  80. root = nullptr;
  81. }
  82.  
  83. void insert(int val)
  84. {
  85. root = insertRec(val, root);
  86. }
  87.  
  88. void remove(int val)
  89. {
  90. root = removeRec(val, root);
  91. }
  92.  
  93. void print()
  94. {
  95. printRec(root);
  96. }
  97.  
  98. void printOddLayers()
  99. {
  100. queue<Node*> q;
  101. q.push(root);
  102.  
  103. int currSize = 1;
  104. int level = 1;
  105.  
  106. while (!q.empty())
  107. {
  108. for (int i = 0; i < currSize; i++)
  109. {
  110. Node* tmp = q.front();
  111. q.pop();
  112.  
  113. if (level % 2 != 0)
  114. cout << tmp->data << " ";
  115.  
  116. if (tmp->left)
  117. q.push(tmp->left);
  118. if (tmp->right)
  119. q.push(tmp->right);
  120. }
  121.  
  122. currSize = q.size();
  123. level++;
  124. }
  125. }
  126. };
  127.  
  128. int main() {
  129.  
  130. int N;
  131. cin >> N;
  132.  
  133. BST b;
  134.  
  135. int input;
  136. string command;
  137. for (int i = 0; i < N; i++)
  138. {
  139. cin >> command;
  140. if (command == "add")
  141. {
  142. cin >> input;
  143. b.insert(input);
  144. }
  145. else if (command == "print")
  146. {
  147. b.print();
  148. }
  149. else if (command == "remove")
  150. {
  151. cin >> input;
  152. b.remove(input);
  153. }
  154. else if (command == "print_odd_layers")
  155. {
  156. b.printOddLayers();
  157. }
  158. }
  159.  
  160. return 0;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement