Case Study: Tower of Hanoi

Paul Ngugi - Jul 2 - - Dev Community

The Tower of Hanoi problem is a classic problem that can be solved easily using recursion, but it is difficult to solve otherwise. The problem involves moving a specified number of disks of distinct sizes from one tower to another while observing the following rules:

  • There are n disks labeled 1, 2, 3, . . . , n and three towers labeled A, B, and C.
  • No disk can be on top of a smaller disk at any time.
  • All the disks are initially placed on tower A.
  • Only one disk can be moved at a time, and it must be the smallest disk on a tower.

The objective of the problem is to move all the disks from A to B with the assistance of C. For example, if you have three disks, the steps to move all of the disks from A to B are shown in Figure below.

Image description

In the case of three disks, you can find the solution manually. For a larger number of disks, however—even for four—the problem is quite complex. Fortunately, the problem has an inherently recursive nature, which leads to a straightforward recursive solution.

The base case for the problem is n = 1. If n == 1, you could simply move the disk from A to B. When n > 1, you could split the original problem into the following three subproblems and solve them sequentially.

  1. Move the first n - 1 disks from A to C recursively with the assistance of tower B, as shown in Step 1 in Figure below.
  2. Move disk n from A to B, as shown in Step 2 in Figure below.
  3. Move n - 1 disks from C to B recursively with the assistance of tower A, as shown in Step 3 in Figure below.

Image description

The following method moves n disks from the fromTower to the toTower with the assistance of the auxTower:

void moveDisks(int n, char fromTower, char toTower, char auxTower)

The algorithm for the method can be described as:

if (n == 1) // Stopping condition
Move disk 1 from the fromTower to the toTower;
else {
moveDisks(n - 1, fromTower, auxTower, toTower);
Move disk n from the fromTower to the toTower;
moveDisks(n - 1, auxTower, toTower, fromTower);
}

The code below gives a program that prompts the user to enter the number of disks and invokes the recursive method moveDisks to display the solution for moving the disks.

Image description

This problem is inherently recursive. Using recursion makes it possible to find a natural, simple solution. It would be difficult to solve the problem without using recursion.

Consider tracing the program for n = 3. The successive recursive calls are shown in Figure below. As you can see, writing the program is easier than tracing the recursive calls. The system uses stacks to manage the calls behind the scenes. To some extent, recursion provides a level of abstraction that hides iterations and other details from the user.

Image description

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .