IITP · Semester ISummaries · Quizzes · Assignments

ECS 5101 — Design and Analysis of Algorithms

Assignment 4 — Data Structures: Trees, Heaps & AVL

Source: IITP\Sem-1\ECS5101-DAA\assignments\assignment-4.md

Topic: Binary Search Trees · M-way and B-trees · Heaps · Heap Sort · AVL Trees Questions: 20 (17 MCQ + 2 True/False + 1 True/False) · Marks: as per LMS Source copies: source/ — two PDFs of the same question set with shuffled option order.

Verification status: ✅ All 20 marked answers checked. All 20 are correct. No corrections needed. Option letters differ between the two source PDFs (the options are shuffled), but every correct option text agrees.


Quick answer key

Q Answer Q Answer Q Answer Q Answer
1 C — Left, Root, Right 6 B — Minimum element 11 A — 20, 40, 30, 60, 80, 70, 50 16 A — Right rotation
2 B — 3 7 A — O(n log n) worst case 12 C — 10, 20, 30, 40, 50, 60 17 D — RL
3 B — Multiple sorted keys 8 B — Heapify reduced heap 13 B — Inorder successor 18 C — 2
4 B — 27 9 A — [10, 5, 3, 4, 1] 14 D — Self-balancing BST 19 True
5 B — Between 30 and 60 10 C — No, generally not 15 B — −1, 0, 1 20 True

Detailed questions, verified answers & explanations

Q1. What is the inorder traversal of a Binary Search Tree?

A. Left, Right, Root B. Right, Root, Left C. Left, Root, Right D. Root, Left, Right

✅ Correct: C — Left, Root, Right Inorder = visit left subtree → root → right subtree. In a BST this yields keys in ascending (sorted) order. Why not: A = postorder, B = reverse inorder, D = preorder.


Q2. Maximum number of keys in a single node of an order-4 M-way search tree?

A. 4 B. 3 C. 2 D. 5

✅ Correct: B — 3 An M-way node has at most M children and therefore at most M − 1 keys. Order 4 → 4 − 1 = 3 keys.


Q3. Which statement correctly describes an M-way search tree?

A. All leaves must be at the same level B. Each node can contain multiple sorted keys C. All nodes must have exactly m children D. Each node can contain only one key

✅ Correct: B — Each node can contain multiple sorted keys Each node holds up to M − 1 keys in sorted order, and the keys split the child subtrees into ranges. Why not: leaves-at-same-level is a B-tree property; nodes have up to M children (not exactly); a node holds up to M − 1 keys (not one).


Q4. Maximum number of nodes at level 3 of a full M-way tree of order 3 (root at level 0)?

A. 9 B. 27 C. 18 D. 81

✅ Correct: B — 27 Level k has at most M^k nodes. Here 3³ = 27.


Q5. A B-tree of order 5 has a root with keys 30, 60. Valid range of keys in its middle child?

A. Greater than 60 B. Between 30 and 60 C. Any integer value D. Less than 30

✅ Correct: B — Between 30 and 60 Root keys split children into three ranges: left < 30, middle between 30 and 60, right > 60.


Q6. Which element is always present at the root of a Min Heap?

A. Last inserted element B. Minimum element C. Median element D. Maximum element

✅ Correct: B — Minimum element Heap property: every parent ≤ its children, so the minimum sits at the root. Why not: the maximum is at a leaf; median has no fixed position.


Q7. Major advantage of Heap Sort over Quick Sort?

A. Guarantees O(n log n) worst-case time B. Always faster C. Requires O(n) auxiliary space D. Is stable

✅ Correct: A — Guarantees O(n log n) worst-case Quick Sort degrades to O(n²) with bad pivots; Heap Sort is O(n log n) in all cases. Why not: Heap Sort is not stable and is in-place (O(1) extra space), and Quick Sort is usually faster in practice.


Q8. During Heap Sort (Max Heap), after swapping the root with the last element of the active heap, what is performed?

A. Merge two sorted arrays B. Heapify the reduced heap C. Binary search D. Reverse the entire array

✅ Correct: B — Heapify the reduced heap The maximum is now in its final position; shrink the heap by one and sift-down (heapify) from the new root.


Q9. Array [4, 10, 3, 5, 1] — Max Heap after bottom-up heap construction?

A. [10, 5, 3, 4, 1] B. [10, 4, 5, 3, 1] C. [10, 3, 5, 4, 1] D. [5, 10, 3, 4, 1]

