Sub topic 1
Complete Binary Trees: The Shape Behind a Heap
A heap is a binary tree that obeys two rules. One rule is about order: how parent values relate to child values. The other rule is about shape, and the shape has to be a complete binary tree. This chapter covers the shape rule. The array storage, the heap property and the sift-down operation in later chapters all depend on it.
Prerequisites
This chapter assumes you can:
- Read and write basic Python functions and loops.
- Recognize binary tree terms: root, parent, child, leaf, level, height.
- Use Big-O notation, especially O(log n).
What "complete" means
A binary tree is complete when it meets both of these conditions:
- Every level is full except possibly the last level.
- The nodes on the last level sit as far left as possible, with no gaps between them.
You can also think of it as a fill order. You add nodes level by level, and within each level you go from left to right. Any tree you can build this way is complete, and any other tree is not.
This tree is complete. Levels 0 and 1 are full. Level 2 holds three nodes, packed from the left. If F were C's right child instead of its left child, the tree would have a gap and would not be complete.
Complete vs. full vs. perfect
These three terms get mixed up often, and they mean different things:
| Term | Rule | Is the six-node tree above an example? |
|---|---|---|
| Full | Every node has 0 or 2 children | No: C has one child |
| Perfect | Every level is completely filled | No: level 2 has 3 of 4 slots |
| Complete | All levels full except the last, which is filled left to right | Yes |
Every perfect tree is complete. A full tree does not have to be complete, and a complete tree does not have to be full.
Why heaps need this shape
The shape rule gives heaps two useful guarantees.
The height stays small. For a given number of nodes, a complete tree is as short as a binary tree can be. A complete tree with n nodes has height ⌊log₂ n⌋. Sift-down moves a value along a single root-to-leaf path, so it costs O(log n). That bound is where heap sort's O(n log n) running time comes from.
The tree has no holes. Because nodes fill in a fixed order, each node has a single numbered position: 0, 1, 2, and so on. The next chapter turns these positions into array indices, which means the tree can be stored without any pointers.
The code below computes the height and the number of nodes on each level for a complete tree of n nodes:
def height(n):
# Height of a complete binary tree with n >= 1 nodes.
return n.bit_length() - 1
def level_sizes(n):
# Number of nodes on each level, top to bottom.
sizes = []
capacity = 1
while n > 0:
sizes.append(min(capacity, n))
n -= capacity
capacity *= 2
return sizes
for n in [1, 6, 7, 8, 10]:
print(n, height(n), level_sizes(n))
# 1 0 [1]
# 6 2 [1, 2, 3]
# 7 2 [1, 2, 4]
# 8 3 [1, 2, 4, 1]
# 10 3 [1, 2, 4, 3]The height only goes up when n reaches a power of two. That happens when one extra node starts a new level.
Common mistakes
- Treating a heap like a binary search tree. A heap does not order left children against right children. It constrains only the shape and the relationship between each parent and its children.
- Off-by-one errors in height. Height counts edges, so a single node has height 0, not 1.
Exercise
A complete binary tree has 12 nodes. What is its height, and how many nodes are on its last level?
Show answer
The height is ⌊log₂ 12⌋ = 3. Levels 0 to 2 hold 1 + 2 + 4 = 7 nodes, which leaves 12 − 7 = 5 nodes on level 3.
Exercise
A tree has a root with two children, B and C. B has no children, and C has a left child. Is this tree complete?
Show answer
No. The last level has a node under C while the slots under B are empty. That leaves a gap on the left, so the tree breaks the left-to-right fill rule.