The elements 32, 15, 20, 30, 12, 25, 16 are inserted one by one in the given…
2004
The elements 32, 15, 20, 30, 12, 25, 16 are inserted one by one in the given order into a Max Heap. The resultant Max Heap is.
- A.
a
- B.
b
- C.
c
- D.
d
Attempted by 206 students.
Show answer & explanation
Correct answer: A
Solution: Build the max-heap by inserting each element and percolating up as needed.
Insert 32: heap = [32].
Insert 15: heap = [32, 15]. No percolation since 15 < 32.
Insert 20: heap = [32, 15, 20]. No percolation since 20 < 32.
Insert 30: temporary array [32, 15, 20, 30]. Percolate 30 up: swap with parent 15 -> [32, 30, 20, 15].
Insert 12: heap = [32, 30, 20, 15, 12]. 12 < 30, so no percolation.
Insert 25: temporary [32, 30, 20, 15, 12, 25]. Percolate 25 up: swap with parent 20 -> [32, 30, 25, 15, 12, 20]. 25 < 32 so stop.
Insert 16: heap = [32, 30, 25, 15, 12, 20, 16]. 16 < 25, so no percolation.
Final heap array (level order): [32, 30, 25, 15, 12, 20, 16]
Final tree structure (by positions):
Root: 32
Left child of root: 30 (children: 15 left, 12 right)
Right child of root: 25 (children: 20 left, 16 right)
Therefore the correct tree is the one with root 32, left subtree rooted at 30 with children 15 and 12, and right subtree rooted at 25 with children 20 and 16.
A video solution is available for this question — log in and enroll to watch it.