Similar questions 4 more →
Heaps & Priority Queues
![Question 77, GATE 2006: A ternary max-heap is stored level-by-level, left-to-right, in a zero-based array. Root is a[0]; children of i are 3i+1,3i+2,3i+3. Insert by appending then moving upward until the parent is at least as large. Start from the valid heap [9,5,6,8,3,1] and insert 7,2,10,4 in that order. What final array results?
Options:
(A) [10,7,9,8,3,1,5,2,6,4]
(B) [10,9,8,7,6,5,4,3,2,1]
(C) [10,9,4,5,7,6,8,2,1,3]
(D) [10,8,6,9,7,2,3,4,1,5]](../../assets/questions/CS-2006-Q77-1.webp)
![Question 77, GATE 2006: A ternary max-heap is stored level-by-level, left-to-right, in a zero-based array. Root is a[0]; children of i are 3i+1,3i+2,3i+3. Insert by appending then moving upward until the parent is at least as large. Start from the valid heap [9,5,6,8,3,1] and insert 7,2,10,4 in that order. What final array results?
Options:
(A) [10,7,9,8,3,1,5,2,6,4]
(B) [10,9,8,7,6,5,4,3,2,1]
(C) [10,9,4,5,7,6,8,2,1,3]
(D) [10,8,6,9,7,2,3,4,1,5]](../../assets/questions/CS-2006-Q77-2.webp)
![Question 77, GATE 2006: A ternary max-heap is stored level-by-level, left-to-right, in a zero-based array. Root is a[0]; children of i are 3i+1,3i+2,3i+3. Insert by appending then moving upward until the parent is at least as large. Start from the valid heap [9,5,6,8,3,1] and insert 7,2,10,4 in that order. What final array results?
Options:
(A) [10,7,9,8,3,1,5,2,6,4]
(B) [10,9,8,7,6,5,4,3,2,1]
(C) [10,9,4,5,7,6,8,2,1,3]
(D) [10,8,6,9,7,2,3,4,1,5]](../../assets/questions/CS-2006-Q77-3.webp)
Answer & solution
Answer: A (no official key was published)
Step through it (9 steps)
1. Start with [9,5,6,8,3,1]. Insert 7 at index 6 and exchange with parent index 1, obtaining [9,7,6,8,3,1,5].
2. Insert 2 at index 7, whose parent 6 is already larger: [9,7,6,8,3,1,5,2]. Insert 10 at index 8, exchange with parent index 2 (6), then root 9, obtaining [10,7,9,8,3,1,5,2,6].
3. Insert 4 at index 9 under parent 9, with no exchange. Final array [10,7,9,8,3,1,5,2,6,4], A.
4. Teaching check: The parent index is floor((i−1)/3). After appending 10 at index 8, its parent is index 2, then the root, so two upward swaps occur. Appending 4 at index 9 places it under the current 9 and requires no swap.