fork(1) 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 colorToRemove) {
  53. if (root == nullptr) return nullptr;
  54. root->left = removeColor(root->left, colorToRemove);
  55. root->right = removeColor(root->right, colorToRemove);
  56. if (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 = removeColor(root->right, colorToRemove);
  74. }
  75. }
  76. return root;
  77. }
  78.  
  79. int main() {
  80. Node* root = nullptr;
  81. int value;
  82. string color;
  83.  
  84. // Input nodes
  85. while (true) {
  86. cin >> value;
  87. cin >> color;
  88. if (value == -1 && color == "null") break;
  89.  
  90. root = insert(root, value, color);
  91. }
  92.  
  93. // Input color to remove
  94. cin >> color;
  95.  
  96. // Remove nodes with the specified color
  97. root = removeColor(root, color);
  98.  
  99. // Print the result
  100. cout << "Preorder : ";
  101. preorder(root);
  102. cout << endl << "Inorder : ";
  103. inorder(root);
  104. cout << endl << "Postorder : ";
  105. postorder(root);
  106. cout << endl;
  107.  
  108. return 0;
  109. }
  110.  
  111.  
Success #stdin #stdout 0.01s 5288KB
stdin
11 yellow
20 yellow
7 purple
1 orange
9 yellow
12 purple
22 orange
-1 null
yellow
stdout
Preorder : 12 7 1 22 12 22 
Inorder : 1 7 12 12 22 22 
Postorder : 1 7 12 22 22 12