Iโ€™ve made a simple python package called recursion-visualiser which you can install via pip that helps you to easily trace function calls for any arbitary recursive function and save tree as gif and png image by simply adding a decorator to your function.

Let's draw the tree for recursive Fibonacci function. Here is the recursive code:

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

def main():
    # Call function
    print(fib(n=6))

if __name__ == "__main__":
    main()

Now let's modify the code to draw recursion tree. First let's draw a very minimalist example

# Import Visualiser class from module visualiser
from visualiser.visualiser import Visualiser as vs

# Add decorator
@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()

The output file is saved as fibonacci.gif and fibonacci.png. Here is how output animation looks: Also the final image of recursion tree:

We can also make it better using node color and other properties:

# Import Visualiser class from module visualiser
from visualiser.visualiser import Visualiser as vs

# Add 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)

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

if __name__ == "__main__":
    main()

Here is the output which looks much better:

Here is the final image of the recursion tree:

Check out more examples at here

Answer from Bishal Sarang on Stack Overflow
๐ŸŒ
Vercel
recursion.vercel.app
Recursion Tree Visualizer
Input the source code of any recursive function in javascript, python or golang and visualize its recursion tree
๐ŸŒ
VisuAlgo
visualgo.net โ€บ en โ€บ recursion
Recursion Tree and DAG (Dynamic Programming/DP) - VisuAlgo
This visualization can visualize the recursion tree of any recursive algorithm or the recursion tree of a Divide and Conquer (D&C) algorithm recurrence (e.g., Master Theorem) that we can legally write in JavaScript.We can also visualize the Directed Acyclic Graph (DAG) of a Dynamic Programming ...
People also ask

What is a recursion tree?
A recursion tree is a visual representation of all recursive calls made by an algorithm. Each node represents a function call, and edges connect a call to its sub-calls. The tree shows the total work done at each level and helps analyze time complexity. For example, fib(5) creates a tree with nodes for fib(5), fib(4), fib(3), fib(2), fib(1), with many duplicate calls visible.
๐ŸŒ
crackr.dev
crackr.dev โ€บ visualize โ€บ recursion-tree
Recursion Tree Visualizer | Free Recursion Tree Generator
What is the difference between a recursion tree and a recursion DAG?
A recursion tree shows every call as a separate node, even duplicates. A recursion DAG (directed acyclic graph) merges duplicate calls into a single node with multiple parents. The DAG represents what happens with memoization: duplicate calls hit the cache instead of recomputing. For Fibonacci, the tree has O(2^n) nodes but the DAG has only O(n) nodes.
๐ŸŒ
crackr.dev
crackr.dev โ€บ visualize โ€บ recursion-tree
Recursion Tree Visualizer | Free Recursion Tree Generator
Why is the Fibonacci recursion tree exponential?
Each fib(n) call makes two recursive calls: fib(n-1) and fib(n-2). Without memoization, this creates a binary tree of depth n with roughly 2^n nodes. Many calls are duplicates (fib(3) is computed multiple times). With memoization (the DAG), each unique call is computed only once, reducing the time from O(2^n) to O(n).
๐ŸŒ
crackr.dev
crackr.dev โ€บ visualize โ€บ recursion-tree
Recursion Tree Visualizer | Free Recursion Tree Generator
Top answer
1 of 2
8

Iโ€™ve made a simple python package called recursion-visualiser which you can install via pip that helps you to easily trace function calls for any arbitary recursive function and save tree as gif and png image by simply adding a decorator to your function.

Let's draw the tree for recursive Fibonacci function. Here is the recursive code:

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

def main():
    # Call function
    print(fib(n=6))

if __name__ == "__main__":
    main()

Now let's modify the code to draw recursion tree. First let's draw a very minimalist example

# Import Visualiser class from module visualiser
from visualiser.visualiser import Visualiser as vs

# Add decorator
@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()

The output file is saved as fibonacci.gif and fibonacci.png. Here is how output animation looks: Also the final image of recursion tree:

We can also make it better using node color and other properties:

# Import Visualiser class from module visualiser
from visualiser.visualiser import Visualiser as vs

# Add 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)

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

if __name__ == "__main__":
    main()

Here is the output which looks much better:

Here is the final image of the recursion tree:

Check out more examples at here

2 of 2
1

Have a look at graphviz and the examples of usage.

Here is an example:

long long fib(int n)
{
    if (n <= 1) return 1;
    std::cout << "fib" << n << " -> fib" << n-2 << '\n';
    std::cout << "fib" << n << " -> fib" << n-1 << '\n';
    return fib(n-2) + fib(n-1);
}

int main()
{
    std::cout << "digraph {\n";
    fib(5);
    std::cout << "}\n";
}

Running

program > t.dot 
dot -Tpng t.dot

produced:

This is more compact than the requested image, where repeated calls with the same value are represented by one node, and N edges between nodes u,v if there are N calls from fib(u) to fib(v).

To get a tree one has to maintain unique IDs for each call. Here is an example for that:

static unsigned id = 0;
long long fib(int n)
{
    auto call_id = id++;
    std::cout << "fib" << call_id << " [label=\"fib(" << n << ")\"]\n";
    if (n <= 1) return 1;
    std::cout << "fib" << call_id << " -> fib" << id << '\n';
    auto fib_n_minus_2 = fib(n-2);
    std::cout << "fib" << call_id << " -> fib" << id << '\n';
    auto fib_n_minus_1 = fib(n-1);
    return fib_n_minus_2 + fib_n_minus_1;
}

int main()
{
    std::cout << "digraph {\n";
    fib(5);
    std::cout << "}\n";
}

And the graph is:

๐ŸŒ
Crackr
crackr.dev โ€บ visualize โ€บ recursion-tree
Recursion Tree Visualizer | Free Recursion Tree Generator
Free recursion tree visualizer and generator with step-by-step animations. See memoization collapse trees into DAGs.
๐ŸŒ
Processing
processing.org โ€บ examples โ€บ tree
Recursive Tree / Examples / Processing.org
Renders a simple tree-like structure via recursion. The branching angle is calculated as a function of the horizontal mouse location. Move the mouse left and right to change the angle.
Author: brpapa
Find elsewhere
๐ŸŒ
Recursionvisualizer
recursionvisualizer.com
Recursion Visualizer
๐Ÿ‘‹๐Ÿป Are you comfortable publicly sharing your visualizations? I'd love to see how folks are using this tool. Post a link in the discussions or @ me on social media (Twitter, Mastodon) ยท Source code on Github. Thank you @carlsborg for the rcviz library
๐ŸŒ
Towards AI
pub.towardsai.net โ€บ visualizing-recursion-trees-cb08103b54fe
Visualizing Recursion Trees. Reflections on Vibe Coding | by Han Qi | Towards AI
March 25, 2025 - All the previous attempts included attaching the graphviz image, and the printed Digraph text too in an attempt to generate a graphviz tree. After prompting to use ipywidgets and graphviz specifically, gpt got close. import matplotlib.pyplot as plt import ipywidgets as widgets from graphviz import Digraph from io import BytesIO from IPython.display import display, clear_output # Generate the recursion tree and track nodes def permute(nums): graph = Digraph(format="png") node_id = 0 # Unique ID for each node nodes_data = [] # Store nodes and their depth def backtrack(first=0, depth=0, parent=No
๐ŸŒ
Quanticdev
quanticdev.com โ€บ tools โ€บ recursion-visualization
Recursion Tree Visualizer
You need to enable JavaScript to run this app ยท Made with โ™ฅ by Bruno Papa โ€ข Github
๐ŸŒ
AlgoCademy
algocademy.com โ€บ blog โ€บ introduction-to-recursion-trees-and-visualization
Introduction to Recursion Trees and Visualization โ€“ AlgoCademy Blog
While drawing recursion trees by hand is an excellent exercise for understanding, there are also several tools available to help automate this process: Python libraries like graphviz or networkx can be used to generate recursion trees programmatically.
๐ŸŒ
OpenProcessing
openprocessing.org โ€บ @Poersch โ€บ 90192
Recursive Tree - Poersch - OpenProcessing
Since everybody got one: Here is my version of a recursive Tree. The text you see at the bottom of the sketch is the seed value for the random generator. Change the seed and see what happens. Controls: - TYPE SOMETHING = create seed - MOUSECLICK = create random seed and generate tree - BACKSPACE/DEL = delete last letter - RETURN/ENTER/CTRL = confirm your seed and generate tree - ALT = save image
๐ŸŒ
Arpit Bhayani
arpitbhayani.me โ€บ home โ€บ blogs โ€บ systems internals โ€บ visualizing recursion in python with just a decorator
Visualizing Recursion in Python with Just a Decorator
December 13, 2020 - Visualize recursion in Python! Learn to build a simple recursion tree visualizer using decorators and the recviz package.
๐ŸŒ
Lode
lodev.org โ€บ cgtutor โ€บ recursiontrees.html
Recursion Trees
If the max number of recursions is reached, the function returns so that it doesn't call itself anymore. Finally, the function calculates new angles and vectors for the next branches and calls itself again, if that branch is enabled at least. Here are just a few of the trees that can be generated this way: You can do much more with this to create very natural trees, for example you can randomize the angle each recursion, or randomize whether or not a next branch will be drawn.
๐ŸŒ
Creately
creately.com โ€บ diagram โ€บ example โ€บ ijxe0ywk2 โ€บ recursive-tree-classic
Recursive Tree [classic] | Creately
Easily visualize your processes and workflows with smart automation ยท Create visual organizational structures with images and detailed profiles
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ dsa โ€บ how-to-solve-time-complexity-recurrence-relations-using-recursion-tree-method
Recursion Tree Method to Solve Recurrences - GeeksforGeeks
January 11, 2026 - The recursion tree method is used to analyze the time complexity of recursive algorithms by visually representing the recurrence as a tree. Each node of the tree represents the work done in a single recursive call, and each level represents ...
๐ŸŒ
DEV Community
dev.to โ€บ brpapa โ€บ i-built-a-recursion-tree-visualizer-1l49
My Recursion Tree Visualizer project went viral on Linkedin - DEV Community
October 4, 2020 - It is a Recursion Tree Visualizer that helps programmers understand recursion. You input any recursive function using javascript code and visualize your corresponding recursion tree.
๐ŸŒ
JMP User Community
community.jmp.com โ€บ t5 โ€บ Uncharted โ€บ Recursive-Tree-Generator โ€บ ba-p โ€บ 163350
Recursive Tree Generator - JMP User Community
February 24, 2019 - There's no real 3D here, the perspective is odd, it is just a toy for a rainy Saturday. The attached JSL is set up to generate images into a directory. I picked a few of the better ones and cropped them a bit.
๐ŸŒ
PyPI
pypi.org โ€บ project โ€บ recursion-tree-plotter
recursion-tree-plotter ยท PyPI
January 17, 2021 - A python decorator to generate a visual tree for recursive functions.
๐ŸŒ
Medium
medium.com โ€บ @lshapz โ€บ recursive-tree-generation-2017767a2696
Recursive Tree Generation. aka Node Your Roots | by Laura Shapiro | Medium
May 1, 2017 - Since I was going to use recursion, I figured I would wrap this task in a โ€œwhileโ€ conditional. Then, when we reach subsequent loops, the โ€œwhileโ€ condition would evaluate false and this step of the code would be skipped nicely. (I could have used a regular old if, I suppose.) const treeify = function (array) { while(company.root === null) { let boss = array.filter(emp=>{ return emp.reports_to === null }) array = array.filter(emp=>{ return emp.reports_to !== null }) company.add(boss[0].role) }... }