Recursion visualizer
A recursive function solves a problem by calling itself on smaller versions of it. Pick a function and an input, then step through every call and return: the recursion tree grows as calls are made, the call stack shows which ones are still waiting, and memoization shows how much repeated work can be skipped.
function fib(n) {
if (n < 2) return n;
return fib(n - 1) + fib(n - 2);
}Press Step or Play to make the first call.
- Empty: nothing has been called yet
- Calls so far
- 0 of 15
- Deepest stack
- 5 frames
- Different subproblems
- 6
- Result
- 5
How to use the visualizer
Choose a function, type its input, and press Step to make one call or return at a time, or Play to run it. Each box in the tree is one call; it is highlighted while it is on the call stack, and shows its return value once it finishes. The panel on the right lists the stack from the running call down to the first one, each waiting for the call above it.
- For Fibonacci and binomial coefficients, turn on Memoization to see repeated calls answered from a table instead of recomputed.
- Copy answer gives the whole call trace, indented by depth, the way exercises ask for it; Copy link saves the function, input and step.
What is recursion?
A function is recursive when it calls itself. It works when two things are true: there is a base case, an input small enough to answer directly, and every other input is handled by a recursive case that calls the function on something closer to the base case and combines the result.
Factorial is the standard first example: 0! is 1, and n! is n × (n − 1)!. Computing fact(5) calls fact(4), which calls fact(3), and so on down to fact(0), which returns 1. The calls then finish in reverse order, each multiplying by its own n, until fact(5) returns 120. That is 6 calls in all.
The call stack
Each call needs its own copy of its arguments and local variables, and must remember where to continue when it finishes. The program keeps these in a stack frame, and the frames of all calls in progress form the call stack. A call pushes a frame; a return pops it. The stack therefore holds exactly the calls that have started and not yet returned, with the running call on top and every call below it waiting for the one above.
The deepest the stack gets is the recursion's depth, and it decides how much memory the recursion needs. fact(5) reaches 6 frames. Plain Fibonacci makes far more calls but only goes as deep as its longest chain, n frames for fib(n): fib(30) makes 2,692,537 calls but never has more than 30 frames on the stack at once. A recursion that is too deep exhausts the space reserved for the stack, which is a stack overflow.
Recursion trees and counting calls
Drawing each call as a node, with an edge to every call it makes, gives the recursion tree. Factorial's tree is a single chain. Fibonacci's branches twice at every call that is not a base case, because fib(n) needs both fib(n − 1) and fib(n − 2), so the number of calls grows exponentially: it is 2F(n + 1) − 1, where F(n) is the nth Fibonacci number.
| n | fib(n) | Calls, plain | Calls, memoized | Deepest stack |
|---|---|---|---|---|
| 5 | 5 | 15 | 9 | 5 |
| 10 | 55 | 177 | 19 | 10 |
| 20 | 6,765 | 21,891 | 39 | 20 |
| 30 | 832,040 | 2,692,537 | 59 | 30 |
The tree also shows why the plain version is slow: the same subproblem appears again and again. In fib(5)'s tree, fib(2) is computed three times and fib(1) five times.
Memoization
Memoization stores each result in a table the first time it is computed. A later call with the same arguments looks the answer up and returns at once, without making any calls of its own. Fibonacci then computes each of fib(0) to fib(n) once, and the total falls to 2n − 1 calls, as the table above shows. Binomial coefficients behave the same way: computing C(20, 10) = 184,756 with the plain recursion takes 369,511 calls, and with memoization 201.
Python's standard library builds this in as functools.lru_cache, and its documentation uses Fibonacci as the example. A single memoized fib(15) in this visualizer computes 16 different values and answers 13 calls from the table. Memoization only helps when calls repeat: factorial and fast power never call themselves twice with the same arguments, so a table would never be used. Filling the same table from the bottom up, without recursion, is dynamic programming's other form, tabulation.
The recursion tree method
Recursion trees are also a tool for analysing divide-and-conquer algorithms. Merge sort splits an array in half, sorts each half recursively and merges them, so its running time satisfies T(n) = 2T(n/2) + n. Drawing the tree and adding up the work at each level solves it: every level merges all n elements between its calls, and halving reaches single elements after log₂ n levels, so the total is about n log₂ n.
The "Merge sort calls" function shows this exactly. For 16 elements, the levels merge 16, 16, 16, 16 elements, 4 levels of 16 and 64 in total, from 31 calls. For 1,000 elements, which do not halve evenly, there are 10 levels and 9,976 elements merged.
Tower of Hanoi
The puzzle moves a stack of n disks from peg A to peg C, one disk at a time, never placing a larger disk on a smaller one. The recursive solution moves the top n − 1 disks to the spare peg B, moves the largest disk to C, then moves the n − 1 disks from B onto it. The number of moves M(n) = 2M(n − 1) + 1 with M(1) = 1, which is 2ⁿ − 1. Three disks take these 7 moves:
- Move disk 1 from A to C
- Move disk 2 from A to B
- Move disk 1 from C to B
- Move disk 3 from A to C
- Move disk 1 from B to A
- Move disk 2 from B to C
- Move disk 1 from A to C
The count doubles with every disk: 3 disks need 7, 10 disks need 1,023, 20 disks need 1,048,575.
Tail recursion
A call is a tail call when it is the last thing a function does, with nothing left to compute after it returns. Euclid's algorithm is tail recursive: gcd(a, b) simply returns gcd(b, a mod b). Factorial as written above is not, because each call still has to multiply by n after the inner call returns. gcd(252, 105) goes gcd(252, 105) → gcd(105, 42) → gcd(42, 21) → gcd(21, 0), returning 21.
Because nothing is waiting in a tail-calling frame, a language can reuse the frame instead of pushing a new one, so tail recursion runs in constant stack space, like a loop. The Scheme standard requires implementations to be properly tail recursive in this sense. Many other languages make no such promise, so a deep tail-recursive function can still overflow the stack there, and is better written as a loop.
Recursion questions and answers
How many calls does recursive Fibonacci make?
The plain version calls itself 2F(n + 1) − 1 times, where F is the Fibonacci sequence: 177 calls for fib(10), 21,891 for fib(20) and 2,692,537 for fib(30). The count grows by a factor of about 1.6 for every step in n, because almost every call makes two more.
What does memoization do to recursion?
It stores each result the first time it is computed and returns the stored value on later calls with the same arguments. Fibonacci then makes 2n − 1 calls instead of exponentially many: 59 for fib(30) against 2,692,537. Only functions whose calls repeat the same arguments benefit; factorial never does.
What is the call stack?
Each call gets a stack frame holding its arguments, local variables and the place to return to. A call pushes a frame, a return pops it, so the stack holds exactly the calls that have started and not finished. fact(5) needs 6 frames at its deepest point, one for each of fact(5) down to fact(0).
What is the base case in recursion?
The input a recursive function answers directly, without calling itself, such as n = 0 for factorial. Every recursive call must move closer to a base case, or the function never stops and eventually runs out of stack space.
What is the recursion tree method?
A way to solve recurrences such as T(n) = 2T(n/2) + n by drawing the tree of calls and adding up the work at each level. For merge sort on 16 elements every level merges 16 elements in total and there are 4 levels of merging, so the work is 16 × 4 = 64, which generalises to n log₂ n.
How many moves does the Tower of Hanoi take?
2ⁿ − 1 for n disks, which is also the number of recursive calls when the base case is one disk: 3 disks take 7, 10 disks take 1,023, 20 disks take 1,048,575.