Example code
This example demonstrates a straightforward recursive factorial implementation:How it works
The recursive factorial function:- Base case: Returns
nwhenn <= 1 - Recursive case: Returns
n * fact(n-1), creating a single recursive call - The recursion continues linearly until reaching
n = 1
Unlike Fibonacci, the factorial function creates a linear recursion tree (a single path) rather than a branching tree. This makes it easier to understand the call stack behavior.
Decorator configuration
This example uses the@vs() decorator with default settings, which:
- Shows all function arguments in the tree nodes
- Displays argument names
- Shows return values
- Uses default node styling
Output
Running this code withfact(6) generates:
- Console output:
720(6! = 6 × 5 × 4 × 3 × 2 × 1) - Animation file:
factorial.gifshowing the linear recursion tree with a 2-second delay between frames