After inserting the following elements, in the given order, into an initially…
2023
After inserting the following elements, in the given order, into an initially empty max heap, which array representation is obtained?
Elements: 20, 30, 70, 10, 5, 50, 80
- A.
80, 70, 50, 30, 20, 10, 5
- B.
5, 10, 20, 30, 50, 70, 80
- C.
70, 20, 80, 10, 5, 30, 50
- D.
80, 20, 70, 10, 5, 30, 50
Attempted by 107 students.
Show answer & explanation
Correct answer: D
Correct answer: 80, 20, 70, 10, 5, 30, 50
After inserting 20, 30, 70, 10, 5, and 50 in order, the heap array becomes 70, 20, 50, 10, 5, 30.
Insert 80 at the next available leaf position. It first swaps with 50, then swaps with 70 because 80 is larger than both.
The final level-order array is 80, 20, 70, 10, 5, 30, 50.
Note: An array may satisfy the max-heap parent-child condition and still not be the result of this particular insertion order. The question is asking for the array obtained by insertion in the given order.