Which of the following is a valid heap ?
2017
Which of the following is a valid heap ?
Attempted by 532 students.
Show answer & explanation
Answer: The only valid heap is the tree in which every parent node is greater than or equal to its children and the tree is a complete binary tree. Specifically, the valid tree satisfies the comparisons 16≥14,10; 14≥8,7; 8≥2,4; 7≥1; 10≥9,3.
Why the selected tree is valid: it is a complete binary tree and every parent value is greater than or equal to its children (examples: 16≥14 and 16≥10; 14≥8 and 14≥7; 8≥2 and 8≥4; 7≥1; 10≥9 and 10≥3).
Why the first other tree is invalid: the node with value 4 has a child 8 (8 > 4), so the heap-order property is violated.
Why the second other tree is invalid: the node with value 1 has a child 7 (7 > 1), which breaks the heap-order property.
Why the fourth tree is invalid: the node with value 3 has children 9 and 10 (both > 3), and also another parent-child violation exists where 4 has child 8 (8 > 4); these violate the heap-order property.