An array is heapified. Which one of the following options represents the first three elements in the heapified array?
View worked solution
Answer: D
Since the options start with either 82 or 131 (the maximum element of the array), and no option starts with 11 (the minimum element), the question refers to a max-heap.
We apply the standard bottom-up Build-Max-Heap algorithm on the 1-indexed array:
of size .
The non-leaf nodes are at indices down to , which are indices .
- Heapify at index 5 (): Children are at index 10 (). Since , no swap is needed.
Array: - Heapify at index 4 (): Children are at indices 8 () and 9 (). Swap with the maximum child .
Array: - Heapify at index 3 (): Children are at indices 6 () and 7 (). Since and , no swap is needed.
Array: - Heapify at index 2 (): Children are at indices 4 () and 5 (). Swap with the maximum child .
Array: - Heapify at index 1 (): Children are at indices 2 () and 3 (). Swap with .
Array:
Now, heapify the swapped element at index 2. Its children are at indices 4 () and 5 (). Swap with .
Array:
Now, heapify at index 5. Its child is at index 10 (). Swap with .
Array:
The first three elements of the heapified array are .
Free practice · Your answers on this page are not saved.
