Call Tree
Visual representation of recursive call hierarchy
Help
Understanding Recursion
- Recursion occurs when a function calls itself
- Every recursive function needs a base case to stop recursion
- Each call creates a new stack frame with its own parameters and local variables
- Return values propagate back up the call stack
- Stack grows until base case is reached, then unwinds with return values
Function Descriptions
- Factorial(n): Returns n! = n × (n-1) × ... × 1. Linear recursion, makes n calls.
- Fibonacci(n): Returns nth Fibonacci number. Tree recursion, exponential calls - very inefficient!
- Tower of Hanoi(n): Solves puzzle of moving n disks. Shows elegant recursive solution to complex problem.
- Binary Search: Searches sorted array by dividing in half. Logarithmic recursion, very efficient.
Recursion vs Iteration
- Pros: More elegant, natural for tree/graph problems, matches mathematical definitions
- Cons: Uses more memory (stack frames), risk of stack overflow, often slower
- Some recursive problems can be converted to iteration (like factorial)
- Others are much harder to express iteratively (like tree traversals)
- Tail recursion can be optimized by compilers to iteration
Stack Visualization
- Blue frames: Active function calls on the stack
- Green frames: Function calls that have returned
- Red highlight: Current executing frame
- Stack grows upward as functions are called
- Stack shrinks as functions return
- Watch parameters and return values in each frame