✅ Correct: A — [10, 5, 3, 4, 1] Start at last non-leaf (index 1): 10 ≥ children 5, 1 → no change. Index 0 (4): swap with larger child 10 → [10, 4, 3, 5, 1]; then at index 1, 4 < 5 → swap → [10, 5, 3, 4, 1]. Check: 10 ≥ 5,3 and 5 ≥ 4,1. ✓


Q10. Is Heap Sort a stable sorting algorithm?

A. Only for Min Heap B. Yes, always C. No, generally not D. Only for Max Heap

✅ Correct: C — No, generally not Swap-and-heapify moves elements long distances, so equal keys can change relative order (e.g. [2a, 2b, 1]).


Q11. Insert 50, 30, 70, 20, 40, 60, 80 into an empty BST. What is the postorder traversal?

A. 20, 40, 30, 60, 80, 70, 50 B. 50, 30, 20, 40, 70, 60, 80 C. 80, 70, 60, 50, 40, 30, 20 D. 20, 30, 40, 50, 60, 70, 80

✅ Correct: A — 20, 40, 30, 60, 80, 70, 50 Postorder = Left, Right, Root. Left subtree (30: 20, 40) → 20, 40, 30; right subtree (70: 60, 80) → 60, 80, 70; then root 50. Note: the other source PDF's copy of this question was truncated; it assumed inorder, whose answer is 20, 30, 40, 50, 60, 70, 80. Both copies are internally consistent — see the note below.


Q12. Which insertion sequence produces a completely skewed BST?

A. 40, 20, 60, 10, 30, 50, 70 B. 50, 30, 70, 20, 40, 60, 80 C. 10, 20, 30, 40, 50, 60 D. 40, 20, 60, 30, 10, 50

✅ Correct: C — 10, 20, 30, 40, 50, 60 Strictly increasing (or decreasing) insertion makes every new key the right (or left) child of the previous one → a chain.


Q13. Deleting a node with two children from a BST — what can replace it?

A. Any node from the left subtree B. Inorder successor C. Root of the tree D. Any leaf node

✅ Correct: B — Inorder successor Use the inorder successor (smallest key in the right subtree) — or equivalently the inorder predecessor (largest key in the left subtree). Both preserve BST ordering.


Q14. An AVL tree is a:

A. Complete binary tree B. Multiway search tree C. Full binary tree D. Self-balancing binary search tree

✅ Correct: D — Self-balancing binary search tree A BST where every node's subtree heights differ by at most 1; rotations restore balance after insert/delete → O(log n).


Q15. Which balance factors are valid for every node in a properly balanced AVL tree?

A. −2, 0, 2 B. −1, 0, 1 C. −2, −1, 0, 1, 2 D. 0, 1, 2

✅ Correct: B — −1, 0, 1 Balance factor = height(left) − height(right) ∈ {−1, 0, 1}. Reaching ±2 signals imbalance → rotation.


Q16. Rotation required for insertion sequence 30, 20, 10?

A. Right rotation B. Right-left rotation C. Left-right rotation D. Left rotation

✅ Correct: A — Right rotation 30 → 20 → 10 forms a left-left (LL) chain; a single right rotation at 30 makes 20 the root.


Q17. Rotation required for insertion sequence 10, 30, 20?

A. LL B. RR C. LR D. RL

✅ Correct: D — RL (Right-Left) 20 is inserted into the left subtree of the right child (30) → RL case: right-rotate at 30, then left-rotate at 10. Result: 20 root, children 10 and 30.


Q18. A node has left subtree height 4 and right subtree height 2. Balance factor?

A. 6 B. −1 C. 2 D. −2

✅ Correct: C — 2 BF = 4 − 2 = 2 → outside {−1, 0, 1}, so the node is unbalanced (left-heavy).


Q19. The inorder predecessor of a node is the largest key in its left subtree, if that subtree exists.

True / False

✅ Correct: True If a left subtree exists, the inorder predecessor is its rightmost (largest) key. Otherwise it is the nearest ancestor for which the node lies in the right subtree.


Q20. A Max Heap produces ascending order when repeatedly removing its maximum and placing it at the end of the array.

True / False

✅ Correct: True Each extraction puts the current maximum in the last free slot; after all extractions the array reads smallest → largest — exactly Heap Sort.


Cross-check notes

  • The two source PDFs contain the same 20 questions with shuffled option order. Every correct option text matches across both.
  • Q11 is the only place the two copies diverge in wording: DAA_Assignment_4.pdf states "postorder" (answer A); ECS 5101-…-Assignment-4.pdf had the question truncated and assumed "inorder" (answer = 20,30,40,50,60,70,80). If your LMS paper says postorder, answer A; if it says inorder, answer the sorted sequence. Both source keys are correct for their own wording.
  • All other 19 answers are correct as marked — no corrections required.