Afterglowscreensavers

Binary Heap Builder Screensaver

The Binary Heap Builder screensaver inserts real random values into a live binary max-heap, drawn as an actual tree, each one bubbling up into correct heap order. It is free, runs in your browser, and ESC exits.

Live preview

Binary Heap Builder screensaver — A real binary heap, heapifying live as an actual tree.

Press ESC to leave fullscreen. Settings are saved in your browser only.

How the Binary Heap Builder screensaver works

The saver inserts a real random value at the next open leaf position of a binary tree, exactly where a real heap insert places it.

The new value then bubbles upward, swapping with its parent whenever it is larger, until real heap order is restored.

Once the heap fills to a set size, the saver repeatedly pops the real maximum from the root and re-heapifies downward instead.

After the heap fully drains, it holds briefly, then a fresh binary heap build begins from empty.

Heaps: a tree stored in an array

A binary heap is a tree with two rules. It is complete, meaning every level is full except possibly the last, which fills from the left, and it obeys the heap property: in a max-heap, every parent is at least as large as its children. The largest item therefore always sits at the root. J. W. J. Williams introduced the structure in 1964 as a data structure for heapsort.

Because the tree is complete, no pointers are needed. It lives in a plain array, where, counting from zero, the children of the item at index i sit at positions 2i+1 and 2i+2, and its parent at (i-1)/2 rounded down. Inserting appends at the end and swaps upward while the new value beats its parent; removing the root moves the last item to the top and swaps it downward with its larger child. In the worst case each takes O(log n) time, proportional to the tree's height.

Heaps are the usual engine behind priority queues, used in schedulers, event simulations and graph algorithms such as Dijkstra's. They are not fully sorted: only the relationship between parent and children is guaranteed, so siblings and cousins can be in any order, and finding an arbitrary value takes O(n) because the heap is not sorted.

A worked example

A freshly inserted large value climbs several levels up the tree, swapping with each parent in turn, until it settles exactly where real heap order requires it to sit.

Settings & tips

Frequently asked questions

Is this a real binary heap?
Yes — a real, correct binary max-heap, with every insert and pop shown as an actual tree operation.
What is heapify?
The real process of moving a value up or down the tree until the heap-order property is restored.
Is a key needed?
No — it is a pure local computation.
Is it free?
Yes — free, no download, in your browser.
Is a heap the same as a sorted array?
No. A heap only guarantees that each parent beats its children, so the rest of the order is loose. Repeatedly removing the root does yield sorted output.
How is the tree stored without pointers?
Since it is complete, the position of every node's parent and children can be calculated from its array index.
What is a priority queue?
It is a collection from which you always take the most important item first. A heap implements that efficiently.

← All Science & Research screensavers