0% found this document useful (0 votes)
21 views8 pages

A1 CS201 ODD2021 Aditya Mishra

The document provides instructions for an assignment submission. It lists the student name and details, assignment title and requirements. It provides two questions - the first question explains in-order, pre-order and post-order tree traversals with examples. The second question defines a binary search tree, provides a representation and explains search and insert operations with algorithms. It then inserts the given numbers in order into an empty binary search tree.

Uploaded by

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

A1 CS201 ODD2021 Aditya Mishra

The document provides instructions for an assignment submission. It lists the student name and details, assignment title and requirements. It provides two questions - the first question explains in-order, pre-order and post-order tree traversals with examples. The second question defines a binary search tree, provides a representation and explains search and insert operations with algorithms. It then inserts the given numbers in order into an empty binary search tree.

Uploaded by

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

Assignment

Name of Student: Aditya Mishra ROLL NO. - 180102114 SAP ID: 1000011196

Assignment ID Submission
(as per the Deadline
policy Assignment Submission Assessment Group/ Date of (Date and
guidelines) Title Mode Method Individual Weightage Release time)
16-11-
01 Unit-3 Online Online Individual 10 2021 21-11-2021

Instructions:

 Assignment must be submitted by the Due Date and Time as mentioned above.
 Assignment submitted after Due Date and Time and before the next 48 hours will be
marked late and will attract a penalty of 50 % marks. Assignment will not be considered
for evaluation subsequently (after 48 hours past due date and time), and a score of zero
will be awarded.
 Submitted assignment must have your Full Name and SAP ID in the space provided
above this page in the Header.

Submitting this Assignment

 Questions must be answered in the given order.


 Submit a pdf version of this document.
 Name this document as A1_CS201_ODD2021_Name.pdf

Problems:

1. What is in-order, pre-order and post-order traversal? Explain with example.

2. What do you mean by binary search tree? Explain with example. Insert following six
numbers in order in to an empty binary search tree.
40, 60, 50, 33, 55, 11
Assignment

Name of Student: Aditya Mishra ROLL NO. - 180102114 SAP ID: 1000011196

Solution -

Q1) Traversal is a process to visit all the nodes of a tree and may print their values too. Because,
all nodes are connected via edges (links) we always start from the root (head) node. That is, we
cannot randomly access a node in a tree. There are three ways which we use to traverse a tree −
 In-order Traversal
 Pre-order Traversal
 Post-order Traversal
Generally, we traverse a tree to search or locate a given item or key in the tree or to print all the
values it contains.

In-order Traversal
In this traversal method, the left subtree is visited first, then the root and later the right sub-tree.
We should always remember that every node may represent a subtree itself.
If a binary tree is traversed in-order, the output will produce sorted key values in an ascending
order.

We start from A, and following in-order traversal, we move to its left subtree B. B is also
traversed in-order. The process goes on until all the nodes are visited. The output of inorder
traversal of this tree will be −
D→B→E→A→F→C→G
Assignment

Name of Student: Aditya Mishra ROLL NO. - 180102114 SAP ID: 1000011196

Algorithm
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Visit root node.
Step 3 − Recursively traverse right subtree.

Pre-order Traversal
In this traversal method, the root node is visited first, then the left subtree and finally the right
subtree.

We start from A, and following pre-order traversal, we first visit A itself and then move to its left
subtree B. B is also traversed pre-order. The process goes on until all the nodes are visited. The
output of pre-order traversal of this tree will be −
A→B→D→E→C→F→G

Algorithm
Until all nodes are traversed −
Step 1 − Visit root node.
Step 2 − Recursively traverse left subtree.
Step 3 − Recursively traverse right subtree.
Assignment

Name of Student: Aditya Mishra ROLL NO. - 180102114 SAP ID: 1000011196

Post-order Traversal
In this traversal method, the root node is visited last, hence the name. First we traverse the left
subtree, then the right subtree and finally the root node.

We start from A, and following Post-order traversal, we first visit the left subtree B. B is also
traversed post-order. The process goes on until all the nodes are visited. The output of post-order
traversal of this tree will be −
D→E→B→F→G→C→A

Algorithm
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Recursively traverse right subtree.
Step 3 − Visit root node.

Q2) A Binary Search Tree (BST) is a tree in which all the nodes follow the below-mentioned
properties −
 The value of the key of the left sub-tree is less than the value of its parent (root) node's
key.
Assignment

Name of Student: Aditya Mishra ROLL NO. - 180102114 SAP ID: 1000011196

 The value of the key of the right sub-tree is greater than or equal to the value of its parent
(root) node's key.
Thus, BST divides all its sub-trees into two segments; the left sub-tree and the right sub-tree and
can be defined as −
left_subtree (keys) < node (key) ≤ right_subtree (keys)

Representation
BST is a collection of nodes arranged in a way where they maintain BST properties. Each node
has a key and an associated value. While searching, the desired key is compared to the keys in
BST and if found, the associated value is retrieved.
Following is a pictorial representation of BST −

We observe that the root node key (27) has all less-valued keys on the left sub-tree and the higher
valued keys on the right sub-tree.

Basic Operations
Following are the basic operations of a tree −
 Search − Searches an element in a tree.
 Insert − Inserts an element in a tree.
 Pre-order Traversal − Traverses a tree in a pre-order manner.
 In-order Traversal − Traverses a tree in an in-order manner.
 Post-order Traversal − Traverses a tree in a post-order manner.

Node
Define a node having some data, references to its left and right child nodes.
struct node {
int data;
struct node *leftChild;
struct node *rightChild;
};
Assignment

Name of Student: Aditya Mishra ROLL NO. - 180102114 SAP ID: 1000011196

Search Operation
Whenever an element is to be searched, start searching from the root node. Then if the data is less
than the key value, search for the element in the left subtree. Otherwise, search for the element in
the right subtree. Follow the same algorithm for each node.
Algorithm
struct node* search(int data){
struct node *current = root;
printf("Visiting elements: ");

while(current->data != data){

if(current != NULL) {
printf("%d ",current->data);

//go to left tree


if(current->data > data){
current = current->leftChild;
} //else go to right tree
else {
current = current->rightChild;
}

//not found
if(current == NULL){
return NULL;
}
}
}

return current;
}

Insert Operation
Assignment

Name of Student: Aditya Mishra ROLL NO. - 180102114 SAP ID: 1000011196

Whenever an element is to be inserted, first locate its proper location. Start searching from the
root node, then if the data is less than the key value, search for the empty location in the left
subtree and insert the data. Otherwise, search for the empty location in the right subtree and insert
the data.
Algorithm
void insert(int data) {
struct node *tempNode = (struct node*) malloc(sizeof(struct node));
struct node *current;
struct node *parent;

tempNode->data = data;
tempNode->leftChild = NULL;
tempNode->rightChild = NULL;

//if tree is empty


if(root == NULL) {
root = tempNode;
} else {
current = root;
parent = NULL;

while(1) {
parent = current;

//go to left of the tree


if(data < parent->data) {
current = current->leftChild;
//insert to the left

if(current == NULL) {
parent->leftChild = tempNode;
return;
}
} //go to right of the tree
else {
current = current->rightChild;

//insert to the right


if(current == NULL) {
parent->rightChild = tempNode;
Assignment

Name of Student: Aditya Mishra ROLL NO. - 180102114 SAP ID: 1000011196

return;
}
}
}
}
}

The given sequence of numbers, in order, can be inserted into a binary search tree. The binary
search tree can be represented in the figure given below –

You might also like