A binary heap is a complete binary tree where every parent is smaller than its children (a min-heap) or larger (a max-heap), so the smallest or largest value is always at the root. It is the usual way to build a priority queue. This visualizer draws the heap as a tree and, underneath, as the array it is really stored in, so you can see that index i has children at 2i + 1 and 2i + 2.
Insert a value and watch it sift up, swapping with its parent until the heap property holds. Extract the top and watch the last value move to the root and sift down. Build a heap from a random array to see the O(n) bottom-up heapify. Swapped nodes slide to their new places so every move is easy to follow.
How to use the Binary Heap & Priority Queue Visualizer tool
- Choose Min-heap or Max-heap.
- Type a value and press Insert, or press Extract min (or max) to remove the root.
- Press Build heap from random array to watch heapify turn an unordered array into a heap.
- Compare the tree and the array: orange nodes are being compared, red nodes are being swapped.
Why use ToolOrca Binary Heap & Priority Queue Visualizer
- Tree view and array view of the same heap
- Min-heap and max-heap modes
- Insert with sift up, extract with sift down
- Bottom-up build heap (heapify)
- Animated swaps and a swap counter
- Code in C++, Java, Python and JavaScript
How a binary heap works
A heap is a complete binary tree: every level is full except possibly the last, which fills from left to right. That shape lets the tree live in a plain array with no pointers: the root is at index 0, the children of index i are at 2i + 1 and 2i + 2, and its parent is at (i − 1) / 2 rounded down.
The heap property says each parent is ≤ its children in a min-heap (≥ in a max-heap). It does not sort the array; it only guarantees the root is the minimum (or maximum). That weaker rule is what makes updates cheap: the tree height is log₂ n, so insert and extract take O(log n).
Insert, extract and heapify
Insert: put the new value in the next free slot at the end of the array, then sift up: while it is smaller than its parent (min-heap), swap them. Extract min: remove the root, move the last value to the root, then sift down: swap it with its smaller child until neither child is smaller.
Build heap: instead of inserting n values one by one (O(n log n)), sift down every parent from the last one up to the root. Most nodes are near the bottom and move only a little, so the total is O(n). Heap sort uses exactly this, followed by n extractions.
Where priority queues are used
Priority queues pick the most urgent item next: Dijkstra’s shortest path and Prim’s minimum spanning tree, task and event schedulers, A* search, merging k sorted lists, finding the k largest values in a stream and Huffman coding. Use std::priority_queue in C++, PriorityQueue in Java and heapq in Python.
Every visualizer on this site runs in your browser with the same controls: Play, Pause, Step forward, Step back and a speed slider. You can also drag the position slider to jump to any step, or focus the drawing and use the Space bar and arrow keys.
Frequently asked questions
What is the difference between a heap and a priority queue?
A priority queue is the abstract idea (always remove the highest-priority item). A binary heap is the most common data structure used to implement it.
How do you find the children of a node in a heap array?
For the node at index i (starting from 0), the left child is at 2i + 1, the right child at 2i + 2 and the parent at (i − 1) / 2, rounded down.
Is a heap sorted?
No. Only the root is guaranteed to be the minimum (or maximum). Siblings and cousins can be in any order.
Why is building a heap O(n) and not O(n log n)?
Bottom-up heapify sifts each parent down. Half the nodes are leaves and do nothing, a quarter move at most one level, and so on. The sum of these small moves is less than 2n.
How do I make a max-heap with Python heapq?
heapq is a min-heap only. Push negative values (heappush(h, -x)) and negate them again when you pop.