Final Exam Paper Solution
Final Exam Paper Solution
Instructions:
Read each question completely before answering it. There are 10 questions and 3 pages.
Students are not allowed to write anything on the question paper except roll number & section.
Submit the question paper along with the answer script.
Final Postorder Sequence: 15, 10, 23, 25, 20, 35, 42, 39, 30
2. You are given the following sequence of integers to insert into an AVL tree:
Sequence: 25, 27, 30, 7, 5, 20, 10, 22, 15, 35, 40
Construct the AVL tree by inserting the numbers while ensuring it remains balanced after each
insertion. Also, explain any rotations performed while balancing the AVL tree. (7 Marks)
Insert 25
Insert 27
1
Insert 30
RR Rotation
Insert 7
Insert 5
LL Rotation
Insert 20
LR Rotation
2
Insert 10
Insert 22
Insert 15
RL Rotation
3
Insert 35
RR Rotation
Insert 40
3. Consider the following BST. Explain, with reasoning, what element will replace if the node to be
deleted is the root node. (3 Marks)
In a Binary Search Tree (BST), when the root node is deleted, it must be replaced by either:
The inorder successor (smallest value in the right subtree). [8 will replace root node]
The inorder predecessor (largest value in the left subtree). [3 will replace root node]
4
4. Perform a dry run of the Quick Sort algorithm using the array: [8, 4, 7, 3, 10]. Choose a pivot
element as the last element of the array. (5 Marks)
Summary of Steps
Partition [8, 4, 7, 3, 10] with pivot 10 → [8, 4, 7, 3, 10].
Partition [8, 4, 7, 3] with pivot 3 → [3, 4, 7, 8].
Partition [4, 7, 8] with pivot 8 → [4, 7, 8].
Partition [4, 7] with pivot 7 → [4, 7].
Combine partitions → [3, 4, 7, 8, 10].
5. Perform the Breath-First Search – BFS and Depth-First Search – DFS on the following graph. (5 Marks)
Initialization:
Steps:
5
oPrint: E
6. Dequeue F and explore its neighbors (B, C, E, G).
o Add unvisited neighbors (G) to the queue.
o Queue: [D, G]
o Visited: {A, B, C, E, F, D, G}
o Print: F
7. Dequeue D and explore its neighbors (C, G).
o All neighbors are already visited.
o Queue: [G]
o Visited: {A, B, C, E, F, D, G}
o Print: D
8. Dequeue G and explore its neighbors (F, D).
o All neighbors are already visited.
o Queue: []
o Visited: {A, B, C, E, F, D, G}
o Print: G
BFS Order: A, B, C, E, F, D, G
Initialization:
Steps:
6
o Print: F
6. Pop G and explore its neighbors (F, D).
o Push unvisited neighbors (D) to the stack.
o Stack: [C, D]
o Visited: {A, B, C, E, F, G, D}
o Print: G
7. Pop D and explore its neighbors (C, G).
o All neighbors are already visited.
o Stack: [C]
o Visited: {A, B, C, E, F, G, D}
o Print: D
8. Pop C and explore its neighbors (A, B, D, F).
o All neighbors are already visited.
o Stack: []
o Visited: {A, B, C, E, F, G, D}
o Print: C
DFS Order: A, B, E, F, G, D, C
6. You are given a sequence of integers to insert the elements into the following 2-3 Tree. Construct the
tree by inserting the numbers one by one. (5 Marks)
Sequence: 42, 44, 11
Insert 42
7
Insert 44
Insert 11
8
Section: Coding
7. If we want to create a binary tree whose nodes contain integer values, we can represent the nodes
using instances of the following Java class.
Write a function that takes root node as an input and returns the sum of the values contained in all
of the nodes of the binary tree with root n. Hint: Recursion is your friend. (5 Marks)
InitializeGraph(numVertices):
adjMatrix ← 2D array of size numVertices x numVertices
For i from 0 to numVertices - 1:
For j from 0 to numVertices - 1:
adjMatrix[i][j] ← 0
AddEdge(vertex1, vertex2):
adjMatrix[vertex1][vertex2] ← 1
adjMatrix[vertex2][vertex1] ← 1 # Because the graph is undirected
DisplayGraph():
9
Print "Graph Edges:"
For i from 0 to numVertices - 1:
For j from i + 1 to numVertices - 1: # Avoid duplicate edges
If adjMatrix[i][j] = 1:
Print "Vertex", i, "-> Vertex", j
9. A company has employees of different roles: Manager and Developer. Each employee has a method
called calculateBonus() to calculate their yearly bonus.
The Manager gets a fixed bonus of PKR 5000.
The Developer gets a bonus of PKR 2000 plus PKR 10 for every line of code written.
Define the Employee class with a method calculateBonus() and at least one relevant attribute.
(1 Mark)
Extend the Employee class to create Manager and Developer classes. Override the
calculateBonus() method in each class. (2 Marks)
Write a main method that:
o Creates an array of Employee objects containing at least one Manager and one
Developer. (1 Mark)
o Demonstrates runtime polymorphism by calling the calculateBonus() method on each
object. (1 Mark)
@Override
public int calculateBonus() {
return 5000;
}
}
10
super(name);
this.linesOfCode = linesOfCode;
}
@Override
public int calculateBonus() {
return 2000 + (10 * linesOfCode);
}
}
10. You are given a singly linked list where only the head node is maintained. The linked list initially
contains the following elements:
Head -> 10 -> 20 -> 30 -> 40 -> null
Implement the deleteNode(int value) method in a LinkedList class. This method should delete the
first occurrence of a node with the specified value. If the value does not exist in the list, the method
should print an appropriate message. (5 Marks)
deleteNode(value):
// Case 1: If the list is empty
if head is null:
Print "The list is empty. Nothing to delete."
return
Good Luck
12