Insert into a heap
Warm-up · Trace the algorithm · about 5 min
The min-heap [2, 5, 3, 9, 7, 8] receives the value 1. It is placed in the next free slot first.
Answer each question before the step plays: first which node 1 meets, then the array after the sift up finishes.
Example 1
Input:
{"values":[2,5,3,9,7,8],"value":1}
Output:
[1,5,2,9,7,8,3]
The new value 1 climbs to the root.
Constraints
- The trace uses the heap and value shown
Learn the idea first: Binary heaps: array layout, sift up, sift down, a visual lesson.
More Binary heaps: array layout, sift up, sift down problems
- Parent and child indices Warm-up
- Which array is a min-heap? Warm-up
- Is this array a min-heap? Warm-up
- Extract the minimum Standard
- Implement push and pop Standard
- Spot the bug: sift down Standard
- Implement a max-heap with decrease-key Challenge