Recursion Basics: Base Case and Recursive Case
The Towers of Hanoi problem is easy to solve recursively because the problem contains smaller copies of itself: move N minus 1 disks, move the bottom disk, then move N minus 1 disks again.
Try it: The Call Stack
How the call stack keeps track of function calls: each call pushes a frame with its own arguments and local variables, and each return pops it.
How it works
- Calling a function pushes a new frame on top of the stack; the caller pauses.
- The frame holds that call's parameters and local variables.
- When the function returns, its frame is popped and the return value goes back to the paused caller.
- A recursive function pushes one frame per call until the base case, then the frames unwind in reverse order.
Default run (17 steps): Program starts: main is about to call factorial(4). … The stack is empty again. Result: 24. Deepest point: 4 frames, 4 calls in total.
Loading the simulation…
Why the Puzzle Looks Harder Than It Is
The Towers of Hanoi puzzle starts with a stack of disks of various sizes, piled on a base in order of decreasing size. The goal is to move the whole stack from one base to another. Only one disk can move at a time, and no disk may ever be placed on top of a smaller disk. A third base is available as a spare.
Many learners assume that a program solving this puzzle must keep track of every disk and every move at once, because the puzzle is made of many small physical steps. That belief feels natural, and it is the belief this article tests. The recursive solution turns out to need far less bookkeeping than the puzzle's detail suggests. Keep that expectation in mind as you work through the example below.
What do you think happens?
Before reading on, predict the correct moves when there are two disks to move from Stack 0 to Stack 1, using Stack 2 as the spare. Name each disk and the stacks it moves between, in order. No code is needed; only the rules matter: the small disk must get out of the way before the large disk can move.
Reveal answer
Move the small disk from Stack 0 to Stack 2. Move the large disk from Stack 0 to Stack 1. Move the small disk from Stack 2 to Stack 1.
The large disk is at the bottom of the source stack and cannot move while the small disk sits on top of it. So the small disk must first go to the spare stack, which is not the destination. Once the large disk reaches Stack 1, the small disk must come back over and land on top of it, which is legal because it is smaller than the disk beneath it.
Tracing the Two-Disk Moves
The diagram below follows the two-disk case from the prediction step. Each node is the state of the three stacks after one more move. Watch what changes at each step: only the top disk of one stack ever moves, and it always lands on a larger disk or on an empty stack. Stack 0 starts with the large disk underneath and the small disk on top. Stack 1 is the destination and Stack 2 is the spare.
Notice that the three moves form a pattern. The first and third moves are the same kind of job: moving a single small disk out of the way and then onto its final place. The middle move is the one that finally moves the bottom disk to the destination. This pattern is the seed of the recursive solution.
Breaking One Move Into Three Smaller Calls
The key idea is to notice that the problem contains a smaller copy of itself. Suppose there are N disks on the source stack. The bottom disk must eventually travel from the source stack to the destination stack. Before that can happen, all of the other N minus 1 disks must be sitting on the spare stack, because the bottom disk cannot move while smaller disks are on top of it. After the bottom disk arrives at the destination, the N minus 1 disks must travel from the spare stack to the destination stack.
This gives a three-step pattern: move N minus 1 disks, move the bottom disk, then move N minus 1 disks again. Moving N minus 1 disks is the same kind of problem as moving N disks, only smaller. That is why recursion fits so well here: the problem is reduced to smaller problems of the same type.
In the tree, the top-level call has three children, and the two subproblems each expand into their own three-step pattern. The middle child is the single move of the largest disk. The structure is the same at every level, which is exactly what makes the recursive description short even though the list of moves is long.
The Four Parameters Explained
The source expresses the solution as a subroutine that prints step-by-step instructions. It takes four parameters, and each one has a specific job. The first is the number of disks to move. The second is the source stack, the third is the destination stack, and the fourth is the spare stack. Stacks are identified by number: 0, 1, or 2.
The spare parameter is technically redundant, since it could be worked out from the other three stacks. The source keeps it anyway because it makes each call easier to read. To solve the top-level problem of moving N disks from Stack 0 to Stack 1, the subroutine is called as TowersOfHanoi(N,0,1,2). The source's own program calls the method with that name, so match that spelling when you read the source code.
The roles of the stacks are not fixed. The first subproblem moves disks from the source to the spare, and the second moves disks from the spare to the destination. So the subroutine must accept source, destination, and spare as inputs, and the roles of those three stacks change from one call to the next. A stack that is the destination in one call may be the spare in the next.
Where the Recursion Stops
Every recursive method needs a stopping point, and here the stopping point is the case with only one disk. In that case the solution is trivial: the single disk moves in one step. The subroutine checks for this case first. If the number of disks is exactly one, it prints one move and does not call itself again. Otherwise it performs the three-step pattern. The one-disk case is the base case, and the three-step pattern is the recursive case.
The recursion works because each recursive call handles a smaller number of disks. Each step of the descent reduces the disk count by one, so the calls eventually reach the one-disk base case. The source states this directly: the recursion works because each recursive call involves a smaller number of disks, and the problem is trivial in the base case.
The following pseudocode captures the structure described above. It is an illustration generated for teaching purposes, following the base case and the three-step pattern from the source. The parameter order matches the roles the source gives to each stack.
- The base case checks whether disks equals 1 and prints a single move.
- The first recursive call moves the other disks from the source to the spare.
- The middle print is the single move of the bottom disk.
- The second recursive call moves the other disks from the spare to the destination.
Following Three Disks Through the Calls
Moving three disks from Stack 0 to Stack 1
Call TowersOfHanoi(3,0,1,2) and list the moves in order. Stack 0 holds disks 1 (smallest), 2, and 3 (largest). The goal is to move all three to Stack 1 with Stack 2 as the spare.
- 1Apply the three-step pattern to the top-level call
Since disks is 3, not 1, the base case does not apply. The first subproblem moves 2 disks from Stack 0 to Stack 2, using Stack 1 as spare:
TowersOfHanoi(2,0,2,1). - 2Expand the first subproblem
For 2 disks, the small disk goes to the spare (Stack 1), the large disk goes to the destination (Stack 2), and the small disk comes back on top. The moves are: disk 1 from 0 to 1, disk 2 from 0 to 2, disk 1 from 1 to 2.
- 3Move the bottom disk
With the two smaller disks parked on Stack 2, the largest disk, disk 3, moves from Stack 0 to Stack 1. This is the middle call of the top-level pattern, and it is a single move.
- 4Apply the pattern to the second subproblem
The remaining 2 disks move from Stack 2 to Stack 1 with Stack 0 as spare:
TowersOfHanoi(2,2,1,0). The moves are: disk 1 from 2 to 0, disk 2 from 2 to 1, disk 1 from 0 to 1. - 5Count the total
The sequence has three moves, then one move, then three moves, for seven moves in total. This matches the formula for N disks, 2 to the power N minus 1, with N equal to 3.
The seven moves in order are: disk 1 from 0 to 1, disk 2 from 0 to 2, disk 1 from 1 to 2, disk 3 from 0 to 1, disk 1 from 2 to 0, disk 2 from 2 to 1, disk 1 from 0 to 1.
Why Zero Disks Never Stops
The solution depends on a precondition: the number of disks must be positive. The source notes that violating this precondition produces infinite recursion. The reason is visible in the code. The only stopping check is whether disks equals 1. A call with zero disks never matches that check, so it always takes the recursive branch.
The lesson is that a recursive method is only correct when every call moves toward its base case. Here the recursion steps downward by one disk per call, so a starting value that is positive will reach 1, while a starting value of zero or below will skip past 1 forever. The positive-disk precondition is what guarantees that the base case is reachable.
Why the Work Explodes
Recursion makes the solution elegant, but it does not make the solution cheap. The number of moves needed for N disks is given by a simple formula. Each extra disk roughly doubles the work, so the count grows exponentially.
The source gives a story to illustrate this growth. Monks are said to move a stack of 64 disks, one disk per day, and the universe is said to end when they finish. The source also states that the algorithm runs in time Theta of 2 to the power n, where n is the number of disks. Because this grows so quickly, the problem can be solved in practice only for a small number of disks.
Mistakes With Base Cases and Parameters
Common mistake Believing the program must plan every move before it starts
Why it failsThe recursive solution needs only one base case, one three-step pattern, and the rule that each call works on fewer disks. The detailed list of moves is produced by the computer as the calls run.
Do this insteadDescribe the solution by its pattern. Trust the recursion to generate the individual moves.
Common mistake Treating the source and destination stacks as fixed across all calls
What it looks likeIn the first subproblem of TowersOfHanoi(3,0,1,2), the destination is Stack 2, not Stack 1.
Why it failsThe first subproblem moves disks from the source to the spare, and the second moves them from the spare to the destination, so the roles of the stacks change from call to call.
Do this insteadRead the arguments of each recursive call separately and identify which stack plays each role in that call.
Common mistake Checking for the base case with the wrong condition
What it looks likeA call with disks equal to 0 never matches the check disks equals 1.
Why it failsThe only stopping check is whether disks equals 1. Any starting value that is zero or negative skips past that check forever.
Do this insteadEnsure the number of disks is positive before calling the subroutine, so that every chain of calls reaches the one-disk base case.
Common mistake Forgetting that the spare parameter is only a convenience, not a new piece of information, so it can feel like a required extra input when it is actually redundant
Practice Predicting the Next Moves
Call TowersOfHanoi(2,0,1,2). Without running any code, write the three moves in order, naming each disk and its source and destination stacks. Then state which of the three moves is the one that corresponds to the bottom disk, and which stack serves as the spare in the first subproblem.
Hints
- Apply the three-step pattern: first move the one smaller disk to the spare stack.
- The bottom disk can only move once the smaller disk is off of it and sitting on the spare.
- The first subproblem uses Stack 1 as the destination, because the spare for the top-level call is Stack 2.
A teammate writes TowersOfHanoi(0,0,1,2) and says the program will print nothing and finish. Explain in two or three sentences why that prediction is wrong, referring to the base-case check.
Hints
- Ask which branch the subroutine takes when disks equals 0.
- Consider what the next call's disk count would be.
Key Takeaways
- The Towers of Hanoi problem is easy to solve recursively because it contains smaller copies of itself: move N minus 1 disks to the spare, move the bottom disk to the destination, then move N minus 1 disks onto the destination.
- The subroutine takes four parameters: the number of disks, the source stack, the destination stack, and the spare stack, and the roles of the three stacks change from one recursive call to the next.
- The base case is a single disk, which moves in one step, and every recursive call handles fewer disks so that the calls reach the base case.
- A non-positive number of disks never matches the base-case check and produces infinite recursion, and the total number of moves for N disks is 2 to the power N minus 1, so the work grows exponentially.