0% found this document useful (0 votes)
205 views11 pages

Assignment 5 Dsa

Uploaded by

nishcheybali31
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)
205 views11 pages

Assignment 5 Dsa

Uploaded by

nishcheybali31
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/ 11

ASSIGNMENT-5: NONLINEAR DATA STRUCTURE

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.

Ques 2: What are the applications of trees?

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:

Inorder Traversal: DGBAHEICF


Postorder Traversal: GDBHIEFCA

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.

Final tree structure:

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

Ques 4: Preorder and Inorder traversal of the given tree:


Answer:

Binary Tree:

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

Traversal Results:

1. Preorder Traversal (Root -> Left -> Right):

F, B, A, D, C, E, G, I, H

2. Inorder Traversal (Left -> Root -> Right):

A, B, C, D, E, F, G, H, I

Ques 5: Construct Binary Search Tree for the following data:

Answer:

Data: 10, 3, 15, 22, 6, 45, 65, 23, 78, 34, 5

Constructing the Binary Search Tree:

1. Insert 10 (root node)


2. Insert 3 (left of 10)
3. Insert 15 (right of 10)
4. Insert 22 (right of 15)
5. Insert 6 (left of 10, right of 3)
6. Insert 45 (right of 22)
7. Insert 65 (right of 45)
8. Insert 23 (left of 45, right of 22)
9. Insert 78 (right of 65)
10. Insert 34 (left of 45, right of 23)
11. Insert 5 (right of 3, left of 6)

Final Binary Search Tree:

10
/ \
3 15
\ \
6 22
\ \
5 45
/ \
23 65
/ / \
34 78

 Inorder: 3, 5, 6, 10, 15, 22, 23, 34, 45, 65, 78


 Preorder: 10, 3, 6, 5, 15, 22, 45, 23, 34, 65, 78
 Postorder: 5, 6, 3, 34, 23, 78, 65, 45, 22, 15, 10

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

AVL Tree Construction:

1. Insert 32 as the root.


2. Insert 16 (left of 32).
3. Insert 44 (right of 32).
4. Insert 52 (right of 44).
5. Insert 78 (right of 52), causing an imbalance.
6. Perform a left rotation on 44.
7. Continue inserting the remaining elements in a balanced way, using rotations when necessary.

Final AVL Tree:

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:

1. Efficient Tree Traversal:


Threaded binary trees eliminate the need for recursion or auxiliary data structures like stacks during
tree traversal (especially in in-order traversal). This makes traversal more efficient and reduces
overhead.
2. Space Efficiency:
Traditional tree traversal methods require extra space for recursion (function call stack) or stack-
based traversal. In threaded binary trees, the null pointers are reused as threads, reducing memory
consumption.
3. Faster In-order Traversal:
By using threads to link nodes in the in-order sequence, threaded binary trees allow for quicker
traversal since there is no need to backtrack or search for the next node in the traversal sequence. The
threads directly link to the next node, making traversal faster.
4. Avoiding Stack Overflow:
Since threaded binary trees do not rely on recursion or explicit stack space, they avoid the problem of
stack overflow which can occur in deeply nested or very large trees when using traditional
recursion-based methods.

Original Binary Tree (Fig. 1):

A
/ \
B E
/\ /\
C DF H
\
G
Right-Threaded Binary Tree:

1. For each node:


o If the right child is null, a thread is created pointing to the in-order successor of that node.
2. Inorder Traversal Sequence of the given tree:

C→B→D→A→F→G→E→H

Steps to Draw the Right-Threaded Binary Tree:

 For node C: Right thread → B


 For node B: Right thread → D
 For node D: Right thread → A
 For node F: Right thread → G
 For node G: Right thread → E
 For node E: Right thread → H
 Node H has no right thread as it is the last node.

Diagram of the Right-Threaded Binary Tree:

/ \

B E

/\ /\

C DF H

\ \ \

B A E

Ques 9: Write an Algorithm to Perform Traversal of Binary Search Tree (BST)

Answer:

Preorder Traversal Algorithm

In preorder traversal, the order is:

1. Visit the root node.


2. Traverse the left subtree.
3. Traverse the right subtree.

void preorderTraversal(struct Node* root) {


if (root != NULL) {
printf("%d ", root->data); // Visit the root node
preorderTraversal(root->left); // Traverse left subtree
preorderTraversal(root->right); // Traverse right subtree
}
}

Inorder Traversal Algorithm

In inorder traversal, the order is:

1. Traverse the left subtree.


2. Visit the root node.
3. Traverse the right subtree.

void inorderTraversal(struct Node* root) {


if (root != NULL) {
inorderTraversal(root->left); // Traverse left subtree
printf("%d ", root->data); // Visit the root node
inorderTraversal(root->right); // Traverse right subtree
}
}

Postorder Traversal Algorithm

In postorder traversal, the order is:

1. Traverse the left subtree.


2. Traverse the right subtree.
3. Visit the root node.

void postorderTraversal(struct Node* root) {


if (root != NULL) {
postorderTraversal(root->left); // Traverse left subtree
postorderTraversal(root->right); // Traverse right subtree
printf("%d ", root->data); // Visit the root node
}
}

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.

The AVL tree maintains the following balance conditions:

 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.

Step-by-Step Insertion and Rotations:

1. Insert 150

 Tree is empty, so 150 becomes the root node.


 No rotation needed.

150

2. Insert 155

 155 is greater than 150, so it becomes the right child of 150.


 No rotation needed because the tree is still balanced.

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.

After Left Rotation:

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.

After Left-Right Rotation:


markdown
Copy code
155
/ \
130 160
/ \
115 150
/ \ \
110 120 145

10. Insert 147

 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

11. Insert 170

 170 is greater than 160, so it becomes the right child of 160.


 No rotation needed because the tree remains balanced.

155
/ \
130 160
/ \ \
115 150 170
/ \ \
110 120 145
\
147

12. Insert 180

 180 is greater than 170, so it becomes the right child of 170.


 The balance factor of 160 becomes -2 (left height = 0, right height = 2), so the tree is unbalanced.
 Right-Right Case: Perform a left rotation at node 160.

After Left Rotation:

155
/ \
130 170
/ \ / \
115 150 160 180
/ \ \
110 120 145
\
147

Final AVL Tree:

155
/ \
130 170
/ \ / \
115 150 160 180
/ \ \
110 120 145
\
147

You might also like