fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. // Node structure
  6. struct Node {
  7. int value;
  8. string color;
  9. Node* left;
  10. Node* right;
  11. Node(int v, string c) : value(v), color(c), left(nullptr), right(nullptr) {}
  12. };
  13.  
  14. // Function to insert a node into the tree
  15. Node* insert(Node* root, int value, string color) {
  16. if (root == nullptr) {
  17. return new Node(value, color);
  18. }
  19. if (value < root->value) {
  20. root->left = insert(root->left, value, color);
  21. } else {
  22. root->right = insert(root->right, value, color);
  23. }
  24. return root;
  25. }
  26.  
  27. // Function to traverse and print tree in preorder
  28. void preorder(Node* root) {
  29. if (root == nullptr) return;
  30. cout << root->value << " ";
  31. preorder(root->left);
  32. preorder(root->right);
  33. }
  34.  
  35. // Function to traverse and print tree in inorder
  36. void inorder(Node* root) {
  37. if (root == nullptr) return;
  38. inorder(root->left);
  39. cout << root->value << " ";
  40. inorder(root->right);
  41. }
  42.  
  43. // Function to traverse and print tree in postorder
  44. void postorder(Node* root) {
  45. if (root == nullptr) return;
  46. postorder(root->left);
  47. postorder(root->right);
  48. cout << root->value << " ";
  49. }
  50.  
  51. //Function to remove nodes with a specific color
  52. Node* removeNode(Node* root, int valueToRemove, string colorToRemove) {
  53. if (root == nullptr) return nullptr;
  54. root->left = removeNode(root->left, valueToRemove, colorToRemove);
  55. root->right = removeNode(root->right, valueToRemove, colorToRemove);
  56. if (root->value == valueToRemove && root->color == colorToRemove) {
  57. // cout << "Removing node with value " << root->value << " and color " << root->color << endl;
  58. if (root->left == nullptr) {
  59. Node* temp = root->right;
  60. delete root;
  61. return temp;
  62. } else if (root->right == nullptr) {
  63. Node* temp = root->left;
  64. delete root;
  65. return temp;
  66. } else {
  67. Node* temp = root->right;
  68. while (temp->left != nullptr) {
  69. temp = temp->left;
  70. }
  71. root->value = temp->value;
  72. root->color = temp->color;
  73. root->right = removeNode(root->right, valueToRemove, colorToRemove);
  74. }
  75. }
  76. return root;
  77. }
  78.  
  79. //Function to remove nodes with a specific color
  80. Node* removeColor(Node* root, string colorToRemove) {
  81. if (root == nullptr) return nullptr;
  82. root->left = removeColor(root->left, colorToRemove);
  83. root->right = removeColor(root->right, colorToRemove);
  84. if (root->color == colorToRemove) {
  85. // cout << "Removing node with value " << root->value << " and color " << root->color << endl;
  86. if (root->left == nullptr) {
  87. Node* temp = root->right;
  88. delete root;
  89. return temp;
  90. } else if (root->right == nullptr) {
  91. Node* temp = root->left;
  92. delete root;
  93. return temp;
  94. } else {
  95. Node* temp = root->right;
  96. while (temp->left != nullptr) {
  97. temp = temp->left;
  98. }
  99. root->value = temp->value;
  100. root->color = temp->color;
  101. root->right = removeNode(root->right, temp->value, temp->color);
  102. }
  103. }
  104. return root;
  105. }
  106.  
  107. int main() {
  108. Node* root = nullptr;
  109. int value;
  110. string color;
  111.  
  112. // Input nodes
  113. while (true) {
  114. cin >> value;
  115. cin >> color;
  116. if (value == -1 && color == "null") break;
  117.  
  118. root = insert(root, value, color);
  119. }
  120.  
  121. // Input color to remove
  122. cin >> color;
  123.  
  124. // Remove nodes with the specified color
  125. root = removeColor(root, color);
  126.  
  127. // Print the result
  128. cout << "Preorder : ";
  129. preorder(root);
  130. cout << endl << "Inorder : ";
  131. inorder(root);
  132. cout << endl << "Postorder : ";
  133. postorder(root);
  134. cout << endl;
  135.  
  136. return 0;
  137. }
  138.  
  139.  
Success #stdin #stdout 0s 5304KB
stdin
6 yellow
3 purple
22 orange
10 yellow
15 yellow
2 purple
7 orange
12 yellow
5 orange
3 yellow
9 purple
4 orange
-1 null
yellow
stdout
Preorder : 7 3 2 5 4 22 9 
Inorder : 2 3 4 5 7 9 22 
Postorder : 2 4 5 3 9 22 7