AlgoFrames

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