diff --git a/C++/Data-Structures/04-tree/inorder_tree_traversal.cpp b/C++/Data-Structures/04-tree/inorder_tree_traversal.cpp new file mode 100644 index 0000000..3d650a8 --- /dev/null +++ b/C++/Data-Structures/04-tree/inorder_tree_traversal.cpp @@ -0,0 +1,58 @@ +#include +using namespace std; + +// Definition for a binary tree node. +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + + TreeNode(int x) : val(x), left(NULL), right(NULL) {} +}; + +// Function to insert a node in a Binary Search Tree (BST) +TreeNode* insert(TreeNode* root, int val) { + if (root == NULL) { + return new TreeNode(val); + } + if (val < root->val) { + root->left = insert(root->left, val); + } else { + root->right = insert(root->right, val); + } + return root; +} + +// Inorder Traversal of the Binary Tree +void inorderTraversal(TreeNode* root) { + if (root == NULL) { + return; + } + inorderTraversal(root->left); // Traverse left subtree + cout << root->val << " "; // Visit node + inorderTraversal(root->right); // Traverse right subtree +} + +int main() { + TreeNode* root = NULL; + int n, val; + // Taking number of elements as input + cout << "Enter the number of nodes: "; + cin >> n; + // Taking node values as input + cout << "Enter the values of the nodes:\n"; + for (int i = 0; i < n; i++) { + cin >> val; + root = insert(root, val); // Insert each value in the BST + } + // Performing inorder traversal + cout << "Inorder Traversal: "; + inorderTraversal(root); + cout << endl; + return 0; +} + +// Enter the number of nodes: 5 +// Enter the values of the nodes: +// 4 2 5 1 3 +// Inorder Traversal: 1 2 3 4 5 \ No newline at end of file diff --git a/C++/Data-Structures/04-tree/postorder_tree_traversal.cpp b/C++/Data-Structures/04-tree/postorder_tree_traversal.cpp new file mode 100644 index 0000000..083d5a2 --- /dev/null +++ b/C++/Data-Structures/04-tree/postorder_tree_traversal.cpp @@ -0,0 +1,68 @@ +#include +using namespace std; + +// Define the structure for the binary tree node +struct Node { + int data; + Node* left; + Node* right; + + Node(int val) { + data = val; + left = right = nullptr; + } +}; + +// Function to insert nodes in the binary tree +Node* insertNode(Node* root, int data) { + if (root == nullptr) { + return new Node(data); + } + if (data < root->data) { + root->left = insertNode(root->left, data); + } else { + root->right = insertNode(root->right, data); + } + return root; +} + +// Function to perform postorder traversal of the binary tree +void postorderTraversal(Node* root) { + if (root == nullptr) { + return; + } + // Traverse the left subtree + postorderTraversal(root->left); + // Traverse the right subtree + postorderTraversal(root->right); + // Visit the root node + cout << root->data << " "; +} + +int main() { + Node* root = nullptr; + int n, value; + // Take the number of nodes to be inserted + cout << "Enter the number of nodes: "; + cin >> n; + // Insert nodes into the binary tree + for (int i = 0; i < n; i++) { + cout << "Enter value of node " << (i + 1) << ": "; + cin >> value; + root = insertNode(root, value); + } + // Perform postorder traversal + cout << "Postorder Traversal: "; + postorderTraversal(root); + cout << endl; + + return 0; +} + +// Enter the number of nodes: 5 +// Enter value of node 1: 10 +// Enter value of node 2: 5 +// Enter value of node 3: 20 +// Enter value of node 4: 3 +// Enter value of node 5: 7 +// Postorder Traversal: 3 7 5 20 10 \ No newline at end of file diff --git a/C++/Data-Structures/04-tree/preorder_tree_traversal.cpp b/C++/Data-Structures/04-tree/preorder_tree_traversal.cpp new file mode 100644 index 0000000..0b9e7c5 --- /dev/null +++ b/C++/Data-Structures/04-tree/preorder_tree_traversal.cpp @@ -0,0 +1,58 @@ +#include +using namespace std; + +// Definition of a binary tree node +struct TreeNode { + int val; + TreeNode* left; + TreeNode* right; + + TreeNode(int x) : val(x), left(NULL), right(NULL) {} +}; + +// Function to perform preorder traversal +void preorderTraversal(TreeNode* root) { + if (root == NULL) return; + // Visit the root node + cout << root->val << " "; + // Traverse the left subtree + preorderTraversal(root->left); + // Traverse the right subtree + preorderTraversal(root->right); +} + +// Function to create a binary tree from user input +TreeNode* createTree() { + int val; + cout << "Enter node value (-1 for NULL): "; + cin >> val; + // Base case for recursion + if (val == -1) return NULL; + // Create a new node with the given value + TreeNode* root = new TreeNode(val); + // Recursively create the left and right subtrees + cout << "Enter left child of " << val << ": "; + root->left = createTree(); + cout << "Enter right child of " << val << ": "; + root->right = createTree(); + return root; +} + +int main() { + // Create the binary tree + TreeNode* root = createTree(); + // Perform preorder traversal + cout << "Preorder Traversal: "; + preorderTraversal(root); + cout << endl; + return 0; +} + +// Enter node value (-1 for NULL): 1 +// Enter left child of 1: 2 +// Enter left child of 2: -1 +// Enter right child of 2: -1 +// Enter right child of 1: 3 +// Enter left child of 3: -1 +// Enter right child of 3: -1 +// Preorder Traversal: 1 2 3 \ No newline at end of file