Paper-2
Paper-2
i) Root ii) Subtree iii) Level of node iv) Depth of Tree v) Siblings
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.
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
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.
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.
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:
/\
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
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.
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:-
5
/\
3 7
/\ \
2 4 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
The last element in the postorder list is A, so A is the root of the tree.
B
/ \
D E
/\
F G
The right subtree of A now looks like this:
C
/
H
\
I
A
/ \
B C
/ \ /
D E H
/\ \
F G I