Step 2 of 4
Insertion & Sift-Up
You start from the build so far — your own work where you have written it, the reference build where you have not. Either way this step stands on its own.
When inserting an element, we place it at the end of the array to maintain the complete tree structure, and then bubble it up until the min-heap property is restored.
Implement push(val) and siftUp(idx):
- Append
valtothis.data. - Call
siftUpstarting at the last index. - While the current element is smaller than its parent, swap them and continue upward.
Your build
Running is free — Submit is what records the step. Or press ⌘↩
Tests
1 case| call | type | expected | result |
|---|---|---|---|
| runOps([["push",10],["push",5],["push",15],["push",2],["peek"],["elements"]]) | maintains minimum at root after insertions | [null,null,null,null,2,[2,5,15,10]] | — |
Hints
Stuck? Hints open one at a time, each giving a little more away.
2 hints left