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
- 1Pick a method (factorial, Fibonacci, power, GCD or sum of digits) and type a number, or tap an example.
- 2Press Show me, then Play. Use Back and Next, the slider or the arrow keys to go through it at your own speed.
- 3Watch the call tree grow, the call stack fill and empty, and the Java line that is running.
The animation needs JavaScript. Every step is written out below.
Result
- Result
- gcd(48, 18) = 6
Every step in words (12 steps)
- gcd(48, 18) is called, so its frame goes on the call stack.
- b = 18. Is b == 0? No, so it calls gcd(18, 48 % 18) = gcd(18, 12).
- gcd(48, 18) calls gcd(18, 12) and waits for its answer.
- b = 12. Is b == 0? No, so it calls gcd(12, 18 % 12) = gcd(12, 6).
- gcd(18, 12) calls gcd(12, 6) and waits for its answer.
- b = 6. Is b == 0? No, so it calls gcd(6, 12 % 6) = gcd(6, 0).
- gcd(12, 6) calls gcd(6, 0) and waits for its answer.
- b = 0. Is b == 0? Yes, so this is the base case.
- Base case: b is 0, so the answer is a = 6.
- The call below returned 6, and gcd(12, 6) passes it up unchanged: it returns 6.
- The call below returned 6, and gcd(18, 12) passes it up unchanged: it returns 6.
- The call below returned 6, and gcd(48, 18) passes it up unchanged: it returns 6.
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
- What will the function workOut(220) return?2026
- Following method is a part of a class MyArray. … Predict the output of the code considering there is no…2025 Practice
- The following function int solve( ) is a part of some class. Assume 'm' and 'n' are positive integers. Answer…2025
- The following functions are a part of some class: … What will the output of Try( ) when the value of…2024 Specimen
- … The following function quiz( ) is a part of some class. Assume ‘n’ is a positive integer, greater than 0…2023 Specimen
- The following function think( ) is part of some class. Assume $f$ is the lower bound and $l$ is the upper…2025 Improvement
- State the role of base case and recursive case in a recursive method.2026 Improvement
- A class Gcd has been defined to find the Greatest Common Divisor of two integer numbers. Some of the members…2022 Specimen
- Recursion takes toll on memory. Which of the following option is the reason for it?2025 Practice
- The following is a function of some class. What will be the output of the function test ( ) when the value of…2020 Specimen
More Computer Science tools
- Boolean algebra solverSimplify any expression: the laws step by step, truth table, K-map and the logic circuit.
- Infix to postfix converterInfix to postfix or prefix with the stack table, and postfix evaluation step by step.
- Binary tree and BST toolBuild a binary search tree, rebuild a tree from two traversals, and get every traversal and fact.
- Stack and queue simulatorRun push, pop, insert and delete on a stack, queue, circular queue or dequeue, and watch the pointers move.
- Tower of Hanoi visualiserWatch the discs move between the pegs while the recursive calls and the call stack build up.
- Binary search tree visualiserWatch values being inserted into a binary search tree, searched for, and walked inorder, preorder and postorder.
- Binary tree traversal visualiserClick a binary tree together and watch preorder, inorder and postorder walk it, with the recursive calls on the call stack.
- Stack and queue visualiserWatch push, pop, insert and delete move top, front and rear along the array, with overflow and underflow.
- Linked list visualiserWatch nodes being added, deleted, searched, traversed and reversed, with start, p, prev and curr moving along the arrows.
- 2D array visualiserWatch row-wise, column-wise, diagonal, boundary and spiral walks number the cells of a 2D array in the order they print.
- Sorting visualiserWatch bubble, selection and insertion sort compare, swap and shift your own list, step by step.
- Linear and binary search visualiserWatch a linear or binary search look for a value in your list, and count the comparisons.