What this shows
A real binary heap — insert a value, extract the min or max, or peek at the root, and watch every comparison and swap that keeps the heap property intact. Switch between a min-heap and a max-heap to see the exact same algorithm with the comparison direction flipped, and watch both the tree and its underlying array update together, since a heap is really one data structure wearing two representations at once.
Why a heap is just an array wearing a tree costume
Unlike the Binary Search Tree Visualizer, which uses real left/right pointers, a heap needs none — its tree is always complete (every level full except possibly the last, filled left to right), which means a node's position alone determines where its children live. For a node at array index i, its children sit at 2i + 1 and 2i + 2, and its parent sits at ⌊(i − 1) / 2⌋ — no pointers required, just arithmetic. That's why the array view below the tree isn't a bonus visualization, it's the actual, more common way a heap gets implemented.
What insert and extract actually do
Insert appends the new value at the end of the array (the next open slot in a complete tree), then sifts it up: compare with its parent, swap if it's out of order, and repeat until it either reaches the root or lands under a parent that already satisfies the heap property. Extract does the mirror image: pull out the root (the min or max — the only value a heap ever exposes), move the last element into its place to keep the tree complete, then sift it down by repeatedly swapping with the smaller (min-heap) or larger (max-heap) of its two children until it settles. Both operations touch at most one path from root to leaf, which is exactly why they cost O(log n) — bounded by height, just like BST search, but on a tree that's always balanced by construction instead of by luck.
Why peek is free but extract isn't
The whole point of a heap is that the value you care about — the minimum or the maximum — is always sitting at index 0. Peek is O(1): just read it, no comparisons needed. Removing it is the expensive part, because the tree has to stay complete and heap-ordered afterward, which is exactly the sift-down this tool traces step by step rather than skipping to the result.
FAQ
Why does switching heap type reset the heap?
A min-heap array and a max-heap array holding the same values are generally not the same array — flipping the comparison direction without re-heapifying would leave the tree violating the very property it's supposed to demonstrate, so switching type reseeds a heap that's valid for the newly selected type.
What's a real use case for a heap?
Priority queues — task schedulers, Dijkstra's shortest-path algorithm (see the Shortest Path Visualizer), and the top-k-elements family of interview problems all lean on a heap specifically because it gives O(log n) insert/extract while keeping O(1) access to the current best value.
How is this different from the Binary Search Tree Visualizer?
A BST keeps everything ordered — every left subtree smaller, every right subtree larger — which is what makes in-order traversal produce sorted output. A heap only enforces a much weaker rule (a parent beats both its children), which is cheaper to maintain but only ever guarantees fast access to one end of the ordering, not the whole thing. See Hash Table Visualizer for a third structure entirely — no ordering at all, traded for average O(1) lookup.
Is anything I enter here sent anywhere?
No — every operation runs entirely in your browser. Nothing here is ever sent to a server.