Sub topic 4
Recursion Trees: Summing Work Level by Level
This chapter assumes you can read Big-Theta notation, sum a geometric series, and apply log identities such as a^(logb n) = n^(logb a). It also builds on the previous chapter, where you unrolled a recurrence into a sum.
Unrolling works well when each call makes one recursive call. When a call makes two, three, or eight calls, the algebra gets messy fast. A recursion tree organizes that algebra as a picture. Each node is one call. The node is labeled with the non-recursive work that call does, and its children are the calls it makes. To get the total running time, you add up every label in the tree. The easiest way to do that is one level at a time.
Building the Tree for Merge Sort
Merge sort satisfies T(n) = 2T(n/2) + n, with T(1) = 1. The root does n work to merge, then makes two calls on size n/2. Each of those does n/2 work and makes two calls on size n/4, and the pattern continues down the tree.
To sum a recursion tree, answer four questions:
- How many nodes are at depth i? Each node has a children, so there are a^i nodes. Here a = 2.
- What size problem does a depth-i node handle? The size shrinks by a factor b at each level, so it is n/b^i. Here b = 2.
- What does level i cost? Multiply the node count by the per-node work: a^i · f(n/b^i).
- How deep is the tree? Sizes reach 1 when n/b^i = 1, which is at i = log_b n.
For merge sort, level i costs 2^i · (n/2^i) = n. The internal levels run from i = 0 to i = log₂ n − 1, so there are log₂ n of them. The leaf level has 2^(log₂ n) = n leaves, each costing T(1) = 1. Adding everything up:
T(n) = n · log₂ n + n = Θ(n log n).
Checking the Sum Numerically
A tree is only as good as your counting, so it pays to check the result against the recurrence directly. For powers of two, T(n) should equal exactly n log₂ n + n.
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: 1.25 at n = 16, then 1.125, 1.0833, 1.0625, and 1.05. It approaches 1, which confirms the Θ(n log n) bound.
You can also print the cost of each level for any recurrence of the form aT(n/b) + f(n). This is the most direct way to see the shape of a tree.
def level_costs(a, b, f, n):
"""Per-level costs of the tree for T(n) = a*T(n/b) + f(n), T(1) = 1.
Assumes n is a power of b. The last entry is the leaf level."""
costs = []
size, nodes = n, 1
while size > 1:
costs.append(nodes * f(size))
nodes *= a
size //= b
costs.append(nodes) # leaves, each costing T(1) = 1
return costs
print(level_costs(2, 2, lambda m: m, 16)) # [16, 16, 16, 16, 16]
print(level_costs(3, 4, lambda m: m * m, 4 ** 5)) # shrinks by 3/16 per level
print(level_costs(4, 2, lambda m: m, 2 ** 5)) # doubles per levelThe Three Shapes a Tree Can Take
Once you know the level costs, the level sum is usually a geometric series with ratio r. That ratio sets the shape of the tree.
Decreasing levels: the root dominates
Take T(n) = 3T(n/4) + n². Level i has 3^i nodes, each of size n/4^i, so it costs 3^i · (n/4^i)² = (3/16)^i · n². The ratio is 3/16 < 1, so the series converges:
Σ (3/16)^i · n² ≤ n² · 1/(1 − 3/16) = (16/13) n².
The leaf level has 3^(log₄ n) = n^(log₄ 3) ≈ n^0.79 leaves, which is smaller than n². The total is Θ(n²), and the root alone already does a constant fraction of it.
Equal levels: every level counts
Merge sort is the balanced shape. Each level costs the same, so the total is (cost per level) × (number of levels), which is n · Θ(log n).
Increasing levels: the leaves dominate
Take T(n) = 4T(n/2) + n. Level i costs 4^i · n/2^i = 2^i · n, so the cost doubles at every level. In an increasing geometric series, the last term dominates. The last term is the leaf level, with 4^(log₂ n) = n² leaves. The total is Θ(n²), even though the root does only n work.
| Recurrence | Level i cost | Ratio r | Dominant part | Total |
|---|---|---|---|---|
| 3T(n/4) + n² | (3/16)^i n² | 3/16 | Root | Θ(n²) |
| 2T(n/2) + n | n | 1 | All levels equally | Θ(n log n) |
| 4T(n/2) + n | 2^i n | 2 | Leaves | Θ(n²) |
Uneven Splits
Recursion trees still work when the subproblems have different sizes, and this is where they beat formula-based methods. Take T(n) = T(n/3) + T(2n/3) + n.
- Every full level costs at most n, because the sizes of the nodes on a level add up to at most n.
- The longest root-to-leaf path follows the 2n/3 branch. It has length log_{3/2} n.
- The shortest path follows the n/3 branch. It has length log₃ n.
The upper bound is at most n work per level over log_{3/2} n levels, which gives O(n log n). For the lower bound, the first log₃ n levels are complete and each costs exactly n, which gives Ω(n log n). Together these give Θ(n log n). The tree is lopsided because branches on the n/3 side end early. That is why you bound it from both sides instead of computing an exact sum.
Common Errors
- Miscounting depth. The depth is logb n, where b is the factor by which the size shrinks. It is not loga n. In T(n) = 4T(n/2) + n, the depth is log₂ n, not log₄ n.
- Multiplying the wrong things. The cost of a level is (node count) × f(size of one node). It is not f(total size at that level) unless f is linear.
- Summing a geometric series as if it had a constant number of terms when r > 1. With r > 1, the sum is Θ(last term), and the last term depends on n.
- Treating the tree as a proof. A tree drawn with n/b in place of ⌊n/b⌋, and with "..." in the middle, gives a reliable guess. To make it rigorous, prove the guess by induction. That is the substitution method, covered in the next chapter.
Exercise
Use a recursion tree to solve T(n) = 2T(n/2) + n². Give the cost of level i, the ratio between levels, and the final bound.
Show answer
Level i has 2^i nodes of size n/2^i, so it costs 2^i · (n/2^i)² = n²/2^i. The ratio is 1/2, so the levels shrink and the root dominates. The sum is at most 2n², and the leaves contribute only n. T(n) = Θ(n²).
Exercise
Solve T(n) = 8T(n/2) + n² with a recursion tree. How many leaves are there, and which part of the tree dominates?
Show answer
Level i costs 8^i · (n/2^i)² = 2^i · n², so the cost doubles at each level. The leaf count is 8^(log₂ n) = n^(log₂ 8) = n³. The leaves dominate, so T(n) = Θ(n³).
Exercise
For T(n) = T(n/4) + T(3n/4) + n, find the depth of the shortest and longest root-to-leaf paths, and state a tight bound.
Show answer
The shortest path follows the n/4 branch and has depth log₄ n. The longest follows the 3n/4 branch and has depth log_{4/3} n. Each full level costs n, and no level costs more than n. The tree has Θ(log n) levels in both cases, so T(n) = Θ(n log n).