Binary Heap Generator
A binary heap keeps exactly one rule: every parent beats both its children. That is weaker than being sorted — only the root is guaranteed to be the extreme value — and it is precisely what makes insertion and extraction logarithmic rather than linear. The array form has no pointers at all: the children of position i live at 2i+1 and 2i+2, which is why heaps are the priority queue everyone actually implements.
What this generator does
Inserts values one at a time, sifting each up until the heap property holds, and reports how far each one travelled. It then drains the heap repeatedly to show the order values come out in, which is sorted order.
How to use this tool
- Choose how many values to insert and whether the smallest or largest sits on top.
- Insert them yourself and compare the array against the one shown.
- Read the level-by-level view to see the tree shape.
- Check the drained order — it comes out sorted, which is heapsort.
Understanding the controls
- How many values
- Between 4 and 24 distinct values, inserted in the order drawn — the shape depends on that order.
- Order
- A min-heap keeps the smallest at the root; a max-heap the largest. The rule is otherwise identical.
- Seed
- Any word reproduces the same insertion order and therefore the same heap, which matters as a fixture.
Common use cases
- Worked heap examples with the array and the tree side by side
- Showing that a heap is not sorted, only partially ordered
- Test data for a priority queue implementation
- Teaching why the array form needs no pointers
- Reproducing the same heap from a seed
How this generator works
Each insertion appends to the array and swaps upward while the parent loses to the child. The check verifies the property at every parent, that the contents match what was inserted, and — the strongest test — that draining the heap produces exactly sorted order, which is only possible if the structure is correct throughout.
Randomness and fairness
The values and their insertion order are random; the heap property is enforced and verified. Seeded heaps reproduce exactly and are therefore explicitly not cryptographically secure. Without a seed the browser's cryptographically secure generator draws them.
For how randomness is produced across the whole site, see how Generate Random works.
Limitations and good to know
- Binary heaps only — d-ary heaps, pairing heaps and Fibonacci heaps have different trade-offs and are not offered.
- Values are distinct, so ties never arise.
- Deletion of an arbitrary element, which needs a position index, is not shown.
- The tree is listed by level rather than drawn with edges.
- Heaps are not stored between visits; seed one you want again.
Privacy and your data
The heap is built and drained entirely in your browser. Nothing about the values or your seed is transmitted or kept.
Related generators
- Binary Search Tree GeneratorAn insertion order and the tree it builds, with all three traversals, the height, and whether it came out balanced.
- Sorting Algorithm Trace GeneratorAn array, an algorithm, and the state after every pass, with the comparison and move counts that distinguish the three.
- Trie GeneratorPrefix trees over words chosen to overlap, with the storage saving measured and the contents checked in both directions.
- Sparse Matrix GeneratorMatrices with most cells zero, in both dense and compressed-sparse-row form, with the storage comparison stated honestly in both directions.