Understanding recursion in the context of Towers of Hanoi
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Towers of Hanoi is one of the best examples for learning recursion because the problem naturally decomposes into smaller copies of itself. Instead of memorizing calls, the goal is to understand why the recursive breakdown is correct. Once that mental model is clear, many other recursive algorithms become easier to reason about.
Problem Rules and Recursive Shape
The puzzle has three rods and n disks of different sizes. You must move the full stack from source rod to destination rod using an auxiliary rod.
Rules:
- Move one disk at a time.
- Only top disk of a rod can be moved.
- Never place a larger disk on a smaller disk.
The recursive insight:
- Move top
n - 1disks from source to auxiliary. - Move largest disk from source to destination.
- Move
n - 1disks from auxiliary to destination.
Steps 1 and 3 are the same problem at smaller size, so recursion fits directly.
Base Case and Call Stack Intuition
The base case is n == 1: move one disk directly.
Recursive relation:
T(1) = 1T(n) = 2 * T(n - 1) + 1
This gives minimum moves 2^n - 1.
For n = 3, the algorithm makes seven moves. You can visualize recursion as a call tree where each node expands into two smaller subproblems around one direct move.
Python Implementation
The following implementation prints each move and counts total moves.
This code is intentionally explicit and easy to trace in a debugger.
Trace Example for Three Disks
For n = 3, output sequence is:
- Move disk 1 from A to C
- Move disk 2 from A to B
- Move disk 1 from C to B
- Move disk 3 from A to C
- Move disk 1 from B to A
- Move disk 2 from B to C
- Move disk 1 from A to C
Each move is valid, and this is optimal by the recurrence proof.
Why Recursion Beats Ad Hoc Rules Here
You can solve small Hanoi cases manually, but manual rule systems become fragile as n grows. Recursion gives:
- A correctness argument based on subproblem structure.
- A predictable implementation pattern.
- A direct path to complexity analysis.
The same technique applies to tree traversal, divide-and-conquer sorting, and backtracking algorithms.
Iterative Alternative and Tradeoffs
Hanoi also has iterative solutions using bit patterns or explicit stacks. These can avoid function call overhead, but they are harder for beginners to verify. For learning recursion concepts, the recursive solution is clearer because problem structure and code structure are aligned.
If recursion depth is a concern in a language with small stack limits, iterative simulation may be needed for large n.
Testing and Verification Tips
Useful tests:
n = 1should produce exactly one move.n = 2should produce three moves.- General check: move count equals
2^n - 1. - Validate no move places larger disk on smaller one.
A simple validator can replay moves and assert stack ordering constraints. This turns conceptual correctness into executable checks.
Common Pitfalls
A common pitfall is skipping the base case or placing it after recursive calls, which causes infinite recursion. Another issue is mixing up destination and auxiliary rods in recursive arguments. Teams also sometimes treat recursion as magic and never trace one full example, which leads to shallow understanding. Off-by-one mistakes in disk numbering are frequent in custom visualizations. Finally, learners often focus only on syntax and miss the recurrence relation, which is the core reason the algorithm works.
Summary
- Towers of Hanoi is a direct demonstration of recursive problem decomposition.
- Base case handles one disk, recursive case handles
n - 1around one direct move. - Minimum move count is
2^n - 1. - Recursive code is concise and aligns with formal reasoning.
- Tracing small examples and validating move counts builds strong recursion intuition.
Related reading
- Understanding Schönhage-Strassen algorithm huge integer multiplication
- Understanding solution to finding optimal strategy for game involving picking pots of gold
- Understanding super fast blur algorithm
- Understanding the algorithm of Visual C's rand function
- Understanding the pseudocode in the Donald B. Johnson's algorithm
- Understanding the Recursion of mergesort
- Understanding Time complexity calculation for Dijkstra Algorithm
- Understanding Ukkonen's algorithm for suffix trees

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.