Data structures

Binary Heap / Priority Queue

Insert values and extract the maximum from a max-heap. The array and tree views update together, showing bubble-up and bubble-down.

5 minLevel 2/3The interactive stage stays ad-free.
Size7Height3Max90Last actionReady
90708040605030
090170280340460550630

The array is the tree in level order. After every insert or extract, the highlighted nodes show the bubble-up or bubble-down path that restores the max-heap property.

What you are seeing

1

A binary heap is a complete tree stored in an array: children of i are at 2i+1 and 2i+2.

2

Insert appends at the end, then bubbles up while the new value is larger than its parent.

3

Extract removes the root, moves the last element to the top, and bubbles down to restore the heap property.

Why it matters

Priority queues power Dijkstra, A*, task schedulers, event simulation, and top-k queries.

Complexity & details

Insert: O(log n). Extract max: O(log n). Peek max: O(1). Build heap: O(n).

Frequently asked questions

Is a heap fully sorted?+

No. A heap only guarantees that every parent is at least as large as its children. The array is not sorted.

Why store a heap in an array?+

A complete tree has no gaps, so index arithmetic replaces pointers, improving cache locality and memory use.