Sub topic 4
Recursion Trees: Summing Work Level by Level
This chapter assumes you know Big-Theta notation, can sum a geometric series, and have seen how to derive a recurrence from recursive code. Unrolling works well when each call makes only one recursive call. When a call branches into several subproblems, the substitutions get hard to follow. A recursion tree lays the same expansion out as a picture.
The working model
A recursion tree has one node per recursive call. Each node is labeled with the non-recursive work that call does, which is the f(n) term at that call's input size. Its children are the subproblems it creates. The total running time is the sum of all node labels. You compute that sum in three steps:
- Find the cost of each level: the number of nodes at depth i times the work per node.
- Find the depth of the tree: the number of times you divide n by b to reach the base case.
- Add the level costs, and count the leaves separately.
Worked example: merge sort
Merge sort satisfies T(n) = 2T(n/2) + n, with T(1) = 1.
Depth i has 2^i nodes, and each one costs n/2^i, so every level costs n. The tree reaches size 1 after log₂ n levels, and the bottom level has 2^(log₂ n) = n leaves. Adding these up:
T(n) = n · log₂ n + n = Θ(n log n)
You can check the closed form by evaluating the recurrence directly:
import math
def T(n):
# Merge sort's recurrence with T(1) = 1 and f(n) = n
return 1 if n <= 1 else 2 * T(n // 2) + n
for p in range(4, 21, 4):
n = 2 ** p
print(n, T(n), round(T(n) / (n * math.log2(n)), 4))The ratio printed in the last column is 1 + 1/log₂ n. It falls from 1.25 toward 1, which matches the Θ(n log n) bound.
A tree whose levels shrink
Take T(n) = 3T(n/4) + n². Depth i has 3^i nodes, and each costs (n/4^i)², so level i costs (3/16)^i · n². That is a decreasing geometric series, so its sum is at most n² / (1 − 3/16) = (16/13)n². There are 3^(log₄ n) = n^(log₄ 3) ≈ n^0.79 leaves, which is less than n². The root's work dominates, and T(n) = Θ(n²).
Three level-cost patterns
| Level costs | Sum is dominated by | Result |
|---|---|---|
| Constant across levels | All levels equally | f(n) · log n |
| Decreasing geometrically | The root | Θ(f(n)) |
| Increasing geometrically | The leaves | Θ(number of leaves) |
These three patterns are the three cases of the master theorem, which a later chapter states formally.
Common errors
- Counting the leaves wrong. The tree has a^(logb n) = n^(logb a) leaves, not n. They match only when a = b.
- Forgetting the leaf level. When the level costs grow, the leaves make up most of the total.
- Treating the result as a proof. A tree whose sizes don't divide evenly gives you a good guess. You prove that guess with the substitution method, covered in the next chapter.
Exercise
Use a recursion tree to find a tight bound for T(n) = 4T(n/2) + n.
Show answer
Depth i has 4^i nodes, each costing n/2^i, so level i costs 2^i · n. The level costs grow geometrically, so the leaves dominate. There are 4^(log₂ n) = n² leaves, so T(n) = Θ(n²).
Exercise
For T(n) = 2T(n/2) + n², what does level i cost, and what is the overall bound?
Show answer
Level i costs 2^i · (n/2^i)² = n²/2^i. That series shrinks geometrically and sums to at most 2n². The n leaves cost less than that, so T(n) = Θ(n²).