Recursion Visualizer
Head · Tail · Function Calls · Backtracking
Programming Fundamentals Recursion Call Stack Backtracking

Recursion Visualizer

What is recursion

Recursion is when a function calls itself with a smaller input, solving a problem by breaking it into identical sub-problems. Each call creates a new stack frame that stores local variables. When the base case is reached, the stack unwinds — each frame returns its result to the caller. This unwinding is called backtracking.

  • Base Case: The stopping condition — without it, the function recurses forever (stack overflow)
  • Recursive Case: The function calls itself with a smaller/simpler input, moving toward the base case
  • Call Stack: Each recursive call pushes a new frame; returning pops it — LIFO (Last In, First Out)
  • Head Recursion: Work (print/compute) happens before the recursive call → output follows call order (1 → 10)
  • Tail Recursion: Work happens after the recursive call → output follows return order (10 → 1) — compilers can optimize this
  • Space Complexity: O(n) stack frames for depth n — each frame holds its own local variables

How This Visualizer Works

We use print(1 to 10) as the example. Each recursive call either prints before calling (Head Recursion) or prints after returning (Tail Recursion). Watch the call stack grow frame by frame and see how the output order flips completely — all without any tree or graph, just pure function calls.

head_recursion.c  PRINT BEFORE CALL

1void print(int n) {
2if (n > 10) return; // base case
3printf("%d ", n); // ← print FIRST
4print(n + 1); // then recurse
5}
6
7// Output: 1 2 3 4 5 6 7 8 9 10
8print(1); // initial call

tail_recursion.c  PRINT AFTER CALL

1void print(int n) {
2if (n > 10) return; // base case
3print(n + 1); // recurse FIRST
4printf("%d ", n); // ← print AFTER
5}
6
7// Output: 10 9 8 7 6 5 4 3 2 1
8print(1); // initial call

Head Recursion — Print 1 to 10

call_stack.frames  bottom → top

Stack empty — press Run or Step
Active (executing)
Waiting (paused)
Returning

current_action

Waiting to start…

head_recursion.c — live trace

1void print(int n) {
2if (n > 10) return;
3printf("%d", n); ← prints here
4print(n+1);
5} ← return

stdout — output order

// printed values appear here

backtrack_log

// return trace appears here
Speed: 700ms
Ready — head recursion prints before calling

Tail Recursion — Print 10 to 1

call_stack.frames  bottom → top

Stack empty — press Run or Step
Active (executing)
Waiting (paused)
Returning + printing

current_action

Waiting to start…

tail_recursion.c — live trace

1void print(int n) {
2if (n > 10) return;
3print(n+1); ← recurse first
4printf("%d", n); ← prints on return
5} ← return

stdout — output order

// printed values appear here

backtrack_log

// return trace appears here
Speed: 700ms
Ready — tail recursion prints after returning

Head vs Tail — Side-by-Side Comparison

Head Recursion

·printf BEFORE recursive call
·Output during call phase (going deeper)
·Output order: 1 2 3 4 5 6 7 8 9 10
·Stack grows → prints → stack shrinks
·Natural count-up order
·Not tail-call optimizable (print before)

Tail Recursion

·printf AFTER recursive call
·Output during return phase (unwinding)
·Output order: 10 9 8 7 6 5 4 3 2 1
·Stack grows fully → shrinks → prints
·Reversed order — unwinding effect
·Can be optimized to iteration by compiler

💡 The Key Insight — Why Order Flips

Imagine stacking 10 plates. In head recursion, you label each plate as you stack it — so the labels read 1, 2, 3… in stacking order. In tail recursion, you label each plate as you unstack it — so the labels read 10, 9, 8… in reverse.

The call stack is the same in both cases — 10 frames deep. What changes is when the work (printf) executes: before or after the recursive call returns.