0% found this document useful (0 votes)
5 views

Diler 1

0

Uploaded by

sanjeet003yadev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Diler 1

0

Uploaded by

sanjeet003yadev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 6 (a)
Student Name: Diler Singh Khaira UID: 22BCS14516
Branch: CSE Section/Group: 617/B
Semester: 5th Date of Performance:13/09/24
Subject Name: Advanced Programming Lab-1 Subject Code: 22CSP-314

1. Title: Binary Search tree insertion.

2. Aim: Insert the values into their appropriate position in the binary search tree.

3. Objective: You are given a pointer to the root of a binary search tree and
values to be inserted into the tree. Insert the values into their appropriate
position in the binary search tree and return the root of the updated binary
tree.

4. Algorithm:

STEP 1: Node Class: Defines a binary tree node with data, left, and right
pointers.
STEP 2: Insert Function: Recursively inserts a new node into the binary tree
based on its value.
STEP 3: PreOrder Function: Recursively prints the tree nodes in pre-order
traversal (root, left, right).
STEP 4: Main Function: Reads input values, inserts them into the tree, and
performs a pre-order traversal to print the nodes.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

5. Implementation/Code:
#include<bits/stdc++.h>
class Node {
public:
int data;
Node *left;
Node *right;
Node(int d) {
data = d;
left = NULL;
right = NULL;
}
};
class Solution {
public:
void preOrder(Node *root) {
if( root == NULL )
return;
std::cout << root->data << " ";
preOrder(root->left);
preOrder(root->right);
}
Node * insert(Node * root, int data) {
if (root == nullptr) {
return new Node(data);
}
if (data < root->data) {
root->left = insert(root->left, data);
} else {
root->right = insert(root->right, data);
}
return root;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

}
};
int main() {
Solution myTree;
Node* root = NULL;
int t;
int data;
std::cin >> t;
while(t-- > 0) {
std::cin >> data;
root = myTree.insert(root, data);
}
myTree.preOrder(root);
return 0;
}

6. Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

7. Time Complexity: O(n)

8. Space Complexity: O(n)

9. Learning Outcomes:
1) Understanding Binary Trees: Gain a solid grasp of binary tree
structures, including node creation and tree traversal methods.
2) Recursive Thinking: Develop skills in writing and understanding
recursive functions, which are essential for many tree operations.
3) Pre-order Traversal: Understand the concept and implementation of
pre-order traversal, which visits nodes in the order: root, left, right.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Experiment 6 (b)
Student Name: Ankit Koundal UID: 22BCS14481
Branch: BE-CSE Section/Group: 617/B
Semester: 5th Date of Performance:13/09/24
Subject Name: Advanced Programming Lab Subject Code: 22CSP-314

1. Title: Inorder Traversal.

2. Aim: Implement Inorder traversal on a binary tree and prints the node
values in the correct order.

3. Objective: To implement an Inorder traversal of a binary tree, which


involves visiting the nodes in the following order: left subtree, root node,
and right subtree.

4. Algorithm:
STEP 1: Node Class: Defines a binary tree node with data, left, and right
pointers.
STEP 2: Insert Function: Recursively inserts a new node into the binary
tree. If the tree is empty, it creates a new node. Otherwise, it inserts the node
in the left or right subtree based on the value.
STEP 3: InOrder Function: Recursively performs an inorder traversal of
the binary tree, which visits nodes in the order: left subtree, root node, right
subtree.
STEP 4: Main Function: Reads input values, inserts them into the tree, and
performs an inorder traversal to print the node values in a single line of
space-separated values.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

5. Implementation/Code:
#include <bits/stdc++.h>
using namespace std;

class Node {
public:
int data;
Node *left;
Node *right;
Node(int d) {
data = d;
left = NULL;
right = NULL;
}
};
class Solution {
public:
Node* insert(Node* root, int data) {
if(root == NULL) {
return new Node(data);
} else {
Node* cur;
if(data <= root->data) {
cur = insert(root->left, data);
root->left = cur;
} else {
cur = insert(root->right, data);
root->right = cur;
}
return root;
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

void inOrder(Node *root) {


if (root == nullptr)
return;
inOrder(root->left);
cout << root->data << " ";
inOrder(root->right);
}
};
int main() {
Solution myTree;
Node* root = NULL;
int t;
int data;
std::cin >> t;
while(t-- > 0) {
std::cin >> data;
root = myTree.insert(root, data);
}
myTree.inOrder(root);
return 0;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

6. Output:

7. Time Complexity: O(n)

8. Space Complexity: O(n)

9. Learning Outcomes:
1) Learn how to perform inorder traversal, which visits nodes in the
order: left subtree, root node, right subtree.
2) Enhance your ability to write and understand recursive functions,
which are essential for many tree operations.
3) Deepen your understanding of binary tree structures, including node
creation and tree traversal methods.

You might also like