Btech 3rd Sem — Data Structures Solved Paper
- Attempt any five questions. All questions carry equal marks (14 each).
- Marks of every part are shown beside the question; answers are written to that length: definition → working/algorithm → example/diagram → conclusion.
- In case of any doubt, the English version of the question is final.
Question-wise topics
| Q.No. | Topics asked |
|---|---|
| Q1 | Loop complexity; ADT and 3-D array address (row/column major); Prim's algorithm |
| Q2 | Doubly linked list reversal function; infix → postfix; postfix evaluation |
| Q3 | Queue enqueue/dequeue and linked implementation; AVL insertion (10 keys) |
| Q4 | Binary tree from inorder + preorder; complexity of matrix multiplication |
| Q5 | Quick sort; O(n log n) proofs; binary search for 123 |
| Q6 | Threaded binary tree, B-tree; adjacency list / matrix / multilist |
| Q7 | Tower of Hanoi; DFS and BFS algorithms |
| Q8 | Hashing — separate chaining, linear probing, double hashing |
Assume “sequence of statements” is a constant amount of work, O(1).
(i) Two loops in a row
Second loop executes M times → O(M)
Sequential statements add: T = O(N) + O(M)
(ii) Nested loop followed by a non-nested loop
Following loop runs M times → O(M)
T = O(N²) + O(M)
Rules used: consecutive statements → add complexities; nested loops → multiply; keep only the dominant term.
Let A[l₁…u₁][l₂…u₂][l₃…u₃] have dimensions n₁ = u₁ − l₁ + 1, n₂ = u₂ − l₂ + 1, n₃ = u₃ − l₃ + 1, base address B and element size W bytes.
In row-major order the last subscript varies fastest; in column-major order the first subscript varies fastest.
Example: int A[2][3][4] (0-based), B = 1000, W = 4. Address of A[1][0][2]:
- Row-major: 1000 + 4·(1·3·4 + 0·4 + 2) = 1000 + 4·14 = 1056
- Column-major: 1000 + 4·(2·2·3 + 0·2 + 1) = 1000 + 4·13 = 1052
- Choose any start vertex s; T = {s}, MST edge set = ∅.
- While T does not contain all vertices: select the minimum-weight edge (u, v) with u ∈ T and v ∉ T.
- Add v to T and (u, v) to the MST.
- Stop when V − 1 edges are selected.
Complexity O(V²) with adjacency matrix, O(E log V) with a min-heap.
For every node the loop swaps its prev and next pointers, then moves to the next original node (which is now stored in current→prev). Swapping both pointers of every node reverses the list.
| current | temp = old prev | after swap: prev / next | current moves to |
|---|---|---|---|
| 1 | NULL | prev = 2, next = NULL | 2 |
| 2 | 1 | prev = 3, next = 1 | 3 |
| 3 | 2 | prev = 4, next = 2 | 4 |
| 4 | 3 | prev = 5, next = 3 | 5 |
| 5 | 4 | prev = 6, next = 4 | 6 |
| 6 | 5 | prev = NULL, next = 5 | NULL (stop) |
After the loop temp = node 5, and temp→prev is now node 6, so *head_ref = node 6.
(i) Infix to postfix
Precedence: *, / > +, −; all left-associative. Scan left to right using an operator stack:
| Symbol | Stack | Postfix expression | Action |
|---|---|---|---|
| A | — | A | Operand → output |
| + | + | A | Push + |
| ( | + ( | A | Push ( |
| B | + ( | A B | Operand → output |
| + | + ( + | A B | Push + |
| D | + ( + | A B D | Operand → output |
| ) | + | A B D + | Pop till ( : + |
| / | + / | A B D + | Push / |
| E | + / | A B D + E | Operand → output |
| - | - | A B D + E / + | Pop / +, push - |
| F | - | A B D + E / + F | Operand → output |
| * | - * | A B D + E / + F | Push * |
| ( | - * ( | A B D + E / + F | Push ( |
| G | - * ( | A B D + E / + F G | Operand → output |
| + | - * ( + | A B D + E / + F G | Push + |
| H | - * ( + | A B D + E / + F G H | Operand → output |
| / | - * ( + / | A B D + E / + F G H | Push / |
| K | - * ( + / | A B D + E / + F G H K | Operand → output |
| ) | - * | A B D + E / + F G H K / + | Pop till ( : / + |
| (end) | — | A B D + E / + F G H K / + * - | Pop remaining: * - |
(ii) Evaluation of ABC*D/+ (A = 2, B = 3, C = 4, D = 6) — i.e. 2 3 4 * 6 / +
Operand → push; operator → pop two values (second popped is the left operand), apply, push the result.
| Symbol | Action | Stack |
|---|---|---|
| A | Push 2 | 2 |
| B | Push 3 | 2 3 |
| C | Push 4 | 2 3 4 |
| * | Pop 4, 3; 3 * 4 = 12; push | 2 12 |
| D | Push 6 | 2 12 6 |
| / | Pop 6, 12; 12 / 6 = 2; push | 2 2 |
| + | Pop 2, 2; 2 + 2 = 4; push | 4 |
Algorithm ENQUEUE(QUEUE, MAX, FRONT, REAR, ITEM) — array
- If REAR = MAX − 1 → print “Overflow” and exit.
- If FRONT = −1 then FRONT = 0.
- REAR = REAR + 1.
- QUEUE[REAR] = ITEM.
- Exit.
Algorithm DEQUEUE(QUEUE, FRONT, REAR)
- If FRONT = −1 or FRONT > REAR → print “Underflow” and exit.
- ITEM = QUEUE[FRONT].
- If FRONT = REAR then FRONT = REAR = −1 (queue becomes empty) else FRONT = FRONT + 1.
- Return ITEM.
Queue implementation using a linked list
Two pointers are kept: front (first node, for deletion) and rear (last node, for insertion). Both operations are O(1) and the queue never becomes full until memory is exhausted.
- Enqueue: create node t; t→next = NULL; if rear = NULL then front = rear = t else rear→next = t, rear = t.
- Dequeue: if front = NULL → underflow; t = front; front = front→next; if front = NULL then rear = NULL; free(t).
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *front = NULL, *rear = NULL;
void enqueue(int x) {
struct node *t = (struct node *)malloc(sizeof(struct node));
t->data = x;
t->next = NULL;
if (rear == NULL) /* queue empty */
front = rear = t;
else {
rear->next = t;
rear = t;
}
}
int dequeue(void) {
if (front == NULL) { printf("Queue Underflow\n"); return -1; }
struct node *t = front;
int x = t->data;
front = front->next;
if (front == NULL) rear = NULL; /* queue became empty */
free(t);
return x;
}
void display(void) {
struct node *p = front;
printf("Queue: ");
while (p != NULL) { printf("%d ", p->data); p = p->next; }
printf("\n");
}
int main(void) {
enqueue(10); enqueue(20); enqueue(30);
display();
printf("Dequeued %d\n", dequeue());
display();
return 0;
}Output: Queue: 10 20 30 → Dequeued 10 → Queue: 20 30.
Insert as in a BST; after each insertion compute BF = h(L) − h(R) on the path to the root and apply the rotation for the first node with |BF| = 2.
Logic
- The first element of the preorder sequence is the root.
- Locate the root in the inorder sequence: elements to its left form the left subtree, elements to its right form the right subtree.
- Take the next elements of the preorder (as many as the size of each part) and repeat recursively for the left and right subtrees.
Construction
| Step | Preorder part | Inorder part | Root | Left | Right (inorder) |
|---|---|---|---|---|
| 1 | F A E K C D H G B | E A C K F H D B G | F | E A C K | H D B G |
| 2 | A E K C | E A C K | A | E | C K |
| 3 | K C | C K | K | C | — |
| 4 | D H G B | H D B G | D | H | B G |
| 5 | G B | B G | G | B | — |
Check: preorder of the tree = F A E K C D H G B ✔, inorder = E A C K F H D B G ✔, postorder = E C K A H B G D F.
1. Standard (iterative) algorithm
for (i = 0; i < N; i++)
for (j = 0; j < N; j++) {
C[i][j] = 0;
for (k = 0; k < N; k++)
C[i][j] = C[i][j] + A[i][k] * B[k][j];
}Operations counted: scalar multiplications and additions (the basic operations of the innermost statement).
- Multiplications: N · N · N = N³.
- Additions: N³ (or N²(N − 1) if C[i][j] is initialised with the first product).
- Assignments/comparisons of loop counters are proportional to these, so T(N) = Θ(N³).
Best, average and worst case are the same, Θ(N³): the loops always run their full ranges — there is no data-dependent branch, so the values in A and B do not affect the count. (Only special matrices — sparse, diagonal — allow faster special algorithms.)
Space: input matrices 2N², output N² → total Θ(N²); auxiliary space O(1) (only i, j, k).
2. Divide-and-conquer and Strassen's algorithm
- Split each matrix into four N/2 × N/2 blocks: simple divide and conquer needs 8 block multiplications → T(N) = 8T(N/2) + Θ(N²) = Θ(N³) (no gain).
- Strassen uses only 7 block multiplications (and 18 additions) → T(N) = 7T(N/2) + Θ(N²) = Θ(Nlog₂7) ≈ Θ(N2.81) in all cases.
- Strassen needs Θ(N²) extra space for temporary sub-matrices and its recursion stack is O(log N) deep; it pays off only for large N.
| Algorithm | Best = Average = Worst | Extra space |
|---|---|---|
| Standard triple loop | Θ(N³) | O(1) |
| Divide & conquer (8 products) | Θ(N³) | O(N²) |
| Strassen (7 products) | Θ(N^2.81) | O(N²) |
(i) Quick sort
Partition (first element as pivot): move i right while a[i] ≤ pivot, move j left while a[j] > pivot; if i < j swap a[i] and a[j]; when they cross, swap the pivot with a[j]. The pivot is then in its final place; sort the left and right parts recursively.
| Sub-array (index) | Pivot | Swaps during partition | Array after partition |
|---|---|---|---|
| [0..6] | 9 | swap 12 ↔ 7; j crossed i → swap pivot 9 with 5 | 547691012 |
| [0..3] | 5 | j crossed i → swap pivot 5 with 4 | 457691012 |
| [2..3] | 7 | j crossed i → swap pivot 7 with 6 | 456791012 |
| [5..6] | 10 | pivot 10 stays | 456791012 |
Pivot shown in red. First partition in detail: pivot 9, i stops at 12, j stops at 7 → swap → [9, 4, 7, 6, 5, 10, 12]; i stops at 10, j stops at 5 → crossed → swap 9 with 5 → [5, 4, 7, 6, 9, 10, 12].
(ii) Worst-case running times
Heap sort: Build-heap takes O(n). Then n − 1 times: swap root with last element and heapify the root — each heapify moves down at most the height ⌊log₂ n⌋. Total ≤ O(n) + (n − 1)·O(log n) = O(n log n) in every case.
Merge sort: T(n) = 2T(n/2) + cn (divide in two halves, merge in linear time). The recursion tree has log₂ n levels, each level costs cn in total ⇒ T(n) = cn log₂ n + cn = O(n log n) for every input.
Quick sort: if every partition splits the array evenly, T(n) = 2T(n/2) + cn = O(n log n) (best and average case). But in the worst case (e.g. already sorted data with first-element pivot) one part is empty: T(n) = T(n − 1) + cn = c(n + (n − 1) + … + 1) = O(n²).
The list is already sorted (indices 0–11). low = 0, high = 11.
| low | high | mid | a[mid] | Decision |
|---|---|---|---|---|
| 0 | 11 | 5 | 194 | 123 < 194 → search left half: high = 4 |
| 0 | 4 | 2 | 101 | 123 > 101 → search right half: low = 3 |
| 3 | 4 | 3 | 123 | Found |
Yes, there is a difference. In an ordinary (unthreaded) binary tree with n nodes there are 2n link fields, of which n + 1 are NULL — wasted space. A threaded binary tree replaces these NULL links with pointers called threads: a NULL right link points to the node's inorder successor and a NULL left link to its inorder predecessor. One extra bit per link (lthread/rthread) tells whether it is a child pointer or a thread.
| Unthreaded binary tree | Threaded binary tree |
|---|---|
| NULL pointers in leaf links (n + 1 of them) | NULL links replaced by threads to inorder predecessor/successor |
| Inorder traversal needs recursion or an explicit stack | Inorder traversal without stack or recursion |
| Finding inorder successor needs a search from root | Successor found directly via thread (O(1) in many cases) |
| Simpler insertion/deletion | Insertion/deletion must also update threads; needs flag bits |
B-tree
1. every node has at most m children and m − 1 keys (kept in sorted order);
2. every node except the root has at least ⌈m/2⌉ children (⌈m/2⌉ − 1 keys);
3. the root has at least 2 children if it is not a leaf;
4. all leaves are at the same level;
5. keys in a node separate the keys of its subtrees (like a BST).
- Insertion: insert into the proper leaf; if it overflows (m keys), split it — the median key moves up to the parent (splits may propagate to the root, increasing height).
- Deletion: remove the key; if a node underflows, borrow from a sibling or merge with it.
- Advantages: height is O(logm n) — very few disk accesses because each node holds many keys (one node = one disk block).
(i) Adjacency list
An array of head pointers, one per vertex; each points to a linked list of that vertex's neighbours (total 2E = 18 list nodes).
| Vertex | Adjacency list |
|---|---|
| A | B → C → D → E → NULL |
| B | A → D → E → NULL |
| C | A → D → NULL |
| D | A → B → C → F → NULL |
| E | A → B → F → NULL |
| F | D → E → NULL |
(ii) Adjacency matrix
6 × 6 matrix, a[i][j] = 1 if vertices i and j are adjacent, else 0. It is symmetric (undirected graph); row sums give the degrees.
| A | B | C | D | E | F | |
|---|---|---|---|---|---|---|
| A | 0 | 1 | 1 | 1 | 1 | 0 |
| B | 1 | 0 | 0 | 1 | 1 | 0 |
| C | 1 | 0 | 0 | 1 | 0 | 0 |
| D | 1 | 1 | 1 | 0 | 0 | 1 |
| E | 1 | 1 | 0 | 0 | 0 | 1 |
| F | 0 | 0 | 0 | 1 | 1 | 0 |
Degrees: A = 4, B = 3, C = 2, D = 4, E = 3, F = 2 (sum 18 = 2 × 9 edges).
(iii) Adjacency multi-list
Each edge is stored only once as a node with fields [mark | vi | vj | linki | linkj], where linki points to the next edge node incident on vi and linkj to the next edge node incident on vj. Each vertex has a head pointer to its first edge.
| Edge node | mark | vi | vj | link for vi | link for vj |
|---|---|---|---|---|---|
| N1 | 0 | A | B | N2 | N5 |
| N2 | 0 | A | C | N3 | N7 |
| N3 | 0 | A | D | N4 | N5 |
| N4 | 0 | A | E | NULL | N6 |
| N5 | 0 | B | D | N6 | N7 |
| N6 | 0 | B | E | NULL | N9 |
| N7 | 0 | C | D | NULL | N8 |
| N8 | 0 | D | F | NULL | N9 |
| N9 | 0 | E | F | NULL | NULL |
| Vertex | Head → first edge | Edges reached by following links |
|---|---|---|
| A | N1 | N1 → N2 → N3 → N4 |
| B | N1 | N1 → N5 → N6 |
| C | N2 | N2 → N7 |
| D | N3 | N3 → N5 → N7 → N8 |
| E | N4 | N4 → N6 → N9 |
| F | N8 | N8 → N9 |
Recursive solution
void TOH(int n, char src, char aux, char dest) {
if (n == 1) {
printf("Move disk 1 from %c to %c\n", src, dest);
return;
}
TOH(n - 1, src, dest, aux); /* move n-1 disks to auxiliary */
printf("Move disk %d from %c to %c\n", n, src, dest);
TOH(n - 1, aux, src, dest); /* move n-1 disks onto disk n */
}Example: n = 3 — TOH(3, A, B, C)
| Move | Disk | From → To |
|---|---|---|
| 1 | 1 | A → C |
| 2 | 2 | A → B |
| 3 | 1 | C → B |
| 4 | 3 | A → C |
| 5 | 1 | B → A |
| 6 | 2 | B → C |
| 7 | 1 | A → C |
Recurrence: T(n) = 2T(n − 1) + 1, T(1) = 1 ⇒ T(n) = 2ⁿ − 1 moves (7 for n = 3, 15 for n = 4); time O(2ⁿ), recursion depth O(n).
Breadth First Search (BFS)
Visits all neighbours of a vertex first, then their neighbours — level by level. Uses a queue.
- Mark all vertices unvisited. Enqueue the start vertex S and mark it visited.
- While the queue is not empty: dequeue a vertex U and process (print) it.
- For every neighbour V of U that is unvisited: mark V visited and enqueue V.
- Stop when the queue is empty.
Depth First Search (DFS)
Goes as deep as possible along a path before backtracking. Uses a stack (or recursion).
- Mark all vertices unvisited; call DFS(S).
- DFS(U): mark U visited and process it.
- For every neighbour V of U: if V is unvisited, call DFS(V) (push on stack).
- When U has no unvisited neighbour, return (pop / backtrack).
void BFS(int s) {
int u, v;
enqueue(s); visited[s] = 1;
while (!isEmpty()) {
u = dequeue();
printf("%d ", u);
for (v = 0; v < n; v++)
if (adj[u][v] == 1 && visited[v] == 0) {
visited[v] = 1;
enqueue(v);
}
}
}
void DFS(int u) {
int v;
visited[u] = 1;
printf("%d ", u);
for (v = 0; v < n; v++)
if (adj[u][v] == 1 && visited[v] == 0)
DFS(v);
}Example
| Dequeued | Newly enqueued | Queue | BFS order |
|---|---|---|---|
| A | B, C | B C | A |
| B | D, E | C D E | A B |
| C | F | D E F | A B C |
| D | — | E F | A B C D |
| E | — | F | A B C D E |
| F | — | empty | A B C D E F |
DFS trace: A → B → D (dead end, backtrack to B) → E → F → C (all visited, backtrack to A). Both take O(V + E) time with adjacency lists (O(V²) with a matrix).
Hash values
| x | h(x) = x mod 7 | h₁(x) = (2x − 1) mod 7 |
|---|---|---|
| 2341 | 2341 mod 7 = 3 | (2·2341 − 1) mod 7 = 5 |
| 4234 | 4234 mod 7 = 6 | (2·4234 − 1) mod 7 = 4 |
| 2839 | 2839 mod 7 = 4 | (2·2839 − 1) mod 7 = 0 |
| 430 | 430 mod 7 = 3 | (2·430 − 1) mod 7 = 5 |
| 22 | 22 mod 7 = 1 | (2·22 − 1) mod 7 = 1 |
| 397 | 397 mod 7 = 5 | (2·397 − 1) mod 7 = 2 |
| 3920 | 3920 mod 7 = 0 | (2·3920 − 1) mod 7 = 6 |
Collisions: 2341 and 430 both hash to 3.
(i) Separate chaining
Each slot keeps a linked list; a colliding key is appended to the list of its slot.
| Index | Chain |
|---|---|
| 0 | 3920 → NULL |
| 1 | 22 → NULL |
| 2 | NULL |
| 3 | 2341 → 430 → NULL |
| 4 | 2839 → NULL |
| 5 | 397 → NULL |
| 6 | 4234 → NULL |
(ii) Linear probing
On collision try h(x) + 1, h(x) + 2, … (mod 7) until a free slot is found.
| Key | Slots probed | Placed at |
|---|---|---|
| 2341 | 3 | 3 |
| 4234 | 6 | 6 |
| 2839 | 4 | 4 |
| 430 | 3 → 4 → 5 | 5 |
| 22 | 1 | 1 |
| 397 | 5 → 6 → 0 | 0 |
| 3920 | 0 → 1 → 2 | 2 |
| Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|---|
| Key | 397 | 22 | 3920 | 2341 | 2839 | 430 | 4234 |
430 → 3 (taken by 2341) → 4 (2839) → 5 ✔; 397 → 5 (430) → 6 (4234) → 0 ✔; 3920 → 0 (397) → 1 (22) → 2 ✔ — note the clustering.
(iii) Double hashing
Probe sequence: indexk = (h(x) + k·h₁(x)) mod 7, k = 0, 1, 2, …
| Key | h(x) | h₁(x) | Slots probed | Placed at |
|---|---|---|---|---|
| 2341 | 3 | 5 | 3 | 3 |
| 4234 | 6 | 4 | 6 | 6 |
| 2839 | 4 | 0 | 4 | 4 |
| 430 | 3 | 5 | 3 → 1 | 1 |
| 22 | 1 | 1 | 1 → 2 | 2 |
| 397 | 5 | 2 | 5 | 5 |
| 3920 | 0 | 6 | 0 | 0 |
| Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|---|
| Key | 3920 | 430 | 22 | 2341 | 2839 | 397 | 4234 |
430: h = 3 occupied, step 5 → (3 + 5) mod 7 = 1 ✔. 22: h = 1 now occupied by 430, step 1 → 2 ✔. 397 → 5 and 3920 → 0 directly.