Prerequisites
Before you begin, make sure you have:- Python 3.6 or higher installed
- Graphviz installed on your system
- The
recursion-visualiserpackage installed
Your first recursion tree
Let’s visualize a simple Fibonacci function to understand how the library works.Create a Python file
Create a new file called
fibonacci.py:Notice that all recursive calls use keyword arguments (
n=n-1 instead of just n-1). This is required for the visualiser to work correctly.Run the script
Execute your Python script:The script will:
- Calculate
fib(6)which returns8 - Generate
fibonacci.pngshowing the complete recursion tree - Generate
fibonacci.gifwith an animation of the recursion execution
View the results
Open the generated files to see your recursion tree:
- fibonacci.png - Static image of the complete recursion tree
- fibonacci.gif - Animated visualization showing how the tree is built
Understanding the output
The generated visualization shows:- Nodes: Each node represents a function call with its arguments (e.g.,
fib(4)) - Edges: Arrows show which function calls which
- Return values: The value returned by each function call is displayed in the node
- Tree structure: The overall shape reveals the recursion pattern
Customize your visualization
You can customize the appearance of your recursion tree by passing options to the decorator:Control what’s displayed
The decorator accepts several parameters to control the visualization:| Parameter | Type | Default | Description |
|---|---|---|---|
show_argument_name | bool | True | Show parameter names in nodes (e.g., n=5 vs just 5) |
show_return_value | bool | True | Display return values in nodes |
ignore_args | list | [] | List of argument names to hide from visualization |
node_properties_kwargs | dict | {} | Graphviz node styling properties (shape, color, style, etc.) |
Try another example
Let’s try visualizing a factorial function:Key takeaways
Add the
@Visualiser (or @vs) decorator to any recursive functionUse keyword arguments in all recursive calls (
n=n-1 not n-1)Call
vs.make_animation() to generate PNG and GIF outputsCustomize appearance with
node_properties_kwargs and other parametersNext steps
Explore examples
See visualizations of classic recursive algorithms
API reference
Learn about all available configuration options