Sub topic 7
Drawing Recursion Trees to Count Work
This chapter assumes you can read recursive Python functions, understand base and recursive cases, and know basic Big-O notation. You saw how the call stack runs one call at a time. A recursion tree shows all the calls at once, which makes it the standard tool for counting how much work a recursive function does.
What a Recursion Tree Is
A recursion tree has one node for each function call. The root is the first call. A node's children are the calls that node makes directly. Leaves are calls that hit a base case and make no further calls.
To count total work:
- Draw (or imagine) the tree.
- Note how much work each node does outside its recursive calls.
- Add that work across all nodes, often level by level.
A Chain: Factorial
factorial(n) makes exactly one recursive call, so its tree is a straight chain: factorial(4) → factorial(3) → factorial(2) → factorial(1) → factorial(0). That is n + 1 nodes. Each node does one comparison and at most one multiplication, so the total work is O(n).
A function that makes one call per level always has a chain-shaped tree. Its cost is the depth times the work per node.
A Branching Tree
Things change when a call makes two or more recursive calls. Consider this function, which makes two identical calls:
def double_calls(n):
if n == 0:
return 1
return double_calls(n - 1) + double_calls(n - 1)
print(double_calls(3)) # 8Here is the tree for double_calls(3):
Counting Level by Level
Group the nodes by depth:
| Level | Argument | Nodes at level |
|---|---|---|
| 0 | n | 1 |
| 1 | n − 1 | 2 |
| 2 | n − 2 | 4 |
| k | n − k | 2^k |
| n | 0 | 2^n |
The total is 1 + 2 + 4 + … + 2^n = 2^(n+1) − 1. Each node does constant work, so double_calls(n) runs in O(2^n) time. The tree is only n levels deep, though, so the stack never holds more than n + 1 frames. Time follows the number of nodes in the tree. Stack space follows its height.
Checking the Count in Code
You can confirm a count from a tree by instrumenting the function:
calls = 0
def double_calls(n):
global calls
calls += 1
if n == 0:
return 1
return double_calls(n - 1) + double_calls(n - 1)
for n in range(6):
calls = 0
double_calls(n)
print(n, calls, 2 ** (n + 1) - 1)Each row prints the same number in the last two columns. That confirms the formula from the tree.
Common Mistakes
- Counting only the depth. Depth gives stack usage, not running time. A shallow tree can still be very wide.
- Ignoring repeated nodes. The tree above contains
d(1)four times. Identical subtrees are wasted work. The next chapters use this observation to speed up Fibonacci. - Forgetting per-node work. If each node loops over its input, the total is the sum of that work over all nodes, not just the node count.
Exercise
A function g(n) returns 1 when n == 0 and otherwise returns g(n - 1) + g(n - 1) + g(n - 1). How many calls does g(3) make in total, and what is its Big-O running time?
Show answer
Level k has 3^k nodes, so g(3) makes 1 + 3 + 9 + 27 = 40 calls. In general there are (3^(n+1) − 1) / 2 calls, which is O(3^n) time, while stack depth stays O(n).
Exercise
Why does factorial(n) use O(n) time while double_calls(n) uses O(2^n), even though both have recursion depth n?
Show answer
factorial makes one recursive call per node, so its tree is a chain of n + 1 nodes. double_calls makes two calls per node, so the number of nodes doubles at each level and the total is 2^(n+1) − 1.