|
1 | | -# Recursion Visualiser |
2 | | -Recursion visualiser is a python tool that draws recursion tree for recursive function with very less code changes. |
3 | | - |
4 | | -## Example |
5 | | -### 1. Fibonacci |
6 | | -Let's draw the recursion tree for fibonacci number. |
7 | | -Here is how the simple code looks like |
8 | | -```python |
9 | | -def fib(n): |
10 | | - if n <= 1: |
11 | | - return n |
12 | | - return fib(n - 1) + fib(n - 2) |
13 | | - |
14 | | -print(fib(6)) |
15 | | -``` |
16 | | - |
17 | | -Now we want to draw the recursion tree for this function. It is as simple as adding a decorator |
18 | | -```python |
19 | | -# Author: Bishal Sarang |
20 | | - |
21 | | -# Import Visualiser class from module visualiser |
22 | | -from visualiser import Visualiser as vs |
23 | | - |
24 | | -# Add decorator |
25 | | -# Decorator accepts arguments: ignore_args and show_argument_name |
26 | | -@vs(ignore_args=['node_num']) |
27 | | -def fib(n, node_num): |
28 | | - if n <= 1: |
29 | | - return n |
30 | | - |
31 | | - # Increment decorator class node_count before each function calls |
32 | | - vs.node_count += 1 |
33 | | - left = fib(n=n - 1, node_num=vs.node_count) |
34 | | - |
35 | | - # node_count is incremented |
36 | | - vs.node_count += 1 |
37 | | - right = fib(n=n - 2, node_num=vs.node_count) |
38 | | - return left + right |
39 | | - |
40 | | -# Call function |
41 | | -print(fib(n=6, node_num=0)) |
42 | | - |
43 | | -# Save recursion tree to a file |
44 | | -vs.write_image("fibonacci.png") |
45 | | -``` |
46 | | -Here are the changes required: |
47 | | - |
48 | | - - Change function signature from `fib(n)` to `fib(n, node_num)` |
49 | | - - Add decorator Visualiser which accepts arguments `ignore_args` and `show_argument_name` |
50 | | - - Before each function calls make sure to increment `node_count` argument of decorator class |
51 | | - - Change every function calls to pass as keyword arguments. |
52 | | - - Write the image |
53 | | - |
54 | | -Here is how the recursion tree looks like: |
55 | | - |
56 | | - |
57 | | -## 2. Make sum |
58 | | -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](https://qr.ae/TltTCV) |
59 | | - |
60 | | - |
61 | | - |
62 | | -## Installation |
63 | | -The only dependency for recursin visualiser is Graphviz which you can download from [here](https://www.graphviz.org/download/) |
64 | | - |
65 | | - |
66 | | - |
67 | | -- Download [graphviz binary](https://www.graphviz.org/download/) |
68 | | -- Add graphviz bin to path manually or by adding the following line on your script. Change the installation directory according to your installation path |
69 | | - |
| 1 | +# Recursion Visualiser |
| 2 | +Recursion visualiser is a python tool that draws recursion tree for recursive function with very less code changes. |
| 3 | + |
| 4 | + ## Installation |
| 5 | +The only dependency for recursion visualiser is Graphviz which you can download from [here](https://www.graphviz.org/download/) |
| 6 | + |
| 7 | +- Download [graphviz binary](https://www.graphviz.org/download/) |
| 8 | +- Add graphviz bin to path manually or by adding the following line on your script. Change the installation directory according to your installation path |
| 9 | + |
| 10 | +``` |
| 11 | +# Set it to bin folder of graphviz |
| 12 | +os.environ["PATH"] += os.pathsep + 'C:/Program Files (x86)/Graphviz2.38/bin/' |
| 13 | +``` |
| 14 | + |
| 15 | + |
| 16 | +The easiest way to install ```recursion-visualiser``` package is from [pypi](https://pypi.org/project/recursion-visualiser/) |
70 | 17 | ``` |
71 | | -# Set it to bin folder of graphviz |
72 | | -os.environ["PATH"] += os.pathsep + 'C:/Program Files (x86)/Graphviz2.38/bin/' |
| 18 | +pip install recursion-visualiser |
73 | 19 | ``` |
74 | | -- Install all the requirements |
75 | | - |
76 | | -``` |
77 | | -pip install -r requirements.txt |
| 20 | +The preferred way to import the class from the package is as: |
| 21 | +```python |
| 22 | +from visualiser.visualiser import Visualiser as vs |
78 | 23 | ``` |
79 | | - |
80 | | -See the examples inside `example` to know how to use |
81 | | - |
82 | | -Alternatively, you can install ```recursion-visualiser``` package from pypi by:<br> |
83 | | -```pip install recursion-visualiser==0.0.2 ``` |
84 | | - |
85 | | -## TODO: |
| 24 | +## Example |
| 25 | +### 1. Fibonacci |
| 26 | +Let's draw the recursion tree for fibonacci number. |
| 27 | +Here is how the simple code looks like |
| 28 | +```python |
| 29 | +def fib(n): |
| 30 | + if n <= 1: |
| 31 | + return n |
| 32 | + return fib(n - 1) + fib(n - 2) print(fib(6)) |
| 33 | +``` |
| 34 | + |
| 35 | +Now we want to draw the recursion tree for this function. It is as simple as adding a decorator |
| 36 | +```python |
| 37 | +# Author: Bishal Sarang |
| 38 | + |
| 39 | +# Import Visualiser class from visualiser.visualiser module |
| 40 | +from visualiser.visualiser import Visualiser as vs |
| 41 | + |
| 42 | +# Add decorator |
| 43 | +# Decorator accepts arguments: ignore_args and show_argument_name |
| 44 | +@vs(ignore_args=['node_num']) |
| 45 | +def fib(n, node_num): |
| 46 | + if n <= 1: |
| 47 | + return n |
| 48 | + # Increment decorator class node_count before each function calls |
| 49 | + vs.node_count += 1 |
| 50 | + left = fib(n=n - 1, node_num=vs.node_count) |
86 | 51 |
|
87 | | - - [x] Minimal working version |
88 | | - - [x] Upload package to pypi |
89 | | - - [ ] Refactor |
90 | | - - [ ] Handle base cases |
91 | | - - [ ] Make more beautful trees |
92 | | - |
| 52 | + # node_count is incremented |
| 53 | + vs.node_count += 1 right = fib(n=n - 2, node_num=vs.node_count) |
| 54 | + return left + right |
| 55 | + |
| 56 | +# Call function |
| 57 | +print(fib(n=6, node_num=0)) |
| 58 | + |
| 59 | +# Save recursion tree to a file |
| 60 | +vs.write_image("fibonacci.png") |
| 61 | +``` |
| 62 | +Here are the changes required: |
| 63 | + |
| 64 | + - Change function signature from `fib(n)` to `fib(n, node_num)` |
| 65 | + - Add decorator Visualiser which accepts arguments `ignore_args` and `show_argument_name` |
| 66 | + - Before each function calls make sure to increment `node_count` argument of decorator class |
| 67 | + - Change every function calls to pass as keyword arguments. |
| 68 | + - Write the image |
| 69 | + |
| 70 | +Here is how the recursion tree looks like: |
| 71 | + |
| 72 | + |
| 73 | +## 2. Make sum |
| 74 | +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](https://qr.ae/TltTCV) |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | +## TODO: |
| 80 | + - [x] Minimal working version |
| 81 | + - [x] Upload package to pypi |
| 82 | + - [ ] Refactor |
| 83 | + - [ ] Handle base cases |
| 84 | + - [ ] Make more beautiful trees |
0 commit comments