Assignment 5 Dsa
Assignment 5 Dsa
TREE
Ques 1: Discuss the following with reference to trees:
Answer:
Tree: A tree is a non-linear hierarchical data structure consisting of nodes connected by edges. It has
a root node, from which all other nodes branch out, where each node has one parent, and may have
multiple child nodes.
Node: A node is a fundamental unit of a tree, containing data and pointers to its child nodes.
Parent Node: A parent node is a node that has one or more child nodes. It is linked to the child
nodes by edges.
Child Node: A child node is a node that has a parent and is connected by an edge from the parent
node.
Link: A link is the reference or pointer from a parent node to a child node, representing the
relationship between the nodes.
Root: The root is the topmost node in a tree and does not have any parent. All other nodes in the tree
are descendants of the root.
Leaf: A leaf node is a node that has no children. It is the terminal node in a tree.
Level: The level of a node is the number of edges from the root node to the given node. The root is at
level 0.
Height: The height of a tree is the length of the longest path from the root to any leaf node. It is also
the level of the deepest node in the tree.
Degree: The degree of a node is the number of children it has. It is the number of direct descendants
of a node.
Binary Tree: A binary tree is a tree in which each node has at most two children, referred to as the
left child and the right child.
Complete Binary Tree: A complete binary tree is a binary tree in which all levels, except possibly
the last, are completely filled, and all nodes are as far left as possible.
Strictly Binary Tree: A strictly binary tree is a binary tree in which every node has either two
children or no children.
Threaded Binary Tree: A threaded binary tree is a binary tree where the null pointers are replaced
by pointers to the next node in the in-order traversal, allowing faster traversal.
Forest: A forest is a collection of disjoint trees. It can be formed by deleting the root of a tree,
resulting in multiple smaller trees.
Sibling: Siblings are nodes that share the same parent in a tree.
Binary Search Tree (BST): A binary search tree is a binary tree where for every node, the left
subtree contains only nodes with keys less than the node’s key, and the right subtree contains only
nodes with keys greater than the node’s key.
Height Balanced Tree (AVL Tree): An AVL tree is a self-balancing binary search tree where the
difference between the heights of the left and right subtrees of every node is at most 1.
Almost Complete Binary Tree: An almost complete binary tree is a binary tree where all levels are
completely filled, except possibly the last level, which is filled from left to right.
Full Binary Tree: A full binary tree is a binary tree in which every node has either 0 or 2 children.
Answer:
Hierarchical Data Representation: Trees are used to represent hierarchical data such as file
systems, organization structures, and XML/HTML documents.
Search Operations: Binary Search Trees (BSTs) are used for efficient searching, insertion, and
deletion operations in databases.
Network Routing: Trees are used in network routing algorithms like spanning trees for optimal
routing in networks.
Expression Parsing: Trees are used in compilers to parse and evaluate expressions, especially in
abstract syntax trees.
Game Trees: In game theory, trees represent possible moves in games like chess or tic-tac-toe,
where each node represents a game state.
Decision Making: Trees are used in decision-making algorithms (decision trees) for classification
and regression problems in machine learning.
Ques 3: Construct a tree for the given inorder and postorder traversals:
Answer:
Step-by-step construction:
1. The root node is the last node of the postorder traversal, which is A.
2. Split the inorder traversal at A:
Left subtree: DGB
Right subtree: HEICF
3. The next root node from the postorder traversal is C, which is the last element of the right subtree in
postorder.
4. Split the right subtree of the inorder traversal at C:
Left subtree: HEI
Right subtree: F
5. Repeat the same process for the subtrees:
o For the left subtree of C, the root is E.
o For the right subtree of C, the root is F.
6. Similarly, for the left subtree of A, the root is B. Split DGBA into left (D) and right (GB), and then
process recursively.
A
/ \
B C
/\ / \
D G E F
/ \
H I
Binary Tree:
F
/ \
B G
/\ \
A D I
/\ /
C E H
Traversal Results:
F, B, A, D, C, E, G, I, H
A, B, C, D, E, F, G, H, I
Answer:
10
/ \
3 15
\ \
6 22
\ \
5 45
/ \
23 65
/ / \
34 78
Ques 6: Define height of the binary tree and height balanced tree:
Answer:
Height of a Binary Tree: The height of a binary tree is the number of edges on the longest path
from the root to a leaf node.
Height Balanced Tree: A height-balanced tree (AVL tree) is a binary search tree where the
difference in heights of the left and right subtrees of every node is at most 1.
Advantages:
Improved search, insertion, and deletion efficiency as the tree remains balanced, ensuring
logarithmic time complexity (O(log n)).
Ques 7: Construct a height balanced binary tree (AVL tree) for the following data:
Answer:
Data: 32, 16, 44, 52, 78, 40, 12, 22, 02, 23
44
/ \
32 52
/ \ / \
16 40 23 78
/ /
2 22
Ques 8: Why is a Threaded Binary Tree required? Draw a right in threaded binary tree for the given
fig.1.
Answer:
Definition:
A Threaded Binary Tree is a binary tree in which the null pointers (typically in the left and/or right child
of leaf nodes) are replaced with "threads." These threads point to the next node in a specific traversal order,
typically the in-order traversal. The threads allow for efficient traversal without the need for recursion or
stack-based mechanisms.
In a single-threaded binary tree, the null right child pointers are replaced with threads that point to
the in-order successor of the node.
In a double-threaded binary tree, both left and right null child pointers are replaced with threads
that point to the in-order predecessor (left thread) and in-order successor (right thread).
Why It Is Required:
A
/ \
B E
/\ /\
C DF H
\
G
Right-Threaded Binary Tree:
C→B→D→A→F→G→E→H
/ \
B E
/\ /\
C DF H
\ \ \
B A E
Answer:
Ques 10: Define an AVL tree. Obtain an AVL tree by inserting one integer at a time in the following
sequence. 150, 155, 160, 115, 110, 140, 120, 145, 130, 147, 170, 180. Show all the steps
Answer:
An AVL Tree (Adelson-Velsky and Landis Tree) is a self-balancing Binary Search Tree (BST) where the
difference in height between the left and right subtrees of any node is at most 1. This balance is maintained
through rotations. If the tree becomes unbalanced after an insertion, rotations are performed to restore
balance.
Balance Factor: The difference in height between the left and right subtrees of a node. If the balance
factor of a node becomes greater than 1 or less than -1, the tree is unbalanced, and rotations are
performed to restore balance.
Rotations: To restore balance, left rotations, right rotations, and combinations of both (left-right
and right-left) are used.
Insertion Sequence:
We will insert the integers in the following sequence: 150, 155, 160, 115, 110, 140, 120, 145, 130, 147, 170,
180.
1. Insert 150
150
2. Insert 155
150
\
155
3. Insert 160
160 is greater than both 150 and 155, so it becomes the right child of 155.
The balance factor of 150 becomes -2 (left height = 0, right height = 2), so the tree is unbalanced.
Right-Right Case: Perform a left rotation at node 150.
155
/ \
150 160
4. Insert 115
115 is less than 155 and less than 150, so it becomes the left child of 150.
No rotation needed because the tree remains balanced.
155
/ \
150 160
/
115
5. Insert 110
110 is less than 115 and 150, so it becomes the left child of 115.
No rotation needed because the tree remains balanced.
155
/ \
150 160
/
115
/
110
6. Insert 140
140 is less than 155 but greater than 115, so it becomes the right child of 115.
No rotation needed because the tree remains balanced.
155
/ \
150 160
/
115
/ \
110 140
7. Insert 120
120 is greater than 115 but less than 140, so it becomes the left child of 140.
No rotation needed because the tree remains balanced.
155
/ \
150 160
/
115
/ \
110 140
/
120
8. Insert 145
145 is greater than 140 but less than 150, so it becomes the right child of 140.
No rotation needed because the tree remains balanced.
155
/ \
150 160
/
115
/ \
110 140
/ \
120 145
9. Insert 130
130 is greater than 115 but less than 140, so it becomes the left child of 140.
The balance factor of 150 becomes 2, indicating an imbalance (left-heavy).
Left-Right Case: Perform a left rotation on 140, followed by a right rotation on 150.
147 is greater than 145 but less than 150, so it becomes the right child of 145.
No rotation needed because the tree remains balanced.
155
/ \
130 160
/ \
115 150
/ \ \
110 120 145
\
147
155
/ \
130 160
/ \ \
115 150 170
/ \ \
110 120 145
\
147
155
/ \
130 170
/ \ / \
115 150 160 180
/ \ \
110 120 145
\
147
155
/ \
130 170
/ \ / \
115 150 160 180
/ \ \
110 120 145
\
147