DSA - Data Structure & Algorithms, Past Papers, UOS Main Campus

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:

CharacterFrequencyCodeBits
A2401
D81003
E81013
C101103
B121113

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):

StepDequeueEnqueued (distance)Queue
1S (0)u, w, z, L (1)u, w, z, L
2uv (2) — z already seenw, z, L, v
3wx (2) — v seenz, L, v, x
4zy (2) — L seenL, v, x, y
5L—v, x, y
6v— (y seen)x, y
7x— (L seen)y
8y—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.
↓ Download Paper (JPG)

About Saif R

An educator and technology enthusiast passionate about empowering students through quality education and digital learning. Dedicated to making academic resources and opportunities accessible to everyone.