A small python package to visualise recursive function on Python. It draws recursion tree
Project description
Recursion Visualiser
Recursion visualiser is a python tool that draws recursion tree for recursive function with very less code changes.
Installation
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 class from the package is as:
from visualiser.visualiser import Visualiser as vs
Example
1. Fibonacci
Let's draw the recursion tree for fibonacci number.
Here is how the simple code looks like
def fib(n):
if n <= 1:
return n
return fib(n - 1) + fib(n - 2)
print(fib(6))
Now we want to draw the recursion tree for this function. It is as simple as adding a decorator
# Author: Bishal Sarang
# Import Visualiser class from module visualiser
from visualiser.visualiser import Visualiser as vs
# Add decorator
# Decorator accepts arguments: ignore_args and show_argument_name
@vs()
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 are the changes required:
- Add decorator Visualiser which accepts optional arguments
ignore_args
,show_argument_name
and 'show_return_value' - Change every function calls to pass as keyword arguments.
- Make_animation
The output image are saved as "fibonacci.gif" and "fibonacci.png"
Here is how the recursion tree looks like:
Animation:
2. Make sum
This is taken from one of my answers on quora where I had to manually draw recursion tree. Using Visualiser with very less changes I was able to draw the following tree. You can compare the tree here
TODO:
- Minimal working version
- Upload package to pypi
- Support animation
- Refactor
- Handle base cases
- Make more beautiful trees
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for recursion-visualiser-1.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 79a97c92e8eb9f123eb84ea232738c1b4439f6d2b1061200fcea3d3c85ab889e |
|
MD5 | 009a8c3b568aa770662227c4b09162d3 |
|
BLAKE2b-256 | 39a45001c3afdacf6d8d4d0fd63510e703dc58d0b7b8ad58a7dccc1d3b6da832 |
Hashes for recursion_visualiser-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 962947339c39f814f5f146d0b67a7a209b0b3fd1e0bb835e9a1c818034b139b1 |
|
MD5 | 2b7b91fc204872f86a046ba6ee71e768 |
|
BLAKE2b-256 | c2f775598d6f2887008f6e8bc884266587fb70ac5c2db8486f2f1c135ac405c8 |