Build challenges · 4 steps · javascript · python
Binary Min-Heap
Construct an array-backed binary min-heap with parent/child arithmetic, siftUp, siftDown, and linear-time heapify.
A Binary Min-Heap is a complete binary tree where every parent node is smaller than or equal to its children.
Because the tree is always complete, we can store it directly in a flat array without pointer allocations:
- Node index: $i$
- Parent index: $\lfloor (i - 1) / 2 \rfloor$
- Left child index: $2i + 1$
- Right child index: $2i + 2$
In this build, you will construct a Binary Min-Heap from scratch:
- Implement constant-time root inspection (
peek). - Add elements with $O(\log n)$ upward bubbling (
pushandsiftUp). - Extract the minimum element with $O(\log n)$ downward bubbling (
popandsiftDown). - Transform an unsorted array into a valid min-heap in $O(n)$ linear time (
heapify).
The workspace
These files carry across every step. What you write in one step is what you start the next with.
- min_heap.js
- harness.jsread-only
Steps · 0 of 4 done
Start anywhere. Open step 3 first and you are handed the reference build of steps 1 and 2, so every step stands on its own. Nothing here is locked behind anything else.
This build applies Heaps & Priority Queues, Arrays & Traversal and Sorting. Read the lesson first if it is unfamiliar — a recommendation, not a prerequisite.