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:
1. There is one root node (topmost node) in the tree.
2. Each node has zero or more child nodes.
3. Nodes are connected by edges, and there are no cycles in a tree structure.
4. Trees are widely used in applications like file systems, hierarchical databases, and AI algorithms
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).
Depth-First Traversal (DFT)
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.
Breadth-First Traversal (BFT)
1. Level Order Traversal: Visit all nodes level by level from top to bottom.
Algorithms
1) Algorithm Preorder Traversal (NLR):
void preorder(Node* root) {
if (root != NULL) {
printf("%d ", root->data); // Visit root
preorder(root->left); // Traverse left subtree
preorder(root->right); // Traverse right subtree
}}
2) Algorithm for Inorder Traversal (LNR):
void inorder(Node* root) {
if (root != NULL) {
inorder(root->left); // Traverse left subtree
printf("%d ", root->data); // Visit root
inorder(root->right); // Traverse right subtree
}}
3) Algorithm for Postorder Traversal (LRN):
void postorder(Node* root) {
if (root != NULL) {
postorder(root->left); // Traverse left subtree
postorder(root->right); // Traverse right subtree
printf("%d ", root->data); // Visit root
}}
4) Algorithm for Level Order Traversal:
void levelOrder(Node* root) {
if (root == NULL) return;
Queue* queue = createQueue(); // Use a queue for BFS
enqueue(queue, root);
while (!isEmpty(queue)) {
Node* current = dequeue(queue);
printf("%d ", current->data);
if (current->left != NULL) enqueue(queue, current->left);
if (current->right != NULL) enqueue(queue, current->right);
}}
Example
Consider the following tree:
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.
Binary Tree Node Structure
In a binary tree, each node consists of:
1. Data: The value or information stored in the node.
2. Left Child: A pointer/reference to the left child node.
3. Right Child: A pointer/reference to the right child node.
Binary Tree Node Structure in C
typedef struct Node {
int data; // Data part of the node
struct Node* left; // Pointer to the left child
struct Node* right; // Pointer to the right child
} Node;
3. What are the different ways of representing a binary tree?
Explain the Linked representation.
1)Array Representation
• The binary tree is stored in a 1D array where:
o The root node is at index 1 (or 0 in zero-based indexing).
o The left child of a node at index i is at 2*i (or 2*i + 1 in zero-based indexing).
o The right child of a node at index i is at 2*i + 1 (or 2*i + 2 in zero-based indexing).
o The parent of a node at index i is at floor(i/2) (or floor((i-1)/2) in zero-based indexing).
Example: Consider the binary tree
1
/ \
2 3
/ \
4 5
Its array representation (1-based indexing): [1, 2, 3, 4, 5].
2)Linked Representation
• Each node is represented as a structure containing:
o Data field.
o Pointer to the left child.
o Pointer to the right child.
typedef struct Node {
int data; // Data stored in the node
struct Node* left; // Pointer to the left child
struct Node* right; // Pointer to the right child
} Node;
4. Explain the terms 'node', 'root', 'parent', 'child', 'leaf', and 'depth'
in the context of trees.
Terms in Trees
• Node: A basic unit of a tree containing data.
• Root: The topmost node in the tree.
• Parent: A node with child nodes.
• Child: A node that has a parent node.
• Leaf: A node without children.
• Depth: The number of edges from the root to the node.
5. Describe the preorder, inorder, and postorder traversal techniques for binary trees. Provide an example tree
and demonstrate each traversal method.
1. Preorder Traversal (NLR)
• Definition: Visit the root node first, then recursively traverse the left subtree, followed by the right subtree.
• Order of Traversal: Node → Left → Right
Algorithm:
1. Visit the root node.
2. Traverse the left subtree in preorder.
3. Traverse the right subtree in preorder.
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
2. Inorder Traversal (LNR)
• Definition: Recursively traverse the left subtree first, visit the root node, and finally traverse the right
subtree.
• Order of Traversal: Left → Node → Right
Algorithm:
1. Traverse the left subtree in inorder.
2. Visit the root node.
3. Traverse the right subtree in inorder.
Example Tree
1
/ \
2 3
/ \
4 5
Steps:
1. Traverse left subtree: 4, 2, 5.
2. Visit 1 (root).
3. Traverse right subtree: 3.
Output: 4, 2, 5, 1, 3
3. Postorder Traversal (LRN)
• 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:
1. Traverse the left subtree in postorder.
2. Traverse the right subtree in postorder.
3. Visit the root node.
Example Tree
1
/ \
2 3
/ \
4 5
Steps:
1. Traverse left subtree: 4, 5, 2.
2. Traverse right subtree: 3.
3. Visit 1 (root).
Output: 4, 5, 2, 3, 1
6. Define traversal . what are the different types of traversals that
can be performed on a tree?
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.
Types of Traversals in a Tree
1. Depth-First Traversal (DFT)
2. Breadth-First Traversal (BFT)
1. Depth-First Traversal (DFT)
• 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.
Example of DFT Traversal
For the tree:
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
2. Breadth-First Traversal (BFT)
• In breadth-first traversal, nodes are visited level by level from top to bottom.
• This is also known as Level Order Traversal.
Algorithm:
1. Start at the root.
2. Visit all nodes at the current level before moving to the next level.
Example of BFT Traversal
For the tree:
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
2. Insert 10: 10 > 8, goes to the right of 8.
8
\
10
3. Insert 3: 3 < 8, goes to the left of 8.
8
/ \
3 10
4. Insert 6: 6 > 3, goes to the right of 3.
8
/ \
3 10
\
6
5. Insert 14: 14 > 8 and 14 > 10, goes to the right of 10.
8
/ \
3 10
\ \
6 14
6. Insert 1: 1 < 8 and 1 < 3, goes to the left of 3.
8
/ \
3 10
/ \ \
1 6 14
7. Insert 7: 7 < 8, 7 > 3, 7 > 6, goes to the right of 6.
8
/ \
3 10
/ \ \
1 6 14
\
7
8. Insert 4: 4 < 8, 4 > 3, 4 < 6, goes to the left of 6.
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
1. Preorder Traversal (NLR):
Visit root → Traverse left subtree → Traverse right subtree.
Order: 8, 3, 1, 6, 4, 7, 10, 14, 13
2. Inorder Traversal (LNR):
Traverse left subtree → Visit root → Traverse right subtree.
Order: 1, 3, 4, 6, 7, 8, 10, 13, 14
3. Postorder Traversal (LRN):
Traverse left subtree → Traverse right subtree → Visit root.
Order: 1, 4, 7, 6, 3, 13, 14, 10, 8
8. Write a C function to insert an element into a BST. (5 marks)
Node* insert(Node* root, int data) {
if (root == NULL) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
if (data < root->data) root->left = insert(root->left, data);
else root->right = insert(root->right, data);
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
deleting an element from BST. (10 marks)
#include <stdio.h>
#include <stdlib.h>
// Define the structure for a node in BST
typedef struct Node {
int data;
struct Node* left;
struct Node* right;
} Node;
// Function to delete a node from BST
Node* deleteNode(Node* root, int data) {
if (root == NULL) return root; // Tree is empty
// Traverse to find the node to delete
if (data < root->data) {
root->left = deleteNode(root->left, data);
} else if (data > root->data) {
root->right = deleteNode(root->right, data);
} else {
// Node to be deleted found
// Case 1: Node has no children (leaf node)
if (root->left == NULL && root->right == NULL) {
free(root);
return NULL;
// Case 2: Node has one child
if (root->left == NULL) {
Node* temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
Node* temp = root->left;
free(root);
return temp;
// Case 3: Node has two children
Node* temp = findMin(root->right); // Find inorder successor
root->data = temp->data; // Replace with successor's value
root->right = deleteNode(root->right, temp->data); // Delete successor
return root;
// Main function to demonstrate deletion
int main() {
Node* root = NULL;
// Delete a node
printf("Deleting 1 (leaf node)...\n");
root = deleteNode(root, 1);
printf("Inorder Traversal after deletion: ");
inorder(root);
printf("\n");
printf("Deleting 6 (node with one child)...\n");
root = deleteNode(root, 6);
printf("Inorder Traversal after deletion: ");
inorder(root);
printf("\n");
printf("Deleting 8 (node with two children)...\n");
root = deleteNode(root, 8);
printf("Inorder Traversal after deletion: ");
inorder(root);
printf("\n");
return 0;
}
Three Cases of Deletion
1. Case 1: The node to be deleted is a leaf node (no children)
o Simply remove the node from the tree.
2. Case 2: The node to be deleted has one child (left or right)
o Replace the node with its child.
3. Case 3: The node to be deleted has two children
o Find the inorder successor (smallest node in the right subtree) or the inorder predecessor (largest
node in the left subtree).
o Replace the node's value with the inorder successor or predecessor, and recursively delete the
successor/predecessor.
10. Write C functions to perform any two tree traversals on a Binary Search Tree. (5 marks)
Refer 1st question
11. Define Complete Binary Tree with a example (2 marks)
A Complete Binary Tree is a binary tree in which:
1. All levels except possibly the last are completely filled.
2. All nodes in the last level are as far left as possible
Example
Consider the following tree:
1
/ \
2 3
/ \ /
4 5 6
• This is a Complete Binary Tree because:
1. All levels except the last are completely filled.
2. Nodes in the last level (4, 5, 6) are as far left as possible.
12. Define Almost Complete Binary Tree with a example (2marks)
Almost Complete Binary Tree
An Almost Complete Binary Tree is a binary tree in which:
1. Every node has either 0, 1, or 2 children.
2. All leaf nodes are at the same level or one level above the other leaf nodes.
3. The nodes are filled from the leftmost position, maintaining the shape as close to a Complete Binary Tree as
possible.
Example: Consider the following tree:
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.
13. Define Balanced Binary Tree with a example (2 marks)
Balanced Binary Tree
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
Consider the following tree:
1
/ \
2 3
/ \
4 5
• This is a Balanced Binary Tree because:
1. The height of the left subtree (rooted at 2) is 2.
2. The height of the right subtree (rooted at 3) is 1.
3. The height difference for the root node 1 is |2 - 1| = 1, which is ≤ 1.
14. Define Strictly Binary Tree with a example (2 marks)
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
Consider the following tree:
1
/ \
2 3
/ \
4 5
• This is a Strictly Binary Tree because:
1. Every non-leaf node (1, 2) has exactly two children.
2. The leaf nodes are 3, 4, and 5.
15. Define the following terms (2 marks)
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.
For the following tree:
A
/ \
B C
/ \
D E
• Descendants of BBB: D,ED, ED,E.
• Ancestors of EEE: B,AB, AB,A