Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tree Traversal Question issue no #44 #50

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions DSA/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
58 changes: 58 additions & 0 deletions DSA/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