The elements are inserted one by one in the given order into a maxHeap. The resultant maxHeap is
View worked solution
Answer: A
To find the correct max-heap, we insert the elements one by one into an initially empty binary max-heap. In a max-heap, the value of each node must be greater than or equal to the values of its children.
Step-by-step Insertion:
-
Insert 32:
32 -
Insert 15:
32 / 15 -
Insert 20:
32 / \ 15 20 -
Insert 30:
Initially placed as the left child of 15:
32 / \ 15 20 / 30Since , we swap them to restore the max-heap property:
32 / \ 30 20 / 15 -
Insert 12:
Placed as the right child of 30:
32 / \ 30 20 / \ 15 12 -
Insert 25:
Initially placed as the left child of 20:
32 / \ 30 20 / \ / 15 12 25Since , we swap them:
32 / \ 30 25 / \ / 15 12 20 -
Insert 16:
Placed as the right child of 25:
32 / \ 30 25 / \ / \ 15 12 20 16
The array representation of the final max-heap is [32, 30, 25, 15, 12, 20, 16], which corresponds to the tree structure shown in Option A.
Free practice · Your answers on this page are not saved.
