Recursion
Recursion refers to a programming technique where a function calls itself, directly or indirectly, during its definition or execution. The core idea is to break a complex problem into smaller subproblems with the same structure.
Basic Concepts of Recursion
Stack Frame Changes in Recursive Calls - Using factorial(3) as an Example
fact(3)
Waiting for fact(2) to return -> push onto stack
Down, call
fact(2)
Waiting for fact(1) to return -> push onto stack
Down, call
fact(1)
Waiting for fact(0) to return -> push onto stack
Down, call
fact(0) = 1
Base case! Begin returning -> pop from stack
Up, return layer by layer (LIFO)
fact(3) = 3 × fact(2) = 3 × 2 × fact(1) = 3 × 2 × 1 × fact(0) = 3 × 2 × 1 × 1 = 6
Orange box (base case) -> stop recursion and begin returning | stack frames pop in LIFO order: the last pushed returns first
A valid recursive function must contain two key elements:
| Element | Meaning | Example (factorial) |
|---|---|---|
| Base Case | The condition that terminates recursion, preventing infinite recursion | if (n == 0) return 1; |
| Recursive Case | Breaks the problem into smaller subproblems and calls itself | return n * factorial(n-1); |
Classic Recursive Problems
Factorial
Example
#include
/* Recursive implementation of factorial
n! = n * (n-1)!, base case: 0! = 1 */
long
long
factorial
(
int
n
)
{
if
(
n
==
0
)
return
1
;
/* Base case */
return
n
factorial
(
n
1
)
;
/* Recursive case */
}
int
main
(
)
{
printf
(
"5! = %lld\n"
,
factorial
(
5
)
)
;
/* Output: 120 */
return
0
;
}
Fibonacci Sequence
Example
#include
/* Naive recursion: lots of redundant computation, O(2^n) */
int
fib
(
int
n
)
{
if
(
n
<=
1
)
return
n
;
/* Base case */
return
fib
(
n
1
)
fib
(
n
2
)
;
/* Recursive case */
}
/* Memoization optimization: cache computed results, O(n) */
#define MAX 100
long
long
memo
[
MAX
]
=
{
0
}
;
long
long
fibMemo
(
int
n
)
{
if
(
n
<=
1
)
return
n
;
if
(
memo
[
n
]
!=
0
)
return
memo
[
n
]
;
/* Already computed, return directly */
memo
[
n
]
=
fibMemo
(
n
1
)
fibMemo
(
n
2
)
;
return
memo
[
n
]
;
}
int
main
(
)
{
printf
(
"fib(10) (naive): %d\n"
,
fib
(
10
)
)
;
/* Output: 55 */
printf
(
"fibMemo(50) (memoized): %lld\n"
,
fibMemo
(
50
)
)
;
/* Output: 12586269025 */
return
0
;
}
Tower of Hanoi Problem
Example
#include
/* Recursive solution to the Tower of Hanoi
Move n disks from src to dst using aux
Steps: move n-1 from src->aux, the largest from src->dst, n-1 from aux->dst */
void
hanoi
(
int
n
,
char
src
,
char
aux
,
char
dst
)
{
if
(
n
==
1
)
{
printf
(
"Disk 1 moved from %c to %c\n"
,
src
,
dst
)
;
return
;
}
hanoi
(
n
1
,
src
,
dst
,
aux
)
;
/* Move n-1 disks from src to aux */
printf
(
"Disk %d moved from %c to %c\n"
,
n
,
src
,
dst
)
;
hanoi
(
n
1
,
aux
,
src
,
dst
)
;
/* Move n-1 disks from aux to dst */
}
int
main
(
)
{
printf
(
"Tower of Hanoi (3 disks):\n"
)
;
hanoi
(
3
,
'A'
,
'B'
,
'C'
)
;
/* Output: total 7 moves (2^3-1), minimum moves = 2^n-1 */
return
0
;
}
Recursion vs. Iteration Comparison
| Dimension | Recursion | Iterate |
|---|---|---|
| Code Readability | High (close to the mathematical definition) | Sometimes less intuitive than recursion |
| Execution Efficiency | Lower (stack frame overhead) | Higher |
| Space overhead | O(recursion depth) stack space | O(1) |
| Stack Overflow Risk | Yes (when depth is too large) | None |
| Suitable Structures | Natural recursive structures like trees and graphs | Linear structures such as arrays |
Tail RecursionTail recursion is a special form of recursion where the recursive call is the last operation executed in the function body. Some compilers can optimize tail recursion into equivalent iteration to save stack space. However, not all C compilers enable this optimization by default.