A python decorator to generate a visual tree for recursive functions.
$ pip install recursion-tree-plotter
Let's say you have a recursive function for finding n-th element in Fibonacci sequence.
def fib(n):
if n <= 1:
return n
return fib(n - 1) + fib(n - 2)
In order to plot a recursion tree for an execution of above function (say fib(5)
), we put @plot_recursion_tree
decorator over it.
from recursion_tree_plotter import plot_recursion_tree
@plot_recursion_tree
def fib(n):
if n <= 1:
return n
return fib(n - 1) + fib(n - 2)
And boom!
In the tree, node label is constructed as <comma-separated args> [<counter>]
where counter
specifies the order
of execution.
Check out examples
folder for more examples!