← Back

Binary Heap Simulator

What is a Binary Heap?

A binary heap is a complete binary tree stored compactly in an array. In a max heap, every parent is greater than or equal to its children. In a min heap, every parent is less than or equal to its children.

Peek rootO(1)
InsertO(log n)
Extract rootO(log n)
Build heapO(n)

Heap operations

400 ms

Array representation

Example max heap loaded. Choose an operation.

Heap size0
Height0
Comparisons0
Swaps0

Current operation path

Heap Sort result

Array index relationships

parent(i)     = floor((i - 1) / 2)
leftChild(i)  = 2i + 1
rightChild(i) = 2i + 2

insert: append, then sift up
extract: replace root, then sift down

Heap concepts

Complete binary tree

Every level is full except possibly the last, which is filled from left to right. This allows compact array storage.

Sift up

After insertion, swap the new value with its parent while the heap property is violated.

Sift down

After extraction, move the last value to the root and repeatedly swap it with the more appropriate child.

Not globally sorted

A heap guarantees parent–child order only. The array is not sorted, but the root always has the highest or lowest priority.

Complexity summary

OperationTimeReason
Peek rootO(1)The root is at index 0.
InsertO(log n)Sift-up follows one path.
Extract rootO(log n)Sift-down follows one path.
Build heapO(n)Bottom-up construction has linear aggregate cost.
Heap SortO(n log n)The root is extracted n times.