Sub topic 9
Multiple Recursive Calls and Tree Recursion
This chapter assumes you can trace a single recursive call through the call stack and that you're comfortable with the base case and recursive case. Up to now, each recursive case has made one call to itself. Here you will see what happens when a function makes two or more.
When One Call Becomes Two
Tree recursion is a pattern where a function calls itself more than once in its recursive case. Every call can branch into several more calls, so the calls form a tree instead of a straight chain.
The Fibonacci sequence is the standard example. Each number is the sum of the two before it, which leads straight to two recursive calls:
def fib(n):
if n < 2:
return n
return fib(n - 1) + fib(n - 2)
print(fib(10)) # 55The base case covers n equal to 0 or 1. The recursive case makes progress because both n - 1 and n - 2 are smaller than n.
The Call Tree
You can't draw tree recursion as a single column of stack frames. A call tree shows it better. Each node is one call, and its children are the calls it makes.
Python runs this tree depth first. fib(4) calls fib(3), and that whole left subtree must finish before fib(2) on the right begins. The stack never holds more frames than the tree's height, about n. The total number of calls is far larger than that.
Counting the Work
A counter lets you measure how many calls actually happen:
calls = 0
def fib(n):
global calls
calls += 1
if n < 2:
return n
return fib(n - 1) + fib(n - 2)
for n in [5, 10, 20, 25]:
calls = 0
fib(n)
print(n, calls)
# 5 15
# 10 177
# 20 21891
# 25 242785The count grows exponentially, by roughly a factor of 1.6 each time n goes up by one. The cause is repeated subproblems. fib(2) is computed from scratch every time it shows up in the tree, and nothing is remembered between calls.
| Property | Linear recursion (factorial) | Tree recursion (fib) |
|---|---|---|
| Calls per recursive case | 1 | 2 |
| Maximum stack depth | about n | about n |
| Total calls | n + 1 | exponential in n |
Common Errors
- Each branch needs its own path to a base case. If
fibchecked onlyn == 0, the callfib(1)would callfib(-1)and never stop. - Stack depth and running time are different measurements. A tree-recursive function can be shallow enough to run without a
RecursionErrorand still take minutes.
The repeated subproblems in fib can be removed by caching results. The chapter on memoization covers how.
Exercise
When computing fib(5) with the naive function, how many times is fib(2) called, and how many calls are made in total?
Show answer
fib(2) is called 3 times: once inside fib(4) → fib(3), once directly inside fib(4), and once inside fib(3) called from fib(5). The total is 15 calls, because calls(n) = 1 + calls(n-1) + calls(n-2), which gives 1 + 9 + 5 = 15.
Exercise
A function count(n) returns 1 if n <= 0 and otherwise returns count(n - 1) + count(n - 1) + count(n - 1). What value does count(3) return, and what kind of growth does this show?
Show answer
27. Each level makes three calls, so count(n) = 3^n. The calls form a tree with branching factor 3, and the total work grows exponentially.