Skip to content

Commit

Permalink
Merge pull request #55 from kumar007ambi/ambikaDSAMastery
Browse files Browse the repository at this point in the history
Tree Related Problems
  • Loading branch information
gautamankoji authored Oct 22, 2024
2 parents 582454a + d337040 commit 13d6352
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 0 deletions.
58 changes: 58 additions & 0 deletions C++/Data-Structures/04-tree/inorder_tree_traversal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <bits/stdc++.h>
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
68 changes: 68 additions & 0 deletions C++/Data-Structures/04-tree/postorder_tree_traversal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <bits/stdc++.h>
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
58 changes: 58 additions & 0 deletions C++/Data-Structures/04-tree/preorder_tree_traversal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <bits/stdc++.h>
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

0 comments on commit 13d6352

Please sign in to comment.