-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from kumar007ambi/ambikaDSAMastery
Tree Related Problems
- Loading branch information
Showing
3 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |