
.jpg)
This moves the disks to the middle tower (#1) using the other tower (#2) as intermediate. The recursive algorithm for the tower of Hanoi is based on observing that the top n-1 disks at the "from" tower (together with the other two towers) represent a smaller-size instance of the original problem and, thus, can be solved by the call Hanoi(n-1, 0,1,2).

Then express the solution for the original problem in terms of the solutions to the subproblems. Normally (if we denote the towers with 0,1,2), the initial call for n disks will be Hanoi(n,0,2,1).Ī recursive algorithm normally tries to find subproblems (instances of the original problem but with smaller problem size).

via: the "via" tower is that used as an intermediate location as disks are moved between the towers from and to.to: the "to" tower is where the disks must be finally placed.from: the "from" tower is where the disks are placed.n: number of disks serves as the problem size for recursion.The input to the algorithm are 4 integer parameters, as follows: callStack.push() // save parameters to callStack array Copy Code function Hanoi(n, from, to, via)
