Heaps
Oct 17, 2026
Heaps are great data structures that are very useful in many algorithms, specifically those that involve priority queues. They are complete binary trees that maintain a “heap property”: no parent can be larger (min heap) or smaller (max heap) than its children. This allows for dynamically sorting values upon pushing and popping values from the heap with each operation taking time, with the root always remaining as the smallest or largest value in the heap.
The Heap Invariant
The actual heap invariant is shockingly simple. For a min heap, a parent must be smaller than or equal to both of its children, and for a max heap, a parent must be greater than or equal to both of its children. Note that since heaps are “special” binary trees, they should not be confused with binary search trees, which hold their own parent-child relationship invariant.
Below are some examples of heaps.
// min heap // max heap
1 9
/ \ / \
/ \ / \
3 6 3 6
/ \ / \ / \ / \
6 7 8 10 1 2 4 5
Complete Binary Trees and Array Representation
A complete binary tree is one where every node on a given level is as “left-shifted” as possible. A consequence of this is that every level except possibly the last will be entirely full of nodes.
On a technicality, a heap does not need to be a complete binary tree. However, being a complete binary tree makes implementing heaps much easier and improves the performance of the data structure.
// complete // not complete
1 1
/ \ /
/ \ /
3 6 3
/ \ / / \
6 7 8 6 7
If a heap is a complete binary tree, then the tree itself can be neatly represented as an array of its nodes, traversed level by level. This introduces some unique properties for the array.
// tree representation
1
/ \
/ \
3 6
/ \ /
6 7 8
// array representation
[1, 3, 6, 6, 7, 8]
Because the heap is a complete binary tree, the following relationships are
guaranteed to hold: for a node at index i, its left child is found at index
2*i+1, its right child is found at index 2*i+2, and its parent is found at
index (i-1)/2, where / is floor division.
In terms of performance, the array representation uses less memory by removing the need to store pointer information, takes advantage of cache locality by simply being an array, removes the need for memory lookups through pointers, and removes the need for dynamic allocation of new nodes, an all-around improvement over the tree representation.
Modifying Heaps
Given that a heap’s properties are maintained, its root is guaranteed to be the smallest or largest value (depending on the heap type) in the entire tree. This allows for an lookup of the current “best” value (a valuable property of priority queues).
To maintain this through insertions and deletions, we introduce the concepts of bubbling (sifting) up and down.
Inserting Values
Value insertions begin by simply appending the new value at the end of the heap array, equivalent to creating a new node after the last node of the complete binary tree. This clearly has the potential to break the heap invariant, so we then bubble this new value up until the heap invariant is satisfied again.
Bubbling a value upwards is nothing more than swapping it with its parent as many times as needed until the heap invariant is satisfied again. Satisfying the direct relationship involving the new value is enough to guarantee that the invariant is held over the entire tree.
Suppose a value v is inserted into a min heap. At any point, its children are
always guaranteed to be larger than v’s current parent p, so swapping v
and p does not break the heap invariant for p. Thus, bubbling a value up
only ever causes at most one invalid parent-child relationship in the tree
(involving v). Once the stopping condition of this relationship being
satisfied, then the tree is again a heap.
Since insertions travel at most up the height of the tree, this is an operation.
Below is an example inserting 0 into the min heap tree shown previously.
// initial insert
1
/ \
/ \
3 6
/ \ / \
6 7 8 0
[1, 3, 6, 6, 7, 8, 0]
// first step
1
/ \
/ \
3 0
/ \ / \
6 7 8 6
[1, 3, 0, 6, 7, 8, 6]
// second step (done)
0
/ \
/ \
3 1
/ \ / \
6 7 8 6
[0, 3, 1, 6, 7, 8, 6]
Since the inserted value is the smallest value of the heap, it correctly became the new root of the heap. Note that although the values appear as if they have shuffled substantially, the heap invariant still holds.
Deleting Values
Deleting (popping) values is very similar to insertion. Instead of appending a value at the end of the heap, the root value is simply removed from the tree, and the last value is moved to the new root, as the heap should still remain a complete binary tree.
To restore the heap invariant, the new root node is bubbled downwards through the tree until the property is satisfied. This operation, like with insertions, takes time.
From the above example, we have the following.
// pop 0, and move last node to root
6
/ \
/ \
3 1
/ \ /
6 7 8
[6, 3, 1, 6, 7, 8]
// move 6 down (done)
1
/ \
/ \
3 6
/ \ /
6 7 8
[1, 3, 6, 6, 7, 8]
In this particular example, the original heap was restored; note that this is simply a coincidence.
Heapifying Arrays
Since heaps can be represented as arrays, arrays can be “heapified” into heaps. Interestingly, this process only takes time, despite each insertion taking time.
One common explanation for this phenomenon lies in Floyd’s heapify algorithm, where every value is bubbled downwards into place, starting from the deepest level of the tree. At most, each node of depth makes swaps downwards. For a tree with total nodes, the number of nodes per layer is at most , each making swaps. Thus, the total number of swaps required on a tree of size is upper bounded by:
where .
Interestingly, this upper bound converges neatly, with several clever ways to show it. One way is through manipulating the infinite series.
First, we show through the ratio test that the sum is finite, since the ratio between terms converges to a value less than 1, and the series converges to a converging geometric series.
Then, by representing the sum be represented as , we have
The th term of the first series can be matched with the th term of the second series, yielding
Thus, the total number of swaps is upper bounded by , and so the heapify operation is a linear-time operation.
This proof can also be approached through a lens of differentiating the standard geometric series, then modifying the derivative into a form that matches the series above.