Which one of the following sequences when stored in an array at locations forms a max-heap?
GATE CSE 2023Data Structures › Heaps & Priority QueuesVerified
GATE CSE 2023 PYQ: Which one of the following sequences when stored in an array at locations A [ 1 ] , … , A [ 10 ] A[1],…
- A23,17,10,6,13,14,1,5,7,12
- B23,17,14,7,13,10,1,5,6,12Correct answer
- C23,17,14,6,13,10,1,5,7,15
- D23,14,17,1,10,13,16,12,7,5
Answer
(B) 23 , 17 , 14 , 7 , 13 , 10 , 1 , 5 , 6 , 12 23,17,14,7,13,10,1,5,6,12 23 , 17 , 14 , 7 , 13 , 10 , 1 , 5 , 6 , 12
Approach
A binary max-heap stored in an array A[1..10] must satisfy the heap-order property at every node: each parent is at least as large as both its children. With 1-based indexing, node i has children at 2i and 2i+1, which are only present if those indices are ≤ 10. Check the parent-child inequalities for each candidate sequence; the first one that survives all checks (and is a valid complete binary tree, which every 10-element array automatically is) is the answer.
A max-heap in array form requires A[i] ≥ A[2i] and A[i] ≥ A[2i+1] for all valid indices. Testing the five internal nodes (indices 1-5) against their children eliminates A (10 < 14 at node 3), C (13 < 15 at node 5) and D (1 < 12 at node 4). Only option B satisfies every constraint, so B is the max-heap.
Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.