DEV Community

loading...

Visualize Recursion Tree with Animation in Python

bishalsarang profile image Bishal Sarangkoti ・2 min read

Hey everyone, I hope everyone is doing fine. This is going to be my first post on dev.to.

Recursion is an important topic in algorithms. Most of the beginners have trouble understanding recursion about the order in which function calls take place parameters passed and so on.
So, I built a simple python package called recursion-visualiser which can be a useful teaching aid as well as debugging tool to understand recursion.

Let's first install the package:
The only dependency for recursion visualiser is Graphviz which you can download from here

  • Download graphviz binary
  • Add graphviz bin to path manually or by adding the following line on your script. Change the installation directory according to your installation path
# Set it to bin folder of graphviz  
os.environ["PATH"] += os.pathsep +  'C:/Program Files (x86)/Graphviz2.38/bin/'  

The easiest way to install recursion-visualiser package is from pypi

pip install recursion-visualiser

The preferred way to import the decorator class from the package is as:

from visualiser.visualiser import Visualiser as vs

Now, let's draw the recursion tree for fibonacci series.
Here is the simple fibonacci function.

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

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

if __name__ == "__main__":
    main()

So how can we visualise fib(6)?

It is as simple as adding a decorator .
Here are the steps:

  1. Import recursion visualiser by adding this line
 from visualiser.visualiser import Visualiser as vs
  1. Add decorator
@vs(node_properties_kwargs={"shape":"record","color":"#f57542", "style":"filled", "fillcolor":"grey"})

I have suppliednode_properties_kwargs for styling of node.

  1. Modify fib to pass all the argument as keywords
 def fib(n):
    if n <= 1:
        return n
    return fib(n=n - 1) + fib(n=n - 2)
  1. Call make_animation() method
vs.make_animation("fibonacci.gif", delay=2)

Here is how our final code is going to look like:

# Author: Bishal Sarang
# Import Visualiser class from module visualiser
from visualiser.visualiser import Visualiser as vs

# Add decorator
# Decorator accepts optional arguments: ignore_args , show_argument_name, show_return_value and node_properties_kwargs
@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():
    # Call function
    print(fib(n=6))
    # Save recursion tree to a file
    vs.make_animation("fibonacci.gif", delay=2)

if __name__ == "__main__":
    main()

Here is how the animation looks like:
enter image description here
Also the recursion tree is saved as:
enter image description here

Here is the github link to the package:
https://github.com/Bishalsarang/Recursion-Tree-Visualizer

Here are some more examples on coin change problem, fibonacci, constructing binary string, subset sum and combinations problems: https://github.com/Bishalsarang/Recursion-Tree-Visualizer/tree/master/examples
Hope you will enjoy the package. See you in next post.

Discussion (0)

pic
Editor guide