Tower of Hanoi visualiser
Move a tower of discs from peg A to peg C, using B, one disc at a time and never a bigger disc on a smaller one. Press Play and watch the discs move while the recursive calls build up on the call stack and in the tree. A tower of n discs always takes 2n − 1 moves.
How to use it
- 1Type the number of discs (1 to 5), or tap an example.
- 2Press Show me, then Play. The discs move between the pegs A, B and C one step at a time.
- 3Watch the recursive calls on the call stack and in the tree, and the list of moves growing.
The animation needs JavaScript. Every step is written out below.
Result
- Result
- 15 moves (2^4 - 1 = 15)
Every step in words (60 steps)
- hanoi(4, A, C) is called: move 4 discs from A to C using B.
- n = 4, not 1. First move the top 3 discs out of the way (from A to B, using C).
- hanoi(4, A, C) calls hanoi(3, A, B): it will clear the way.
- n = 3, not 1. First move the top 2 discs out of the way (from A to C, using B).
- hanoi(3, A, B) calls hanoi(2, A, C): it will clear the way.
- n = 2, not 1. First move the top 1 disc out of the way (from A to B, using C).
- hanoi(2, A, C) calls hanoi(1, A, B): it will clear the way.
- n = 1: the base case. Just move the one disc.
- Move disc 1 from A to B (move 1).
- Return. The frame leaves the stack.
- Move disc 2 from A to C (move 2).
- hanoi(2, A, C) calls hanoi(1, B, C): it puts the smaller discs back on top.
- n = 1: the base case. Just move the one disc.
- Move disc 1 from B to C (move 3).
- Return. The frame leaves the stack.
- hanoi(2, A, C) has moved 2 discs from A to C. It returns.
- Move disc 3 from A to B (move 4).
- hanoi(3, A, B) calls hanoi(2, C, B): it puts the smaller discs back on top.
- n = 2, not 1. First move the top 1 disc out of the way (from C to A, using B).
- hanoi(2, C, B) calls hanoi(1, C, A): it will clear the way.
- n = 1: the base case. Just move the one disc.
- Move disc 1 from C to A (move 5).
- Return. The frame leaves the stack.
- Move disc 2 from C to B (move 6).
- hanoi(2, C, B) calls hanoi(1, A, B): it puts the smaller discs back on top.
- n = 1: the base case. Just move the one disc.
- Move disc 1 from A to B (move 7).
- Return. The frame leaves the stack.
- hanoi(2, C, B) has moved 2 discs from C to B. It returns.
- hanoi(3, A, B) has moved 3 discs from A to B. It returns.
- Move disc 4 from A to C (move 8).
- hanoi(4, A, C) calls hanoi(3, B, C): it puts the smaller discs back on top.
- n = 3, not 1. First move the top 2 discs out of the way (from B to A, using C).
- hanoi(3, B, C) calls hanoi(2, B, A): it will clear the way.
- n = 2, not 1. First move the top 1 disc out of the way (from B to C, using A).
- hanoi(2, B, A) calls hanoi(1, B, C): it will clear the way.
- n = 1: the base case. Just move the one disc.
- Move disc 1 from B to C (move 9).
- Return. The frame leaves the stack.
- Move disc 2 from B to A (move 10).
- hanoi(2, B, A) calls hanoi(1, C, A): it puts the smaller discs back on top.
- n = 1: the base case. Just move the one disc.
- Move disc 1 from C to A (move 11).
- Return. The frame leaves the stack.
- hanoi(2, B, A) has moved 2 discs from B to A. It returns.
- Move disc 3 from B to C (move 12).
- hanoi(3, B, C) calls hanoi(2, A, C): it puts the smaller discs back on top.
- n = 2, not 1. First move the top 1 disc out of the way (from A to B, using C).
- hanoi(2, A, C) calls hanoi(1, A, B): it will clear the way.
- n = 1: the base case. Just move the one disc.
- Move disc 1 from A to B (move 13).
- Return. The frame leaves the stack.
- Move disc 2 from A to C (move 14).
- hanoi(2, A, C) calls hanoi(1, B, C): it puts the smaller discs back on top.
- n = 1: the base case. Just move the one disc.
- Move disc 1 from B to C (move 15).
- Return. The frame leaves the stack.
- hanoi(2, A, C) has moved 2 discs from A to C. It returns.
- hanoi(3, B, C) has moved 3 discs from B to C. It returns.
- hanoi(4, A, C) has moved 4 discs from A to C. It returns.
About the Tower of Hanoi visualiser
What it does
Solves the Tower of Hanoi for the number of discs you give: all the discs start on peg A and must end on peg C, using peg B, moving one disc at a time and never putting a bigger disc on a smaller one. The recursive method does it in three parts: move the top n − 1 discs to the spare peg, move the biggest disc, then move the n − 1 discs on top of it. The page shows the discs, the call stack, the call tree and the Java code together.
The number of moves
n discs always take 2n − 1 moves: 1, 3, 7, 15 and 31 moves for 1 to 5 discs. The number of moves is written under the answer.
Common questions
How many moves does the Tower of Hanoi need?
2n − 1 for n discs, and no fewer: 3 moves for 2 discs, 7 for 3, 15 for 4 and 31 for 5.
What is the recursive idea behind the Tower of Hanoi?
To move n discs from A to C using B, first move n − 1 discs from A to B using C, then move the biggest disc from A to C, then move the n − 1 discs from B to C using A. The base case is a single disc, which is moved directly.
Why only up to 5 discs?
The number of moves doubles with every disc, so a larger tower would be too long to follow on a page. Five discs already make 31 moves and 31 calls.
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.
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.
- Recursion visualiserWatch factorial, Fibonacci, power, GCD and sum of digits run: the calls, the call stack and the returns.
- 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.