0% found this document useful (0 votes)
4 views5 pages

Paper-2

Ui

Uploaded by

amitkamble2907
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)
4 views5 pages

Paper-2

Ui

Uploaded by

amitkamble2907
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/ 5

20/06/2023

Q5) a) Define the following terms with respect to Trees: [5]

i) Root ii) Subtree iii) Level of node iv) Depth of Tree v) Siblings

Ans:- i) Root:- Repeat

ii) Subtree:- A subtree of a tree is a smaller portion of the main tree that consists of a node and all its
descendants. In other words, if you take any node in a tree, that node and everything below it
(connected by its child nodes) form a subtree. Each subtree is itself a tree structure and follows the
hierarchical properties of the main tree.

iii) Level of a Node:- The level of a node in a tree represents its depth or distance from the root node. The
root node is at level 0, and each level down increases by one. For example, the children of the root node
are at level 1, their children are at level 2, and so on. The level of a node helps determine its position
within the tree's hierarchy.
iv) Siblings:- Siblings are nodes that share the same parent in a tree. They are at the same level and belong
to the same branch, meaning they are connected directly under the same parent node. Siblings help
define family relationships within the tree, making it easier to understand tree structures and
relationships.

examples for each of the terms:

Subtree:- Consider the following binary tree:

A
/\
B C
/\
D E

In this tree, node B and all its descendants (D and E) form a subtree. This subtree rooted at B is:

B
/\
D E

Here, B, D, and E together are a subtree of the main tree.

1. Level of a Node:
Using the same tree:

A
/\
B C
/\
D E

o A is at level 0 (root).
o B and C are at level 1 (children of the root).
o D and E are at level 2 (children of B).

The level of each node indicates its position relative to the root.

2. Siblings:
In the tree above:
o B and C are siblings because they share the same parent, A.
o D and E are also siblings because they both have B as their parent.

Siblings are nodes with the same immediate parent.

v) Depth of Tree: - Repeat.

b) Write a recursive ‘C’ function for inorder, preorder, postorder tree traversal? [6]

Ans:- Repeat

c) Construct the Binary Search Tree (BST) from the following data: [6]

5, 2, 8, 4, 1, 9, 7

Also show preorder, postorder and inorder traversal for the same

Ans:- To construct a Binary Search Tree (BST) from the given data 5, 2, 8, 4, 1, 9, 7, insert each number in the
order provided, following the rules of a BST (left child is less than the parent, and right child is greater than the
parent).

Step-by-Step Construction

1. Insert 5:
o 5 is the first element, so it becomes the root of the tree.
2. Insert 2:
o 2 is less than 5, so it becomes the left child of 5.
3. Insert 8:
o 8 is greater than 5, so it becomes the right child of 5.
4. Insert 4:
o 4 is less than 5 (go to the left of 5).
o 4 is greater than 2, so it becomes the right child of 2.
5. Insert 1:
o 1 is less than 5 (go to the left of 5).
o 1 is less than 2, so it becomes the left child of 2.
6. Insert 9:
o 9 is greater than 5 (go to the right of 5).
o 9 is greater than 8, so it becomes the right child of 8.
7. Insert 7:
o 7 is greater than 5 (go to the right of 5).
o 7 is less than 8, so it becomes the left child of 8.

The resulting BST structure is as follows:


5
/\
2 8
/\/\
1 47 9

Traversals of the BST

1. Inorder Traversal (Left, Root, Right):


o Inorder: 1, 2, 4, 5, 7, 8, 9
2. Preorder Traversal (Root, Left, Right):
o Preorder: 5, 2, 1, 4, 8, 7, 9
3. Postorder Traversal (Left, Right, Root):
o Postorder: 1, 4, 2, 7, 9, 8, 5

Q6) a) Define a tree. Explain with a suitable example how a binary tree can be represented using an array.
[5]

Ans: - A tree is a hierarchical data structure consisting of nodes, where each node has a value and links to child
nodes. The topmost node is called the root, and each link represents a parent-child relationship. Trees are used to
model hierarchical data, such as file systems or organizational structures.

A binary tree can be represented using an array by storing the tree nodes in a way that reflects the structure of
the tree. This method is commonly used for complete binary trees but can also be applied to any binary tree.

Representation Rules:

1. The root of the tree is stored at index 1 (or 0 if using 0-based indexing).
2. For a node at index i:
o The left child is stored at index 2 * i.
o The right child is stored at index 2 * i + 1.
3. If a node has no children, the corresponding positions in the array are left
Example:

Consider the following binary tree:

/\

B C

/\ \

D E F

We can represent this binary tree using an array. Let’s assign indices based on the tree structure:
Index: 1 2 3 4 5 6 7

Value: A B C D E - F

Final Array Representation (0-based index):

Index: 0 1 2 3 4 5 6

Value: A B C D E - F

b) Write an algorithm to implement non-recursive in-order traversal of a binary search tree [6]

Ans:- Algorithm to perform a non-recursive in-order traversal of a binary search tree (BST) using a stack.

Algorithm: Non-Recursive In-Order Traversal

1. Initialize an empty stack.


2. Set current to the root node of the BST.
3. Repeat the following steps until both current is NULL and the stack is empty:
o While current is not NULL:
 Push current onto the stack.
 Move current to its left child.
o Pop the top node from the stack, set it as current, and visit it (print or process the node’s value).
o Move current to its right child.
4. End of the traversal.

 This algorithm uses a stack to track nodes as it navigates to the leftmost node in the subtree.
 After reaching the leftmost node, nodes are visited from the stack, and the traversal proceeds to the right
child.

For Example:-

For a BST with the structure:

5
/\
3 7
/\ \
2 4 8

The in-order traversal visits nodes in the order: 2, 3, 4, 5, 7, 8

c) The postorder and inorder traversals of a binary tree are given below. Is it possible to obtain a unique binary tree
from these traversals? If yes, obtain the tree, if not give justification. [6]

Inorder Traversal: D B F E G A H I C

Postorder Traversal: D F G E B I H C A

Ans:- Yes, it possible to obtain a unique binary tree from these traversals.
Given Traversals:

 Inorder: D B F E G A H I C
 Postorder: D F G E B I H C A

Step-by-Step Tree Construction

Step 1: Find the Root

 The last element in the postorder list is A, so A is the root of the tree.

Step 2: Split Inorder for Left and Right Subtrees

 In the inorder list, find A: D B F E G | A | H I C


o The left side (D B F E G) is the left subtree.
o The right side (H I C) is the right subtree.

So, the left subtree of A looks like this:

B
/ \
D E
/\
F G
The right subtree of A now looks like this:
C
/
H
\
I

Final Tree Structure

Combining everything, the tree looks like this:

A
/ \
B C
/ \ /
D E H
/\ \
F G I

You might also like