Recursion Tree Explorer

Pick a recursive definition, set n, and see how many calls it makes and how many of them solve a problem something else already solved.

Loading interactive simulation...

Fibonacci, computed by counting ones 🖖

Set n to 20. The tree holds 21,891 calls: 10,946 of them reach a base case, and the other 10,945 are additions. Only fib(1) returns 1, fib(0) returns 0, so 6,765 of those leaves carry a one and 4,181 carry nothing. F(20) is 6,765, which is exactly the number of ones. Every value the recursion returns is a sum of some of them, and nothing about that changes with n: the base-case card always reads F(n+1), and the ones among them always number F(n).

Two trees of 21,891 calls, and only one repeats itself 🖖

Naive fib(20) and merge sort on 10,946 items make exactly the same number of calls: 21,891, split into 10,946 base cases and 10,945 combining steps. Three of the six cards read identically, and both definitions branch two ways at every internal node, so branching is not what separates them. The distinct-subproblem card is: 21 against 21,891. A memo table collapses Fibonacci's tree to 21 units of work and leaves merge sort's at 21,891, because merge sort's halves are different slices of one array, and a cached answer for "a run of 5,473 items" would be the wrong 5,473 items.

One subtracted 1 separates φⁿ from 2ⁿ 🖖

Hanoi and Fibonacci both recurse twice and reach much the same depth, 20 against 19. Hanoi at n = 20 makes 2,097,151 calls; Fibonacci makes 21,891, which is 95.8 times fewer. The only difference in the code is the second recursive argument: Hanoi calls itself on nβˆ’1 twice, Fibonacci on nβˆ’1 and then nβˆ’2. That single subtraction drops the base of the exponential from 2 to Ο† = 1.618, and the gap keeps opening. At n = 30 it is 2,147,483,647 against 2,692,537, a factor of 798.

Problems solved in full

  1. The call count closed form when naive fib(10) makes 177 calls 6 steps

    The tool says naive fib(10) makes 177 calls and fib(11) makes 287. Find the closed form for the call count, prove it, and then work out how far naive Fibonacci gets in one second on a machine that manages 108 calls per second.

    1. Add one to each count: 178 and 288. Both are even, and their halves are 89 and 144, which are F11 and F12. So the conjecture is C(n) = 2F(n+1) βˆ’ 1, with F(1) = F(2) = 1.

    2. The tree explains where the doubling comes from. Every internal node makes exactly two calls and every leaf makes none, so a tree with L leaves has L βˆ’ 1 internal nodes and 2L βˆ’ 1 nodes altogether. The tool reports 89 base cases at n = 10, and 2 Γ— 89 βˆ’ 1 = 177.

    3. Base cases first. fib(0) and fib(1) return without recursing, so C(0) = C(1) = 1, and both 2F(1) βˆ’ 1 and 2F(2) βˆ’ 1 equal 1.

    4. Now the induction step. Assume the formula at n βˆ’ 1 and at n βˆ’ 2. The call at n is one node plus its two subtrees, and the two Fibonacci numbers underneath collapse by the definition of the sequence itself. Setting n = 11 gives 2 Γ— 144 βˆ’ 1 = 287, which is what the tool prints.

    5. Leave the tree here. Binet's formula gives F(m) = (Ο†m βˆ’ ψm)/√5 with Ο† = 1.6180 and |ψ| < 1, so F(m) is Ο†m/√5 rounded to the nearest integer, and C(n) β‰ˆ 2Ο†n+1/√5. One second at 108 calls per second buys 108 calls, which fixes Ο†n+1 at 1.1180 Γ— 108.

    6. Take logarithms. ln(1.1180 Γ— 108) = 18.5323 and ln Ο† = 0.4812, so n + 1 = 38.51 and n = 37.5. Round down, because a fractional call does not finish.

    Answer

    n = 37. The tool confirms the two neighbours: 78,176,337 calls at n = 37, and 126,491,971 at n = 38. Past there, every further 1.44 in n doubles the work, because ln 2 / ln Ο† = 1.44. Meanwhile the distinct-subproblem card at n = 37 reads 38. Seventy-eight million calls to answer thirty-eight questions is the argument for dynamic programming in a single line.

  2. Three branches that grow slower than two 6 steps

    Three-step stairs calls itself three times at every node; Towers of Hanoi calls itself twice. Load Stairs at 20, three branches, work out which of the two trees grows faster, and decide what the branching factor is worth as a guide.

    1. Read the recurrence off the definition rather than off the picture. One call for the node itself, then three more at n βˆ’ 1, n βˆ’ 2 and n βˆ’ 3, and anything at 2 or below returns without recursing.

    2. Assume the count grows geometrically, C(n) β‰ˆ A xⁿ, and substitute. Divide through by x raised to n βˆ’ 3 and the leading 1 is left behind as lower-order noise, which leaves a cubic. Its only real root is 1.8392868, the tribonacci constant.

    3. The same recipe on the other two definitions. Fibonacci calls itself at n βˆ’ 1 and n βˆ’ 2, so xΒ² = x + 1 and the root is Ο† = 1.6180340. Hanoi calls itself twice at n βˆ’ 1, so x = 2 and there is nothing to solve.

    4. Check it rather than believe it. Set n to 29, read the calls card, set n to 30, read it again, divide. Stairs gives 1.83929, Fibonacci 1.61803, Hanoi exactly 2. Five decimal places on all three.

    5. What the root does not give you is the count. 1.8393²⁰ is 196,331 while the card reads 128,287, so there is a factor of 0.65 sitting in front; Hanoi's calls are exactly 2ⁿ⁺¹ βˆ’ 1, a factor of 2. The base cases set that constant and the recurrence sets the root, and only the root decides who wins eventually.

    6. Eventually arrives quickly here. At n = 20 Hanoi makes 16.3 times as many calls as stairs, and at n = 30 it makes 37.8 times as many. The ratio doubles every 8.3 steps, since ln 2 divided by ln(2 Γ· 1.8393) is 8.27.

    Answer

    Hanoi grows faster, 2 against 1.8393, and the third branch costs stairs nothing it cannot afford. The branching factor is an upper bound on the growth rate, and it is reached only when every child is one step smaller. Stairs spends two of its three calls on arguments that are 2 and 3 smaller, and those subtrees are cheap enough that they never add up to the branch it appeared to gain.

    The recipe is the part to keep, because it works on a definition nobody has already solved for you. Count the children, note how far each one drops the argument, write x to the largest drop as the sum of x to the remaining drops, and take the largest real root. One caution goes with it: this is a fact about the naive tree, and the naive tree is not what anyone would write. The distinct-subproblem card reads 21 at n = 20 for stairs and 21 for Hanoi, so a memo removes the exponent altogether and the growth rate stops mattering.

Learning path

Counting work, not seconds

Leads to Pathfinding

Example problems