Sub topic 13
Tracing Recursive Algorithms
This chapter assumes you can already trace procedure calls with a call stack, as covered in the previous chapter. It also assumes you know what a recursive function is: a function that calls itself on a smaller version of the problem.
A recursive trace is a call-stack trace in which every frame runs the same code. That makes it easy to lose track of which n belongs to which call. The fix is to give each call its own row, or its own indentation level, and record the work it still has to do after its inner call returns.
Two Phases: Winding and Unwinding
Every recursive trace has two phases:
- Winding. Calls are pushed onto the stack, one per level, until a call reaches the base case. The base case is the condition that returns without recursing.
- Unwinding. Each frame takes the value returned by the call below it, finishes its pending work, and returns a value to the frame above.
Here is the factorial function and its indented trace:
FUNCTION Fact(n)
IF n ≤ 1 THEN
RETURN 1
END IF
RETURN n × Fact(n - 1)
END FUNCTION
Trace of Fact(3):
Fact(3) calls Fact(2)
Fact(2) calls Fact(1)
Fact(1) returns 1
Fact(2) returns 2 × 1 = 2
Fact(3) returns 3 × 2 = 6Each indentation level is one stack frame. Read the trace downward for the winding phase and upward for the returns.
A Frame Table
For longer traces, a table with one row per frame is easier to check than indentation. Fill the "pending work" column while winding, then fill the "returns" column from the bottom row upward.
| Frame | n | Base case? | Pending work | Returns |
|---|---|---|---|---|
| 1 | 3 | no | 3 × Fact(2) | 6 |
| 2 | 2 | no | 2 × Fact(1) | 2 |
| 3 | 1 | yes | none | 1 |
The pending work column shows that the multiplication happens after the inner call returns. A common mistake is to multiply while winding, as if 3 × 2 could be computed before Fact(2) is known.
Common Errors a Trace Exposes
- Missing base case. The table never gets a "yes" row, and the stack grows without limit.
- Unreachable base case.
Fact(-2)with a test ofn = 1instead ofn ≤ 1would step through -3, -4, and so on, never hitting the base case. Tracing with an edge-case input catches this. - Wrong base value. If
Fact(1)returned 0, every row above it would return 0. A bad base value spreads to every level. - Mixing up frames. Writing frame 2's
ninto frame 1's calculation. Keeping one row per frame prevents this.
Exercise
Build a frame table for Sum(A, 3) with A = [4, 1, 5] (1-based), where Sum(A, k) returns 0 if k = 0 and otherwise returns A[k] + Sum(A, k - 1). What is returned, and in which order are the additions done?
Show answer
Frames: k = 3 (pending 5 + Sum(A, 2)), k = 2 (pending 1 + Sum(A, 1)), k = 1 (pending 4 + Sum(A, 0)), k = 0 (base case, returns 0). Unwinding: k = 1 returns 4 + 0 = 4, k = 2 returns 1 + 4 = 5, k = 3 returns 5 + 5 = 10. The result is 10. The additions happen in the order A[1], then A[2], then A[3], which is the reverse of the order the calls were made.
Exercise
Someone changes the base case of Fact to "IF n = 0 THEN RETURN 1". Trace Fact(2) and Fact(-1). Which call is still correct, and what happens to the other?
Show answer
Fact(2) calls Fact(1), which calls Fact(0), which returns 1. Then Fact(1) returns 1 and Fact(2) returns 2, so it is correct. Fact(-1) calls Fact(-2), Fact(-3), and so on. It never reaches n = 0, so it recurses until the stack overflows.