Max-Heapify A Binary Tree
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
max-heapify is the operation that repairs one broken node in a max-heap by pushing it downward until the heap property is restored. Although heaps are drawn as binary trees, most real implementations store them in arrays because the parent-child relationships are easy to compute from indices.
What max-heapify assumes
max-heapify does not turn an arbitrary tree into a heap in a single call. It assumes the left and right subtrees already satisfy the max-heap rule and only the current node may be too small.
For a zero-based array heap:
- left child is at
2 * i + 1 - right child is at
2 * i + 2 - parent is at
(i - 1) // 2
The heap rule is simple: every parent value must be greater than or equal to its children.
Iterative implementation
An iterative version is efficient and avoids recursion depth concerns.
This compares the node at index i with its children, swaps with the larger child if needed, and repeats until the node reaches a valid position.
Recursive version
Textbooks often show the recursive form because it mirrors the tree definition directly.
Both versions run in O(log n) because the element can move down only one level at a time and a heap has logarithmic height.
Build a full max-heap from an array
To heapify an entire array, call max_heapify from the last non-leaf node back to the root.
This bottom-up construction runs in O(n), which is better than repeatedly inserting elements one by one.
Why heaps are usually arrays, not pointer trees
People often say "heapify a binary tree," but heaps are meant to be complete binary trees. Arrays represent complete trees naturally without storing explicit pointers.
If you literally have a node-based binary tree, you can still apply the same comparison-and-swap idea, but keeping the structure complete becomes awkward. In practice, when the goal is a heap, array storage is the standard representation.
Where max-heapify is used
You use this operation whenever the root of a valid heap becomes suspect. Common examples include:
- heap sort after swapping the root with the last element
- priority queues after removing the maximum
- rebuilding a heap after replacing the root value
For example, removing the maximum from a heap usually means:
- move the last element to the root
- shrink the heap size
- call
max_heapifyon index0
That sequence restores the heap property efficiently.
Common Pitfalls
The biggest mistake is assuming one call to max_heapify can convert any random array into a max-heap. It cannot. The function assumes the child subtrees are already heaps.
Another issue is mixing zero-based and one-based formulas. Many textbook formulas use left = 2i and right = 2i + 1, which apply to one-based indexing, not Python or C++ arrays indexed from zero.
Developers also confuse heaps with binary search trees. A heap guarantees that each parent dominates its children, but it does not keep the entire structure sorted left to right.
Finally, be careful with heap_size. During heap sort, the array length stays the same while the active heap shrinks. Passing the full array length after each extraction breaks the algorithm.
Summary
- '
max-heapifyfixes one out-of-place node by moving it downward.' - It assumes both child subtrees already satisfy the max-heap property.
- In zero-based arrays, children of index
iare at2 * i + 1and2 * i + 2. - A single call is
O(log n), while full bottom-up heap construction isO(n). - Heaps are usually stored as arrays because complete binary trees map to arrays naturally.

