Which one of the following array represents a binary max-heap ?
2016
Which one of the following array represents a binary max-heap ?
- A.
[26, 13, 17, 14, 11, 9, 15]
- B.
[26, 15, 14, 17, 11, 9, 13]
- C.
[26, 15, 17, 14, 11, 9, 13]
- D.
[26, 15, 13, 14, 11, 9, 17]
Attempted by 465 students.
Show answer & explanation
Correct answer: C
Key idea: In a max-heap stored as a 0-based array, for every index i the children are at indices 2i+1 and 2i+2 (if they exist). The max-heap property requires each parent to be greater than or equal to its children.
Check the array [26, 15, 17, 14, 11, 9, 13]:
Index 0 (value 26): children at indices 1 and 2 are 15 and 17. 26 >= 15 and 26 >= 17, so this satisfies the property.
Index 1 (value 15): children at indices 3 and 4 are 14 and 11. 15 >= 14 and 15 >= 11, so this satisfies the property.
Index 2 (value 17): children at indices 5 and 6 are 9 and 13. 17 >= 9 and 17 >= 13, so this satisfies the property.
All parent nodes are greater than or equal to their children, therefore the array [26, 15, 17, 14, 11, 9, 13] represents a binary max-heap.