Which one of the following arrays represents a binary max heap?
2022
Which one of the following arrays 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 576 students.
Show answer & explanation
Correct answer: C
Rule: For an array representation of a binary max-heap using 1-based indexing, each parent at index i must be greater than or equal to its children at indices 2i and 2i+1 (if they exist).
For [25, 12, 16, 13, 10, 8, 14]: index 2 has value 12 and its left child at index 4 has value 13. Since 12 < 13, the max-heap property is violated.
For [25, 14, 13, 16, 10, 8, 12]: index 2 has value 14 and its left child at index 4 has value 16. Since 14 < 16, the max-heap property is violated.
For [25, 14, 16, 13, 10, 8, 12]:
Index 1 has value 25 and children 14 and 16; 25 ≥ 14 and 25 ≥ 16.
Index 2 has value 14 and children 13 and 10; 14 ≥ 13 and 14 ≥ 10.
Index 3 has value 16 and children 8 and 12; 16 ≥ 8 and 16 ≥ 12.
For [25, 14, 12, 13, 10, 8, 16]: index 3 has value 12 and its right child at index 7 has value 16. Since 12 < 16, the max-heap property is violated.
Conclusion: [25, 14, 16, 13, 10, 8, 12] is the only array that satisfies the binary max-heap property.