Tower of Hanoi Using Recursion
Tower of Hanoi Using Recursion
Tower of Hanoi
Tower of Hanoi, is a mathematical puzzle which consists of three towers (pegs) and
more than one rings is as depicted −
These rings are of different sizes and stacked upon in an ascending order, i.e. the
smaller one sits over the larger one. There are other variations of the puzzle where
the number of disks increase, but the tower count remains the same.
Rules
The mission is to move all the disks to some another tower without violating the
sequence of arrangement. A few rules to be followed for Tower of Hanoi are −
Only one disk can be moved among the towers at any given time.
Only the "top" disk can be removed.
No large disk can sit over a small disk.
Algorithm
To write an algorithm for Tower of Hanoi, first we need to learn how to solve this
problem with lesser amount of disks, say → 1 or 2. We mark three towers with
name, source, destination and aux (only to help moving the disks). If we have
only one disk, then it can easily be moved from source to destination peg.
If we have 2 disks −
So now, we are in a position to design an algorithm for Tower of Hanoi with more
than two disks. We divide the stack of disks in two parts. The largest disk (n th disk)
is in one part and all other (n-1) disks are in the second part.
Our ultimate aim is to move disk n from source to destination and then put all other
(n1) disks onto it. We can imagine to apply the same in a recursive way for all given
set of disks.
START
ELSE
END IF
END Procedure
STOP
1. The game consists of three pegs and several disks of different sizes, which can slide onto any peg.
2. The disks are initially placed on one peg in order of decreasing size so that the smallest disk is at the
top and the largest disk is at the bottom.
3. The objective of the game is to move the entire stack to another peg, obeying the following rules:
o Only one disk can be moved at a time.
o Each move consists of taking the upper disk from one of the stacks and placing it on top of
another stack or an empty peg.
o No disk may be placed on top of a smaller disk.
4. The puzzle can be solved in the minimum number of moves, which is (2^n)-1, where n is the number
of disks.
5. These rules apply to any version of the Tower of Hanoi puzzle, regardless of the number of pegs or
disks used.
Constraints
Let's assume the number of disks as N = 2. All three pegs are named A, B, and C. Our task is to shift the two
disks from A to C using B. The stack is represented as follows:
1. Move disk 1 from peg A to peg B. After this move, the arrangement looks like this, which is shown in
the below image.
2. Move disk 2 from peg A to peg C. After this move, the arrangement looks like this, which is shown in
the below image.
3. Move disk 1 from peg B to peg C. After this move, the arrangement looks like this which is shown in
the below image.
4. From the above image, we can conclude that every disk is been moved from A (source peg) to C
(destination peg) and this has been achieved by following the given rules which are mentioned
above.
1. Using Recursion
This approach mainly discusses the recursive approach to be followed for moving the disks from the source
to the destination peg.
Initially consider A as the source peg, B as the auxiliary peg, and C as the destination peg.