Labs / Computer Science
Binary Heap Builder
A heap is a tree that lives inside a plain array. Insert a value and watch it bubble up until the ordering rule holds again; extract the root and watch the last element sink back down. Every comparison and swap is highlighted at the same time in the tree and in the array beneath it.
size 0i —parent —children —compares 0swaps 0
What to try
- Insert a value smaller than everything else into a min-heap. How many swaps does it take to reach the root, and does that match the tree's height?
- Turn off auto-play and press Step. At each stop, which two array indices are being compared — and how do they relate as parent and child?
- Flip from min-heap to max-heap. Which value jumps to the root, and why does the whole array rearrange the instant you switch?
- Extract the root a few times in a row. Why does the last element get moved to the top first, instead of simply pulling up the smaller child?
- For an array index i, is its parent always at ⌊(i−1)/2⌋ and its children at 2i+1 and 2i+2? Watch the readouts and check.