Tower of Hanoi- Recursion Explained with Examples

What Is the Tower of Hanoi?

The Tower of Hanoi is a classic puzzle with three pegs and N disks stacked on one peg. The goal is simple: move the entire stack to another peg, following two rules.

It sounds easy. It's not. The puzzle was invented by French mathematician Édouard Lucas in 1883, and it's become the go-to example for teaching recursion in computer science.

Here's why: the solution requires you to solve the exact same problem, just smaller. That's recursion.

The Rules You Must Follow

That's it. No tricks, no loopholes.

Why Recursion Fits Perfectly

Recursion works when a problem can be broken into identical subproblems. Tower of Hanoi is built for this.

To move N disks from source to destination:

  1. Move N-1 disks from source to temporary peg
  2. Move the largest disk from source to destination
  3. Move N-1 disks from temporary to destination

Steps 1 and 3 are the same problem—just smaller. That's the recursive call.

The Base Case

Every recursive solution needs a stopping point. For Tower of Hanoi, that's N = 1. One disk? Just move it. No recursion needed.

Step-by-Step: 3 Disks

Let's walk through moving 3 disks from peg A to peg C.

Move 1

Move disk 1 from A to C

Move 2

Move disk 2 from A to B

Move 3

Move disk 1 from C to B

Move 4

Move disk 3 from A to C

Move 5

Move disk 1 from B to A

Move 6

Move disk 2 from B to C

Move 7

Move disk 1 from A to C

Total moves: 7

Notice the pattern? The solution follows a predictable sequence. You don't need to figure it out manually—the recursion handles it.

The Formula

Minimum moves for N disks:

Moves = 2ⁿ - 1

So:

The numbers grow fast. This is exponential time complexity.

Code Implementation

Python


def tower_of_hanoi(n, source, destination, auxiliary):
    if n == 1:
        print(f"Move disk 1 from {source} to {destination}")
        return
    
    tower_of_hanoi(n - 1, source, auxiliary, destination)
    print(f"Move disk {n} from {source} to {destination}")
    tower_of_hanoi(n - 1, auxiliary, destination, source)

# Example
tower_of_hanoi(3, 'A', 'C', 'B')

JavaScript


function towerOfHanoi(n, source, destination, auxiliary) {
    if (n === 1) {
        console.log(`Move disk 1 from ${source} to ${destination}`);
        return;
    }
    
    towerOfHanoi(n - 1, source, auxiliary, destination);
    console.log(`Move disk ${n} from ${source} to ${destination}`);
    towerOfHanoi(n - 1, auxiliary, destination, source);
}

towerOfHanoi(3, 'A', 'C', 'B');

The code is almost identical in both languages. That's the power of understanding the algorithm—you're not tied to syntax.

Complexity Analysis

Aspect Complexity Explanation
Time O(2ⁿ) Each disk doubles the work
Space O(n) Recursive call stack depth
Moves 2ⁿ - 1 Minimum possible moves

You can't optimize the time complexity. The puzzle requires 2ⁿ - 1 moves. That's mathematically proven.

Getting Started: Build Your Own Solver

Want to implement this yourself? Here's the process:

  1. Define the base case — when N = 1, just print the move
  2. Break the problem — move N-1 disks to the auxiliary peg
  3. Move the bottom disk — from source to destination
  4. Stack the rest — move N-1 from auxiliary to destination

That's the entire algorithm. Four steps.

Common Mistakes

Where You'll See This

Tower of Hanoi isn't just academic. It shows up in:

Understanding this problem means you understand one of the cleanest examples of recursion you'll encounter.

The Bottom Line

Tower of Hanoi demonstrates recursion perfectly: a complex problem solved by breaking it into identical smaller problems. The algorithm is straightforward—move N-1, move the biggest, move N-1 back.

Master this, and recursion stops feeling abstract. It becomes intuitive.