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.
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 import Visualiser as vs
# Add decorator
# Decorator accepts arguments: ignore_args and show_argument_name
@vs(ignore_args=['node_num'])
def fib(n, node_num):
if n <= 1:
return n
# Increment decorator class node_count before each function calls
vs.node_count += 1
left = fib(n=n - 1, node_num=vs.node_count)
# node_count is incremented
vs.node_count += 1
right = fib(n=n - 2, node_num=vs.node_count)
return left + right
# Call function
print(fib(n=6, node_num=0))
# Save recursion tree to a file
vs.write_image("fibonacci.png")
Here are the changes required:
- Change function signature from
fib(n)
tofib(n, node_num)
- Add decorator Visualiser which accepts arguments
ignore_args
andshow_argument_name
- Before each function calls make sure to increment
node_count
argument of decorator class - Change every function calls to pass as keyword arguments.
- Write the image
Here is how the recursion tree looks like:
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
Installation
The only dependency for recursin 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/'
- Install all the requirements
pip install -r requirements.txt
See the examples inside example
toknow how to use
TODO:
- Minimal working version
- Refactor
- Handle base cases
- Make more beautful 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-0.0.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2a1c0afcd2b9d80fa7ea3243f5efe6d25993bb29135c3f69297b85d8b51b7a77 |
|
MD5 | e63741e4fe5d1d6c13b3571c5ccf9e56 |
|
BLAKE2b-256 | 1f12e54f6f88648a6464e1eb09e1766805af288ef1d5dda846e442408d84ff01 |
Hashes for recursion_visualiser-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 947dbcef11d334f2f1608da2ac1ff2a54e5988aa005d74c9e01808d2bf83fc90 |
|
MD5 | ec18371fc680ba038eadd0d718fe7736 |
|
BLAKE2b-256 | e3ace47b9e51799c460ead52ffa303499c70fcf9b1ffb8a127211699d5cc0e5f |