Priority Queues · Complete Binary Trees

Binary Heap: Tree & Array

Explore an existing heap or build one operation at a time. During insertion, watch the new value enter at the array tail and percolate up. During removal, watch the root leave, the tail move to the root, and that value percolate down. The tree and array views stay synchronized at every step.

Set up

Choose an operation

Step through

Begin prepares an operation. Step performs one append, comparison, swap, or removal action. Clear heap removes all values; Generate random heap starts a new example.

Heap size: 0
Heap height: 0
Root priority: —
Operation: Ready

Array representation

Generate a random heap or begin an Add operation to get started.

The array is level-order traversal of the tree. Each Step performs one visible action: append, compare, swap, or finish the percolation.

Tree representation

100%
Value being compared or movedParent or selected child

For array index i, the children are at 2i + 1 and 2i + 2; the parent is at ⌊(i − 1) / 2⌋.

An empty heap satisfies the heap property.