Consider a binary max-heap implemented using an array. Which one of the…
2009
Consider a binary max-heap implemented using an array. Which one of the following array represents a binary max-heap?
- A.
25,12,16,13,10,8,14
- B.
25,14,13,16,10,8,12
- C.
25,14,16,13,10,8,12
- D.
25,14,12,13,10,8,16
Attempted by 265 students.
Show answer & explanation
Correct answer: C
Correct array that represents a binary max-heap: 25,14,16,13,10,8,12
Verification (array indexed from 0):
Index 0 (25): children at indices 1 and 2 are 14 and 16. 25 >= 14 and 25 >= 16.
Index 1 (14): children at indices 3 and 4 are 13 and 10. 14 >= 13 and 14 >= 10.
Index 2 (16): children at indices 5 and 6 are 8 and 12. 16 >= 8 and 16 >= 12.
Since every parent is greater than or equal to its children, the array 25,14,16,13,10,8,12 satisfies the max-heap property.
Why the other arrays fail (brief):
25,12,16,13,10,8,14 fails because index 1 (12) has a left child 13 (index 3) which is greater than 12.
25,14,13,16,10,8,12 fails because index 1 (14) has a left child 16 (index 3) which is greater than 14.
25,14,12,13,10,8,16 fails because index 2 (12) has a right child 16 (index 6) which is greater than 12.
A video solution is available for this question — log in and enroll to watch it.