|
| 1 | +#include <iostream> |
| 2 | + |
| 3 | +/* |
| 4 | + * Structure for a binary tree node |
| 5 | + */ |
| 6 | + |
| 7 | +struct Node { |
| 8 | + int data; // data value stored in the node |
| 9 | + Node* left; // pointer to the left child node |
| 10 | + Node* right; // pointer to the right child node |
| 11 | + |
| 12 | + /** |
| 13 | + * Constructor to create a new node with the given data. |
| 14 | + * |
| 15 | + * @param value the data value for the new node. |
| 16 | + */ |
| 17 | + |
| 18 | + Node(int value) : data(value), left(nullptr), right(nullptr) {} |
| 19 | +}; |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +/** |
| 24 | + * Function to insert a new node into the binary tree. |
| 25 | + * |
| 26 | + * @param root pointer to the root of the binary tree. |
| 27 | + * @param value the value to be inserted. |
| 28 | + * |
| 29 | + * @return pointer to the root of the modified binary tree. |
| 30 | + */ |
| 31 | + |
| 32 | +Node* insert(Node* root, int value) { |
| 33 | + if (root == nullptr) { |
| 34 | + return new Node(value); |
| 35 | + } |
| 36 | + if (value < root->data) { |
| 37 | + root->left = insert(root->left, value); |
| 38 | + } |
| 39 | + else if (value > root->data) { |
| 40 | + root->right = insert(root->right, value); |
| 41 | + } |
| 42 | + return root; |
| 43 | +} |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +/** |
| 48 | + * Function for an in-order traversal of the binary tree (Left-Root-Right). |
| 49 | + * |
| 50 | + * @param root pointer to the root of the binary tree. |
| 51 | + */ |
| 52 | + |
| 53 | +void inOrderTraversal(Node* root) { |
| 54 | + if (root == nullptr) { |
| 55 | + return; |
| 56 | + } |
| 57 | + inOrderTraversal(root->left); |
| 58 | + std::cout << root->data << " "; |
| 59 | + inOrderTraversal(root->right); |
| 60 | +} |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +/** |
| 65 | + * Function for a pre-order traversal of the binary tree (Root-Left-Right). |
| 66 | + * |
| 67 | + * @param root pointer to the root of the binary tree. |
| 68 | + */ |
| 69 | + |
| 70 | +void preOrderTraversal(Node* root) { |
| 71 | + if (root == nullptr) { |
| 72 | + return; |
| 73 | + } |
| 74 | + std::cout << root->data << " "; |
| 75 | + preOrderTraversal(root->left); |
| 76 | + preOrderTraversal(root->right); |
| 77 | +} |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | +/** |
| 82 | + * Function for a post-order traversal of the binary tree (Left-Right-Root). |
| 83 | + * |
| 84 | + * @param root pointer to the root of the binary tree. |
| 85 | + */ |
| 86 | + |
| 87 | +void postOrderTraversal(Node* root) { |
| 88 | + if (root == nullptr) { |
| 89 | + return; |
| 90 | + } |
| 91 | + postOrderTraversal(root->left); |
| 92 | + postOrderTraversal(root->right); |
| 93 | + std::cout << root->data << " "; |
| 94 | +} |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | +int main() { |
| 99 | + Node* root = nullptr; |
| 100 | + |
| 101 | + // insert elements into the binary tree |
| 102 | + root = insert(root, 50); |
| 103 | + root = insert(root, 30); |
| 104 | + root = insert(root, 20); |
| 105 | + root = insert(root, 40); |
| 106 | + root = insert(root, 70); |
| 107 | + root = insert(root, 60); |
| 108 | + root = insert(root, 80); |
| 109 | + |
| 110 | + // in-order traversal |
| 111 | + std::cout << "In-order traversal: "; |
| 112 | + inOrderTraversal(root); |
| 113 | + std::cout << std::endl; |
| 114 | + |
| 115 | + // pre-order traversal |
| 116 | + std::cout << "Pre-order traversal: "; |
| 117 | + preOrderTraversal(root); |
| 118 | + std::cout << std::endl; |
| 119 | + |
| 120 | + // post-order traversal |
| 121 | + std::cout << "Post-order traversal: "; |
| 122 | + postOrderTraversal(root); |
| 123 | + std::cout << std::endl; |
| 124 | + |
| 125 | + return 0; |
| 126 | +} |
0 commit comments