Worked solution
(a) A stack is a Last-In, First-Out (LIFO) data structure where elements are added (pushed) and removed (popped) from the same end (the top). A queue is a First-In, First-Out (FIFO) data structure where elements are inserted at one end (rear/tail) and removed from the opposite end (front/head).
(b) Trace of circular queue (Size 5, indices 0-4):
- Initial: Empty. Head = 0, Tail = 0.
- enqueue(A): Elements = [A, _, _, _, _], Head = 0, Tail = 1.
- enqueue(B): Elements = [A, B, _, _, _], Head = 0, Tail = 2.
- dequeue() (removes A): Elements = [_, B, _, _, _], Head = 1, Tail = 2.
- enqueue(C): Elements = [_, B, C, _, _], Head = 1, Tail = 3.
- enqueue(D): Elements = [_, B, C, D, _], Head = 1, Tail = 4.
- dequeue() (removes B): Elements = [_, _, C, D, _], Head = 2, Tail = 4.
- enqueue(E): Elements = [_, _, C, D, E], Head = 2, Tail = 0 (since index wraps around: \( (4 + 1) \bmod 5 = 0 \)).
- Final State: Head = 2, Tail = 0. Elements in queue: C (at index 2), D (at index 3), E (at index 4).
(c) Binary Search Tree (BST) characteristics:
- Retrieval: A BST keeps elements ordered hierarchically. Starting at the root, the search key is compared to the current node. If smaller, go left; if larger, go right. This eliminates half the search space at each level, leading to an average search time of \( O(\log n) \).
- Dynamic Insertion: To insert, navigate the tree using the same comparison rules until an empty (null) child position is found. Create a new node and link it at this leaf location. No elements need to be shifted, unlike in an array, preserving dynamic and fast efficiency.
(d) Advantages of dynamic data structures:
1. Memory efficiency: They consume memory only as needed, expanding or contracting at runtime.
2. No predefined size limit: Avoids the risk of overflow errors due to fixed array dimensions.
3. Efficient modification: Operations like insertion and deletion do not require shifting all subsequent elements in memory.
Marking scheme
(a) [2 marks]
- 1 mark: Correctly defines stack access (LIFO / same-end access).
- 1 mark: Correctly defines queue access (FIFO / opposite-end access).
(b) [5 marks]
- 1 mark: Correct tracking after enqueue(A) and enqueue(B).
- 1 mark: Correct deletion of A shifting Head pointer to index 1.
- 1 mark: Correct addition of C and D shifting Tail pointer to index 4.
- 1 mark: Correct deletion of B shifting Head pointer to index 2.
- 1 mark: Correct final calculation showing Tail pointer wraps around to index 0 after enqueue(E), and correct identification of final stored elements (C, D, E).
(c) [5 marks]
- 1 mark: Mentions hierarchical root-left-right structure.
- 2 marks: Clear description of search comparison rules (smaller goes left, larger goes right) for logarithmic retrieval efficiency.
- 2 marks: Clear description of the insertion process (finding empty child pointer, attaching new leaf node without shifting other data).
(d) [3 marks]
- 1 mark per valid advantage up to 3 marks (e.g., dynamic resizing, memory conservation, faster insert/delete relative to array shift constraints).