fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. // Define the structure of a tree node
  5. struct TreeNode {
  6. int value;
  7. TreeNode* left;
  8. TreeNode* right;
  9.  
  10. // Constructor to create a new node
  11. TreeNode(int val) : value(val), left(nullptr), right(nullptr) {}
  12. };
  13.  
  14. // Binary Tree class
  15. class BinaryTree {
  16. private:
  17. TreeNode* root;
  18.  
  19. // Helper function for in-order traversal
  20. void inorderHelper(TreeNode* node) {
  21. if (node == nullptr) return;
  22. inorderHelper(node->left);
  23. cout << node->value << " ";
  24. inorderHelper(node->right);
  25. }
  26.  
  27. // Helper function for pre-order traversal
  28. void preorderHelper(TreeNode* node) {
  29. if (node == nullptr) return;
  30. cout << node->value << " ";
  31. preorderHelper(node->left);
  32. preorderHelper(node->right);
  33. }
  34.  
  35. // Helper function for post-order traversal
  36. void postorderHelper(TreeNode* node) {
  37. if (node == nullptr) return;
  38. postorderHelper(node->left);
  39. postorderHelper(node->right);
  40. cout << node->value << " ";
  41. }
  42.  
  43. // Helper function to insert a node recursively
  44. TreeNode* insertHelper(TreeNode* node, int value) {
  45. // If the tree is empty, return a new node
  46. if (node == nullptr) return new TreeNode(value);
  47.  
  48. // Otherwise, recur down the tree
  49. if (value < node->value)
  50. node->left = insertHelper(node->left, value);
  51. else
  52. node->right = insertHelper(node->right, value);
  53.  
  54. return node;
  55. }
  56.  
  57. public:
  58. // Constructor
  59. BinaryTree() : root(nullptr) {}
  60.  
  61. // Public insert function
  62. void insert(int value) {
  63. root = insertHelper(root, value);
  64. }
  65.  
  66. // Public function to perform in-order traversal
  67. void inorder() {
  68. inorderHelper(root);
  69. cout << endl;
  70. }
  71.  
  72. // Public function to perform pre-order traversal
  73. void preorder() {
  74. preorderHelper(root);
  75. cout << endl;
  76. }
  77.  
  78. // Public function to perform post-order traversal
  79. void postorder() {
  80. postorderHelper(root);
  81. cout << endl;
  82. }
  83. };
  84.  
  85. // Driver code
  86. int main() {
  87. BinaryTree tree;
  88.  
  89. // Inserting nodes into the binary tree
  90. tree.insert(50);
  91. tree.insert(30);
  92. tree.insert(20);
  93. tree.insert(40);
  94. tree.insert(70);
  95. tree.insert(60);
  96. tree.insert(80);
  97.  
  98.  
  99. cout << "In-order traversal: ";
  100. tree.inorder(); // Should print: 20 30 40 50 60 70 80
  101.  
  102. cout << "Pre-order traversal: ";
  103. tree.preorder(); // Should print: 50 30 20 40 70 60 80
  104.  
  105. cout << "Post-order traversal: ";
  106. tree.postorder(); // Should print: 20 40 30 60 80 70 50
  107.  
  108. return 0;
  109.  
  110. }
  111.  
Success #stdin #stdout 0.01s 5284KB
stdin
 
stdout
In-order traversal: 20 30 40 50 60 70 80 
Pre-order traversal: 50 30 20 40 70 60 80 
Post-order traversal: 20 40 30 60 80 70 50