Full 2025 Detailed Solutions and
Distinction-Level Answer Guide
Contents
1 Introduction 2
2 Assignment Questions and Solutions 2
2.1 Question 1: Implementing a Binary Search Tree . . . . . . . . . . . . 2
2.2 Question 2: Dynamic Programming - 0/1 Knapsack Problem . . . . . 4
2.3 Question 3: Stack Implementation Using Arrays . . . . . . . . . . . . 5
2.4 Question 4: Queue Implementation Using Linked List . . . . . . . . . 6
2.5 Question 5: QuickSort Algorithm . . . . . . . . . . . . . . . . . . . . . . 7
2.6 Question 6: Graph Representation - Adjacency List . . . . . . . . . . . 8
2.7 Question 7: Doubly Linked List Implementation . . . . . . . . . . . . 9
2.8 Question 8: MergeSort Algorithm . . . . . . . . . . . . . . . . . . . . . 11
2.9 Question 9: Priority Queue Using Binary Heap . . . . . . . . . . . . . 12
2.10 Question 10: Longest Common Subsequence . . . . . . . . . . . . . . 13
3 Remaining Questions (11–100) 14
3.1 Question 11: Circular Linked List . . . . . . . . . . . . . . . . . . . . . 16
4 Conclusion 17
1
,1 Introduction
This document provides detailed, distinction-level solutions for COS3711 Com-
puter Science Assignment 2 for the 2025 academic year. It includes 100 carefully
crafted questions and answers, focusing on core concepts such as data struc-
tures, algorithms, object-oriented programming, and advanced C++ program-
ming techniques. The solutions are concise, clear, and designed to help stu-
dents achieve high marks while understanding key concepts. Each question is
highlighted in very dark green and accompanied by a detailed explanation and,
where applicable, C++ code snippets to illustrate implementation.
2 Assignment Questions and Solutions
Below are 100 questions and solutions tailored to COS3711 Assignment 2, cov-
ering essential topics in advanced C++ programming and computer science con-
cepts. Each question is highlighted in very dark green to enhance readability
and focus.
2.1 Question 1: Implementing a Binary Search Tree
Solution: A Binary Search Tree (BST) is a data structure where each node has at
most two children, with the left subtree containing nodes with values less than
the parent and the right subtree containing nodes with values greater than the
parent.
#include <iostream>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
Node(int val) : data(val), left(nullptr), right(nullptr) {}
};
class BST {
private:
Node* root;
Node* insert(Node* node, int data) {
if (!node) return new Node(data);
if (data < node->data)
node->left = insert(node->left, data);
else if (data > node->data)
node->right = insert(node->right, data);
return node;
}
Node* findMin(Node* node) {
2
, while (node->left) node = node->left;
return node;
}
Node* remove(Node* node, int data) {
if (!node) return nullptr;
if (data < node->data)
node->left = remove(node->left, data);
else if (data > node->data)
node->right = remove(node->right, data);
else {
if (!node->left) {
Node* temp = node->right;
delete node;
return temp;
}
if (!node->right) {
Node* temp = node->left;
delete node;
return temp;
}
Node* temp = findMin(node->right);
node->data = temp->data;
node->right = remove(node->right, temp->data);
}
return node;
}
void inorder(Node* node) {
if (node) {
inorder(node->left);
cout << node->data << ”␣”;
inorder(node->right);
}
}
public:
BST() : root(nullptr) {}
void insert(int data) {
root = insert(root, data);
}
void remove(int data) {
root = remove(root, data);
}
void inorder() {
inorder(root);
cout << endl;
}
3