Blog
Data Structures and Algorithms BS Retake Final Past Paper 2014 – UOS Main Campus
ExamFinal Term
Year2014
CampusUOS Main
AnswersSolved ✓
University of Sargodha main campus (Department of Computer Science) Data Structures and Algorithms final term retake paper, 3 February 2014. Retyped from the original paper, with solved answers below.
Solved answers
Q2 – BST and traversals
11
/ \
6 15
/ \ \
1 8 17
\ \
4 10- Preorder: 11, 6, 1, 4, 8, 10, 15, 17
- Inorder: 1, 4, 6, 8, 10, 11, 15, 17
- Postorder: 4, 1, 10, 8, 6, 17, 15, 11
(The other binary trees mentioned in this question were not included in the available photo.)
Q3 – Huffman codes (A = 24, B = 12, C = 10, D = 8, E = 8)
Merge two smallest repeatedly: D + E = 16; C + B = 22; 16 + 22 = 38; A + 38 = 62 (root). Label left 0, right 1:
| Character | Frequency | Code | Bits |
|---|---|---|---|
| A | 24 | 0 | 1 |
| D | 8 | 100 | 3 |
| E | 8 | 101 | 3 |
| C | 10 | 110 | 3 |
| B | 12 | 111 | 3 |
Total bits = 24×1 + (8+8+10+12)×3 = 138 bits (fixed 3-bit code would need 186).
Q4 – BFS from S
Queue processing (neighbours taken in the order u, w, z, L):
| Step | Dequeue | Enqueued (distance) | Queue |
|---|---|---|---|
| 1 | S (0) | u, w, z, L (1) | u, w, z, L |
| 2 | u | v (2) — z already seen | w, z, L, v |
| 3 | w | x (2) — v seen | z, L, v, x |
| 4 | z | y (2) — L seen | L, v, x, y |
| 5 | L | — | v, x, y |
| 6 | v | — (y seen) | x, y |
| 7 | x | — (L seen) | y |
| 8 | y | — | empty |
BFS order: S, u, w, z, L, v, x, y. Distances: S = 0; u, w, z, L = 1; v, x, y = 2. BFS tree parents: u, w, z, L ← S; v ← u; x ← w; y ← z. Vertex M is not reachable from S (its edges go out of M only).
Q5 – New right son in a threaded binary tree
struct TNode { int info; TNode *left, *right; bool rthread; };
// insert new node as the right son of p in a right-in-threaded tree
void setRight(TNode *p, int x) {
TNode *q = new TNode; q->info = x; q->left = NULL;
if (p == NULL) { cout << "void insertion"; return; }
if (!p->rthread) { cout << "p already has a right son"; return; }
q->right = p->right; // q inherits p's inorder successor
q->rthread = true;
p->right = q;
p->rthread = false;
}Q6 – Delete elements from a circular queue until n is found
// circular queue: a[N], front, rear, count
void deleteTill(int n) {
while (count > 0) {
int v = a[front];
if (v == n) { cout << n << " found"; return; } // stop when n is at the front
front = (front + 1) % N; count--; // delete element
}
cout << "Queue empty, " << n << " not found";
}Q7 – Insert a node before a given node (singly linked list)
struct Node { int data; Node *next; };
// insert value x before the node containing key
void insertBefore(Node *&head, int key, int x) {
if (head == NULL) return;
Node *n = new Node; n->data = x;
if (head->data == key) { n->next = head; head = n; return; }
Node *p = head;
while (p->next != NULL && p->next->data != key) p = p->next;
if (p->next == NULL) { cout << "key not found"; delete n; return; }
n->next = p->next; p->next = n;
}Q8 – Delete a particular node from a doubly linked list
struct DNode { int data; DNode *prev, *next; };
void deleteNode(DNode *&head, int key) {
DNode *p = head;
while (p != NULL && p->data != key) p = p->next;
if (p == NULL) { cout << "not found"; return; }
if (p->prev) p->prev->next = p->next; else head = p->next;
if (p->next) p->next->prev = p->prev;
delete p;
}Save this paper for offline studyDownload the question paper, or save the paper together with all solved answers as a PDF.
