Recurrence Relations and the Recursion Tree Method
Counting loop iterations works for loops. For a recursive function, the cost of a call depends on the cost of the smaller calls it makes, so the natural description is an equation that refers to itself: a recurrence relation. This guide shows how to read a recurrence off recursive code, how to solve it by drawing a recursion tree and summing the work level by level, and how the shape of the tree tells you which part of the computation dominates.
Writing a recurrence from code
Let T(n) be the running time on an input of size n. Look at the function and ask three questions: how many recursive calls does it make, how big is each one, and how much work does it do outside those calls?
- Binary search compares with the middle item, then searches one half. One call on n/2 items plus constant work: T(n) = T(n/2) + O(1).
- Merge sort sorts two halves, then merges them in linear time: T(n) = 2T(n/2) + O(n).
- Naive Fibonacci computes
fib(n - 1) + fib(n - 2): T(n) = T(n - 1) + T(n - 2) + O(1).
Every recurrence also needs a base case, usually T(1) = O(1): an input of constant size takes constant time. It rarely changes the answer, so it is often left implicit. When n is not a power of 2, the halves are really floor(n/2) and ceil(n/2); for these recurrences the rounding does not change the Theta bound, so analyses assume n is a power of 2.
Examples use Python 3.10 or later and only the standard library. Here is merge sort with a counter for the number of calls, so you can see the shape of 2T(n/2) + O(n) directly. The loop that builds merged is the O(n) term; the two recursive calls are the 2T(n/2).
calls = 0
def merge_sort(items):
global calls
calls += 1
if len(items) <= 1:
return items
mid = len(items) // 2
left = merge_sort(items[:mid])
right = merge_sort(items[mid:])
merged, i, j = [], 0, 0
while i < len(left) and j < len(right):
if left[i] <= right[j]:
merged.append(left[i])
i += 1
else:
merged.append(right[j])
j += 1
return merged + left[i:] + right[j:]
print(merge_sort([5, 2, 8, 1, 9, 3, 7, 4]), calls)
Output (the sorted list, then the number of calls):
[1, 2, 3, 4, 5, 7, 8, 9] 15
Eight items produce 15 calls: one on 8 items, two on 4, four on 2 and eight on 1. Those are the nodes of the recursion tree.
The recursion tree method
For a recurrence of the form T(n) = aT(n/b) + n^d, draw the calls as a tree. The root is the original problem. Each node has a children, each on a problem b times smaller. Then:
- at level L there are a^L subproblems;
- each has size n / b^L and does (n / b^L)^d work outside its own recursive calls;
- so level L does a^L x (n / b^L)^d = n^d x (a / b^d)^L work in total;
- the tree has log_b n + 1 levels, from size n down to size 1.
The total cost is the sum of the level totals. Since each level is the previous one multiplied by r = a / b^d, that sum is a geometric series, and a geometric series behaves in one of three ways: it shrinks, stays flat or grows. Those three behaviors are the whole story.
Three worked trees
The numbers below come from the Recursion Tree Calculator, which lists levels 0 to floor(log_b n) and the work on each for given a, b, d and n. Each example uses n = 8 and b = 2, so there are four levels (sizes 8, 4, 2, 1). The calculator's totals are counts for that particular n, not Theta bounds; the bound comes from the pattern.
a = 2, b = 2, d = 1 (merge sort): every level equal
Level 0: one problem of size 8 does 8. Level 1: two of size 4 do 8. Level 2: four of size 2 do 8. Level 3: eight of size 1 do 8. Total 32. Here r = 2 / 2 = 1, so every level does n work and there are log2 n + 1 levels: Theta(n log n). Doubling to n = 16 gives five levels of 16, total 80.
a = 4, b = 2, d = 1: the leaves dominate
Levels do 8, 16, 32, 64; total 120. Here r = 4 / 2 = 2, so each level doubles the last, and the bottom level alone is more than half the total. The bottom level has a^(log_b n) = n^(log_b a) leaves, here n^2 = 64. The answer is Theta(n^2).
a = 2, b = 2, d = 2: the root dominates
Levels do 64, 32, 16, 8; total 120. Here r = 2 / 4 = 1/2, so each level halves the last, and the root alone is more than half the total. The answer is the root's work, Theta(n^2).
This script reproduces the three trees:
def tree_levels(a, b, d, n):
"""Work per level of the tree for T(n) = a T(n/b) + n^d, n a power of b."""
rows = []
level, size = 0, n
while size >= 1:
rows.append(a ** level * size ** d)
level += 1
size //= b
return rows
for a, b, d in [(2, 2, 1), (4, 2, 1), (2, 2, 2)]:
rows = tree_levels(a, b, d, 8)
print(f"a={a} b={b} d={d}:", rows, "total", sum(rows))
Output:
a=2 b=2 d=1: [8, 8, 8, 8] total 32
a=4 b=2 d=1: [8, 16, 32, 64] total 120
a=2 b=2 d=2: [64, 32, 16, 8] total 120
The two totals of 120 are a coincidence of n = 8. At larger n the a = 4 tree grows like n^2 because of its leaves and the d = 2 tree grows like n^2 because of its root; the same total does not mean the same tree.
How the shapes match the master theorem
The three behaviors of the series are exactly the three cases of the master theorem, which compares log_b a with d (equivalently, r = a / b^d with 1):
- Leaves dominate (r > 1, log_b a > d): Theta(n^(log_b a)). Case 1.
- All levels equal (r = 1, log_b a = d): Theta(n^d log n). Case 2.
- Root dominates (r < 1, log_b a < d): Theta(n^d). Case 3.
Binary search is the simplest Case 2 tree: a = 1, b = 2, d = 0, a single chain of nodes each doing constant work. With n = 1024 the calculator shows 11 levels of work 1, total 11: Theta(log n). The Master Theorem Calculator gives the case and bound directly for any a, b and d; the recursion tree shows you why.
Trees that are not this shape
Naive Fibonacci does not split by a constant factor. Its tree has two children per node, one of size n - 1 and one of size n - 2, so branches have different depths. Two easy bounds: every node has at most 2 children and the depth is at most n, so there are fewer than 2^(n+1) nodes, which gives O(2^n). But the left-most path has depth n - 1 while the right-most path, which drops by 2 at each step, has depth only about n/2, so the tree is lopsided and the tight answer is smaller: Theta(phi^n), where phi = (1 + sqrt 5) / 2 is about 1.618. In fact the number of calls is exactly 2F(n + 1) - 1, where F is the Fibonacci sequence, which is why fib(20) makes 21,891 calls (see the recursion guide).
The master theorem does not apply to this recurrence, but the recursion tree still gives a bound. That is its main advantage: it works for any recurrence you can draw.
The substitution method, briefly
A recursion tree gives a guess; the substitution method proves it. Guess a bound, assume it holds for smaller inputs, and show it holds for n. For merge sort, guess T(n) <= c n log2 n. Assuming T(n/2) <= c (n/2) log2(n/2), we get T(n) <= 2 c (n/2)(log2 n - 1) + n = c n log2 n - c n + n, which is at most c n log2 n whenever c >= 1. Check a base case such as n = 2 with a suitable c, and induction finishes the proof. The common mistake is to "prove" T(n) <= cn + n and call it O(n): the constant must stay the same c, not grow.
Check yourself
- Write the recurrence for a function that makes three calls on n/3 items and does O(n) extra work. Which level dominates? (T(n) = 3T(n/3) + O(n); r = 1, all levels equal, Theta(n log n).)
- Why does the calculator report a bottom size of 1.25 for n = 10 and b = 2? (10 is not a power of 2: 10 / 2^3 = 1.25. Use n as a power of b.)