Skip to main content
This guide will walk you through creating your first recursion tree visualization using the Recursion Tree Visualiser.

Prerequisites

Before you begin, make sure you have:
  • Python 3.6 or higher installed
  • Graphviz installed on your system
  • The recursion-visualiser package installed
If you haven’t installed these yet, follow the installation guide.

Your first recursion tree

Let’s visualize a simple Fibonacci function to understand how the library works.
1

Create a Python file

Create a new file called fibonacci.py:
from visualiser.visualiser import Visualiser as vs

@vs()
def fib(n):
    if n <= 1:
        return n
    return fib(n=n - 1) + fib(n=n - 2)

def main():
    print(fib(n=6))
    vs.make_animation("fibonacci.gif", delay=2)

if __name__ == "__main__":
    main()
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.
2

Run the script

Execute your Python script:
python fibonacci.py
The script will:
  • Calculate fib(6) which returns 8
  • Generate fibonacci.png showing the complete recursion tree
  • Generate fibonacci.gif with an animation of the recursion execution
3

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
The recursion tree shows every function call with its arguments and return values, making it easy to understand how the recursive algorithm works.

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
For Fibonacci, you’ll see a binary tree where each call branches into two recursive calls, with overlapping subproblems visible as repeated subtrees.

Customize your visualization

You can customize the appearance of your recursion tree by passing options to the decorator:
@vs(node_properties_kwargs={
    "shape": "record",
    "color": "#f57542", 
    "style": "filled",
    "fillcolor": "grey"
})
def fib(n):
    if n <= 1:
        return n
    return fib(n=n - 1) + fib(n=n - 2)

Control what’s displayed

The decorator accepts several parameters to control the visualization:
ParameterTypeDefaultDescription
show_argument_nameboolTrueShow parameter names in nodes (e.g., n=5 vs just 5)
show_return_valueboolTrueDisplay return values in nodes
ignore_argslist[]List of argument names to hide from visualization
node_properties_kwargsdict{}Graphviz node styling properties (shape, color, style, etc.)
Check out the customization guide for detailed examples of styling options.

Try another example

Let’s try visualizing a factorial function:
from visualiser.visualiser import Visualiser as vs

@vs()
def fact(n):
    if n <= 1:
        return n
    return n * fact(n=n-1)

def main():
    print(fact(n=6))
    vs.make_animation("factorial.gif", delay=2)

if __name__ == "__main__":
    main()
This creates a linear recursion tree (a straight line) instead of the branching tree from Fibonacci, demonstrating how different recursive patterns create different tree shapes.

Key takeaways

Add the @Visualiser (or @vs) decorator to any recursive function
Use keyword arguments in all recursive calls (n=n-1 not n-1)
Call vs.make_animation() to generate PNG and GIF outputs
Customize appearance with node_properties_kwargs and other parameters

Next steps

Explore examples

See visualizations of classic recursive algorithms

API reference

Learn about all available configuration options

Build docs developers (and LLMs) love