Recursion Tree Visualizer

Draws the full recursion tree for nine classic functions, plays the calls and returns step by step with the live call stack, highlights repeated calls and charts memoisation savings.

Funziona nel tuo browser. I tuoi file non lasciano mai il dispositivo. Scopri come verificarlo

…

Come si usa

  1. 1
    Choose a function

    Pick one of the nine functions from the menu.

  2. 2
    Set the arguments

    Type the arguments shown in the hint, such as n for Fibonacci.

  3. 3
    Read the figures

    Check the result, total calls, maximum depth and repeated calls.

  4. 4
    Play the execution

    Press Play or step through the calls and returns and watch the call stack.

  5. 5
    Compare memoisation

    Open Memoisation comparison for the chart and table of calls.

  6. 6
    Practise and export

    Answer recursion questions in Practice and export the tree as PNG or SVG.

Informazioni su questo strumento

Recursion, a function that calls itself on a smaller problem, is one of the ideas that separates beginners from confident programmers. On paper it looks simple; in a running program it is hard to picture how many calls happen, in which order and what each one returns. A recursion tree makes it visible: each call is a node, its recursive calls are its children and the returned value travels back up the edges. This visualizer builds that tree for nine classic functions, from factorial and Fibonacci to merge sort, binary search, fast exponentiation and the Towers of Hanoi. You can play the execution step by step and watch the call stack grow and shrink, see repeated calls highlighted and compare plain Fibonacci with a memoised version.

Reading the tree

Each box is one call, labelled with its arguments. Yellow boxes are on the call stack right now, blue boxes have returned and show their return value, red boxes mark calls that are repeated elsewhere in the tree and green boxes were answered from a cache. Use Play, the step buttons or the slider to move through the events in the order a computer executes them: each call appears when it starts, and its value appears when it returns.

The call stack

The call stack panel lists the calls that have started but not yet returned, from the first call down to the one running now. Its maximum height is the recursion depth, which matters because each frame uses memory; very deep recursion causes a stack overflow. Factorial of n reaches depth n, while merge sort on n items only reaches about log₂ n + 1.

Repeated work and memoisation

Plain recursive Fibonacci recomputes the same values again and again: fib(5) calls fib(1) five times and makes 15 calls in total. In general it makes 2·fib(n + 1) − 1 calls, which grows exponentially. Storing each result the first time it is computed (memoisation) means every value is computed once; later requests are answered from the cache, so the number of calls falls to 2n − 1. The comparison tab charts both for n up to 20: 21 891 calls against 39 for fib(20).

The nine functions

Factorial, plain and memoised Fibonacci, fast power (which halves n each time), Towers of Hanoi (2ⁿ − 1 moves), merge sort (split, sort halves, merge), binary search on a sorted list, Euclid's gcd and the sum of digits. Each shows its pseudo-code next to the tree so you can link every node to a line of code.

Worked example

fib(5) calls fib(4) and fib(3); fib(4) calls fib(3) and fib(2); and so on down to fib(1) and fib(0), which return immediately. The tree has 15 nodes and depth 5, the result is 5, and fib(3) is computed twice and fib(2) three times. With memoisation the second fib(3) and the repeated fib(2) and fib(1) are cache hits, and only 9 calls are made.

Limits

Trees are capped at 800 calls to stay readable, so large inputs are cut off with a notice, and arguments are limited for the fastest-growing functions. The functions are fixed examples; the tool does not run your own code.

Esempio svolto

fib(5): 15 calls, depth 5, result 5; memoised: 9 calls with 3 cache hits.

In breve

Domande frequenti

What is a base case?

The condition where the function returns without calling itself, such as n ≤ 1 for factorial. Without it, recursion would never stop.

Why is plain recursive Fibonacci slow?

It solves the same subproblems many times; the number of calls grows like 1.6 to the power n.

What does memoisation change?

Each result is stored the first time, so repeated calls return immediately from the cache and the total work becomes linear.

What is the recursion depth?

The largest number of calls on the stack at the same time, which is the height of the tree.

Can I enter my own function?

No. The tool traces nine built-in functions with arguments you choose, which covers the usual course examples.

Can I save the tree as an image?

Yes. Export it as PNG or SVG with the buttons at the top.

Completamente gratisNessun costo, nessuna registrazione
I file restano privatiTutto funziona nel tuo browser
Facile nella tua linguaDisponibile in molte lingue