A max-heap is a heap where the value of each parent is greater than or equal to the value of its children. Which of the following is a max-heap?
View worked solution
Answer: B
A max-heap must satisfy two essential properties:
- Shape Property (Complete Binary Tree): All levels of the tree must be completely filled, except possibly the last level, which must be filled from left to right.
- Max-Heap Property: For every node (except the root), the value of the parent of must be greater than or equal to the value of ().
Let us evaluate each option based on these properties:
- Option A: Violates the max-heap property because the root node with value has a right child with value (since , the parent is smaller than its child).
- Option B:
The tree structure is:
10 / \ 8 5 / \ / 7 6 4- It is a complete binary tree because all levels are filled from left to right.
- It satisfies the max-heap property at every node:
- and
- and
- Option C: Violates the complete binary tree property because the node has a right child but no left child. The last level is not filled from left to right.
- Option D: Violates the max-heap property because the node has a right child , and .
Free practice · Your answers on this page are not saved.
