Recursion tree visualiser
Paste a small recursive JavaScript function and see every call it makes as a tree, with exact call counts and the arguments that repeat. Or enter a divide-and-conquer recurrence and get its recursion tree, the work per level and the master-theorem bound.
On this page
Recursion tree from a functionWrite one named recursive function. It runs in a sandboxed worker in your browser, stopped after 2 seconds or 5,000 calls.
- Running
- Waiting on a call
- Returned
- Cache hit
Totals
Result: —
Call stack (top is running)
Most repeated calls
Tracing a function
Write one named function with the function keyword, then say what to call it with. The examples cover the common shapes: linear recursion (factorial, gcd), branching recursion (Fibonacci, binomial coefficients, Towers of Hanoi) and divide and conquer (fast power, merge-sort splitting). The tool opens on the finished tree; use the controls to rewind and replay the calls in order.
- Calls is the total number of times the function was entered, the size of the tree.
- Max depth is the deepest the call stack went, the height of the tree. It tells you how much stack memory the recursion needs.
- Distinct is the number of different argument lists. When it is much smaller than the call count, the function repeats work, and memoisation will help.
Tick Memoise to cache each result by its arguments. Repeated calls then return immediately and show up as dashed purple leaves. For fib(5) the call count drops from 15 to 9; for fib(25) it drops from 242,785 to 49, and the naive tree is too large to draw.
Solving a recurrence
The second tab handles recurrences of the form
which describe algorithms that split a problem of size into subproblems of size and spend splitting and combining. Level of the recursion tree has nodes of size , and the tree has about levels, ending in leaves.
The master theorem
Compare with , the number of leaves.
- If grows polynomially slower, the leaves dominate: .
- If they grow at the same rate, every level does about the same work: . More generally, if then .
- If grows polynomially faster (and satisfies a mild regularity condition, which polynomials do), the root dominates: .
Worked examples
- Merge sort, . Here , the same as : case 2, .
- Binary search, . , the same as : case 2, .
- Karatsuba multiplication, . beats : case 1, , better than the schoolbook .
- Strassen’s matrix multiplication, . beats : case 1. With 8 subproblems instead of 7 it would be .
- . beats : case 3, . The top level already does half of all the work.
The table of exact values computes directly from the recurrence for and divides by the bound. If the bound is right, the ratio settles towards a constant, which is a good way to convince yourself of the answer without trusting the theorem.
Recurrences of other shapes, such as (selection sort, ) or (naive Fibonacci, exponential), are outside the master theorem. Write them as a function in the first tab to see their trees.
Learn the background
The recursion explainer walks through the call stack for factorial and Fibonacci line by line, and Big-O notation explains the growth rates the master theorem produces.
References
- J. L. Bentley, D. Haken and J. B. Saxe, “A general method for solving divide-and-conquer recurrences”, ACM SIGACT News 12(3), 1980. doi:10.1145/1008861.1008865
- T. H. Cormen, C. E. Leiserson, R. L. Rivest and C. Stein, Introduction to Algorithms, 4th ed., MIT Press, 2022. Chapter 4.