Trees
Trees
1. Define Tree. Explain the tree traversals with algorithms and examples.
A tree is a hierarchical data structure composed of nodes, where each node stores data and can have child nodes. A
tree is defined by the following properties:
Tree Traversals
Tree traversal refers to the process of visiting all the nodes in a tree in a specific order. Traversals are mainly divided
into Depth-First Traversals (DFT) and Breadth-First Traversals (BFT).
1. Preorder Traversal (NLR): Visit the root, traverse the left subtree, then traverse the right subtree.
2. Inorder Traversal (LNR): Traverse the left subtree, visit the root, then traverse the right subtree.
3. Postorder Traversal (LRN): Traverse the left subtree, traverse the right subtree, then visit the root.
1. Level Order Traversal: Visit all nodes level by level from top to bottom.
Algorithms
if (root != NULL) {
}}
if (root != NULL) {
}}
3) Algorithm for Postorder Traversal (LRN):
if (root != NULL) {
}}
enqueue(queue, root);
while (!isEmpty(queue)) {
}}
Example
1
/ \
2 3
/ \
4 5
Traversals:
1. Preorder (NLR): 1, 2, 4, 5, 3
2. Inorder (LNR): 4, 2, 5, 1, 3
3. Postorder (LRN): 4, 5, 2, 3, 1
4. Level Order: 1, 2, 3, 4, 5
2. Define binary tree and give the binary tree node structure.
A binary tree is a hierarchical data structure in which each node has at most two children, referred to as the left
child and the right child. Binary trees are widely used in searching, sorting algorithms, and in representing
hierarchical data.
} Node;
1)Array Representation
2)Linked Representation
} Node;
4. Explain the terms 'node', 'root', 'parent', 'child', 'leaf', and 'depth'
Terms in Trees
5. Describe the preorder, inorder, and postorder traversal techniques for binary trees. Provide an example tree
and demonstrate each traversal method.
• Definition: Visit the root node first, then recursively traverse the left subtree, followed by the right subtree.
• Order of Traversal: Node → Left → Right
Algorithm:
Example Tree
1
/ \
2 3
/ \
4 5
Steps:
1. Visit 1 (root).
2. Traverse left subtree: 2 → 4, 5.
3. Traverse right subtree: 3.
Output: 1, 2, 4, 5, 3
• Definition: Recursively traverse the left subtree first, visit the root node, and finally traverse the right
subtree.
• Order of Traversal: Left → Node → Right
Algorithm:
Example Tree
1
/ \
2 3
/ \
4 5
Steps:
Output: 4, 2, 5, 1, 3
• Definition: Recursively traverse the left subtree first, then traverse the right subtree, and finally visit the
root node.
• Order of Traversal: Left → Right → Node
Algorithm:
Example Tree
1
/ \
2 3
/ \
4 5
Steps:
Output: 4, 5, 2, 3, 1
6. Define traversal . what are the different types of traversals that
Traversal is the process of visiting each node in a tree data structure exactly once in a systematic way to perform
some operation (e.g., printing node data, searching for a node). It ensures that all nodes are processed in a structured
manner.
• In depth-first traversal, nodes are visited by exploring as far down a branch as possible before backtracking.
• This category includes the following:
o Preorder Traversal (NLR): Visit root → Traverse left subtree → Traverse right subtree.
o Inorder Traversal (LNR): Traverse left subtree → Visit root → Traverse right subtree.
o Postorder Traversal (LRN): Traverse left subtree → Traverse right subtree → Visit root.
1
/ \
2 3
/ \
4 5
• Preorder (NLR): 1, 2, 4, 5, 3
• Inorder (LNR): 4, 2, 5, 1, 3
• Postorder (LRN): 4, 5, 2, 3, 1
• In breadth-first traversal, nodes are visited level by level from top to bottom.
• This is also known as Level Order Traversal.
Algorithm:
1
/ \
2 3
/ \
4 5
Level Order: 1, 2, 3, 4, 5
7. Construct a BST from the following elements by repeatedly inserting them into the BST. 8 10 3 6 14 1 7 4 13.
Perform and display the three tree traversals on the above tree.(10 marks)
Step-by-Step Construction
1. Insert 8: Root = 8
8
\
10
8
/ \
3 10
8
/ \
3 10
\
6
5. Insert 14: 14 > 8 and 14 > 10, goes to the right of 10.
8
/ \
3 10
\ \
6 14
8
/ \
3 10
/ \ \
1 6 14
8
/ \
3 10
/ \ \
1 6 14
\
7
8
/ \
3 10
/ \ \
1 6 14
/ \
4 7
9. Insert 13: 13 > 8, 13 > 10, 13 < 14, goes to the left of 14.
8
/ \
3 10
/ \ \
1 6 14
/ \ /
4 7 13
Final BST
8
/ \
3 10
/ \ \
1 6 14
/ \ /
4 7 13
Traversal Results
if (root == NULL) {
newNode->data = data;
return newNode;
return root;
}
9. Write a C function to delete an element from the BST. Describe
with an example for each of the three cases that can arise while
#include <stdio.h>
#include <stdlib.h>
int data;
} Node;
} else {
free(root);
return NULL;
if (root->left == NULL) {
free(root);
return temp;
free(root);
return temp;
return root;
int main() {
// Delete a node
inorder(root);
printf("\n");
inorder(root);
printf("\n");
inorder(root);
printf("\n");
return 0;
}
Three Cases of Deletion
10. Write C functions to perform any two tree traversals on a Binary Search Tree. (5 marks)
Example
1
/ \
2 3
/ \ /
4 5 6
1
/ \
2 3
/ \
4 5
• This is an Almost Complete Binary Tree because:
1. The nodes are filled from the leftmost positions.
2. Leaf nodes (4 and 5) are at the same level.
A Balanced Binary Tree is a binary tree in which the height difference between the left and right subtrees of every
node is at most 1. This ensures the tree remains balanced, optimizing search, insertion, and deletion operations.
Example
1
/ \
2 3
/ \
4 5
A Strictly Binary Tree is a binary tree in which every non-leaf node has exactly two children. This means that each
internal node has either two children or is a leaf node.
Example
1
/ \
2 3
/ \
4 5
a) Descendant b)Ancestor
a) Descendant:
A node XXX is a descendant of another node YYY if XXX lies in the subtree rooted at YYY. This includes all nodes
reachable from YYY by following child pointers downward.
• Example: In a tree with root AAA, if BBB is a child of AAA and CCC is a child of BBB, then CCC is a descendant
of both BBB and AAA.
b) Ancestor:
A node XXX is an ancestor of another node YYY if YYY lies in the subtree rooted at XXX. This includes all nodes
reachable from YYY by following parent pointers upward.
• Example: In the same tree, AAA is an ancestor of both BBB and CCC.
A
/ \
B C
/ \
D E