- GATE CS
- 2026–27 pattern practice
- Data Structures
- Heaps
- Medium
The max-heap is stored in an array with the root at index 1. After inserting with the standard sift-up procedure, at which index does end up?
Trees
Pick an option to check your answer.
Answer: D. Solution
Insert 45 at the next free slot, index 8. Its parent is index 4 (value 10); 45 > 10, so swap, and 45 is at index 4. The new parent is index 2 (value 30); 45 > 30, so swap, and 45 is at index 2. The next parent is index 1 (value 50); 45 < 50, so stop. 45 ends at index 2.
Why not the others
- 1 would need 45 to be larger than the root 50, which it is not.
- 4 is where 45 sits after its first swap with 10, but it must still be compared with its new parent 30.
- 8 is where 45 is first placed; it is larger than its parent 10, so it moves up.