Visualize Recursion Tree with Animation in Python
Tags: python, beginners, visualization, algorithms
Published: 2020-04-02 08:28:35

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](https://github.com/Bishalsarang/Recursion-Tree-Visualizer) 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](https://www.graphviz.org/download/)
-   Download  [graphviz binary](https://www.graphviz.org/download/)
-   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](https://pypi.org/project/recursion-visualiser/)

```
pip install recursion-visualiser

```
The preferred way to import the decorator class from the package is as:
```python
from visualiser.visualiser import Visualiser as vs
```
Now, let's draw the recursion tree for fibonacci series.
Here is the simple fibonacci function.
```python
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
```python
 from visualiser.visualiser import Visualiser as vs
```
  
 3. Add decorator 
```python
@vs(node_properties_kwargs={"shape":"record","color":"#f57542", "style":"filled", "fillcolor":"grey"})
```
I have supplied`node_properties_kwargs` for styling of node.
 3. Modify `fib` to pass all the argument as keywords
```python
 def fib(n):
    if n <= 1:
        return n
    return fib(n=n - 1) + fib(n=n - 2)
```
4. Call `make_animation()` method
```python
vs.make_animation("fibonacci.gif", delay=2)
```
Here is how our final code is going to look like:
```python
# 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](https://raw.githubusercontent.com/Bishalsarang/Recursion-Tree-Visualizer/master/examples/fibonacci.gif)
Also the recursion tree is saved as:
![enter image description here](https://raw.githubusercontent.com/Bishalsarang/Recursion-Tree-Visualizer/master/examples/fibonacci.png)

Here is the github link to the package:
[https://github.com/Bishalsarang/Recursion-Tree-Visualizer](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](https://github.com/Bishalsarang/Recursion-Tree-Visualizer/tree/master/examples)
Hope you will enjoy the package. See you in next post.