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* removeColor(Node* root, string color) {
  53. if (root == nullptr) return nullptr;
  54. root->left = removeColor(root->left, color);
  55. root->right = removeColor(root->right, color);
  56. if (root->color == color) {
  57. if (root->left == nullptr) {
  58. Node* temp = root->right;
  59. delete root;
  60. return temp;
  61. } else if (root->right == nullptr) {
  62. Node* temp = root->left;
  63. delete root;
  64. return temp;
  65. } else {
  66. Node* temp = root->right;
  67. while (temp->left != nullptr) {
  68. temp = temp->left;
  69. }
  70. root->value = temp->value;
  71. root->color = temp->color;
  72. root->right = removeColor(root->right, temp->color);
  73. }
  74. }
  75. return root;
  76. }
  77.  
  78. int main() {
  79. Node* root = nullptr;
  80. int value;
  81. string color;
  82.  
  83. // Input nodes
  84. while (true) {
  85. cin >> value;
  86. cin >> color;
  87. if (value == -1 && color == "null") break;
  88.  
  89. root = insert(root, value, color);
  90. }
  91.  
  92. getline(cin, color);
  93.  
  94. // Input color to remove
  95. cin >> color;
  96.  
  97. // Remove nodes with the specified color
  98. root = removeColor(root, color);
  99.  
  100. // Print the result
  101. cout << "Preorder : ";
  102. preorder(root);
  103. cout << endl << "Inorder : ";
  104. inorder(root);
  105. cout << endl << "Postorder : ";
  106. postorder(root);
  107. cout << endl;
  108.  
  109. return 0;
  110. }
  111.  
Success #stdin #stdout 0.01s 5280KB
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
purple
stdout
Preorder : 6 5 3 4 22 10 7 15 12 
Inorder : 3 4 5 6 7 10 12 15 22 
Postorder : 4 3 5 7 12 15 10 22 6