Prashnikaप्रश्निका
Code
About

Recursion visualiser

See what recursion really does. Pick a method and a number, then press Play: each call goes on the call stack, the calls branch out into a tree down to the base case, and the answers come back up one by one, with the Java line that is running lit all the way.

How to use it

  1. 1Pick a method (factorial, Fibonacci, power, GCD or sum of digits) and type a number, or tap an example.
  2. 2Press Show me, then Play. Use Back and Next, the slider or the arrow keys to go through it at your own speed.
  3. 3Watch the call tree grow, the call stack fill and empty, and the Java line that is running.
Which method

The ranges keep each run small. Factorial: 0 to 20. Fibonacci: 0 to 7. Power: base 1 to 20, exponent 0 to 10. GCD: both numbers 1 to 99999. Sum of digits: 0 to 999999999.

The animation needs JavaScript. Every step is written out below.

Result

Result
fact(4) = 24
Every step in words (12 steps)
  1. fact(4) is called, so its frame goes on the call stack.
  2. n = 4. Is n <= 1? No, so it needs fact(3) first.
  3. fact(4) calls fact(3) and waits for its answer.
  4. n = 3. Is n <= 1? No, so it needs fact(2) first.
  5. fact(3) calls fact(2) and waits for its answer.
  6. n = 2. Is n <= 1? No, so it needs fact(1) first.
  7. fact(2) calls fact(1) and waits for its answer.
  8. n = 1. Is n <= 1? Yes, so this is the base case.
  9. Base case: it returns 1 straight away, with no further call.
  10. fact(1) came back with 1, so fact(2) works out 2 * 1 = 2 and returns it. Its frame leaves the stack.
  11. fact(2) came back with 2, so fact(3) works out 3 * 2 = 6 and returns it. Its frame leaves the stack.
  12. fact(3) came back with 6, so fact(4) works out 4 * 6 = 24 and returns it. Its frame leaves the stack.

About the Recursion visualiser

What it does

Runs a recursive method for the number you give and replays it one step at a time. Every call appears in the call tree and goes on the call stack; when a call reaches the base case it returns its value, and each earlier call then finishes its own working and returns in turn. The Java line being run is marked, and a sentence says what just happened. It is the dry run that ISC Computer Science recursion questions ask you to write.

What you see

  • Call tree: one box for every call. A box with a bold outline is waiting for the calls below it; the filled box is the call being worked on; a box with an arrow and a value has returned.
  • Call stack: the calls that have started but not yet returned, newest on top. Its height is how deep the recursion is.
  • Code: the Java method, with the current line marked.

The ranges

To keep every run small the numbers are limited: factorial 0 to 20, Fibonacci 0 to 7, power with a base of 1 to 20 and an exponent of 0 to 10, GCD of two numbers from 1 to 99999, and sum of digits up to 9 digits.

Common questions

What is a base case?

The base case is the call that can be answered without calling the method again, for example n <= 1 in factorial. Every recursive method needs one, or the calls never stop and the program ends with a stack overflow.

What is the call stack in recursion?

It is the list of calls that have started but have not returned yet. Each call is added on top when it is made and removed when it returns, so the most recent call is always the one running. Watch it fill up on the way down to the base case and empty on the way back.

Why does the Fibonacci tree have so many calls?

Each call to fib(n) makes two more calls, and the same values are worked out again and again: fib(7) makes 41 calls in all. This is why this simple recursive method is slow for large n.

Why is factorial of 20 the largest?

20! is the largest factorial that fits in a Java long. The visualiser stops there, and the call stack for it is 20 calls deep.

Can I see the code in Python, JavaScript or C++?

Yes. Choose the language in the menu above the code. Only the listing changes: the steps are always worked out from the Java program that ISC uses, and the highlighted line moves to the matching line of the language you chose.

Practise on real ISC questions

More Computer Science tools

All study tools →