

a disc can only be moved if it is the uppermost disc on a stack.Ī larger disc may not be stacked on top of a smaller disc. To complete the puzzle, put the discs in the same order in the final rod via the middle rod.Īt any given time, only one disc can be transferred.Įach move requires taking the uppermost disc from one of the stacks and placing it on top of another, i.e. In the first rod, the discs are positioned so that the largest disc is at the bottom and the smallest is at the top.

It is a mathematical puzzle game in which three identical rods and n discs of varying sizes are used. The answer to the primary problem is represented in terms of lesser problems until the smallest problem meets the base case in a recursive function. Recursive Case: In the recursive case, the function calls itself till it reaches the base case.

Recursion is most useful for activities that can be broken down into smaller subtasks.Ī recursive function is made up of two major components:īase Case: The base case is where all further calls to the same function end, implying that no additional recursive calls are made. When loops are built or interpreted, they are generally transformed into recursive functions.
#Hanoi towers recursive function code
In general, recursive code is shorter and easier to write than iterative code. The technique of explaining an action in terms of itself is known as recursion. This type of function is known as a recursive function. When a function calls itself repeatedly until it meets a stated stopping condition, this is referred to as recursion. The Oxford English Dictionary defines recursion as the repeated use of a recursive technique or term. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well. Public void solve(int n, String srcTower, String intermediateTower, String destTower) * Recursion to solve Towers of Hanoi problem. Please checkout code snippet and algorithm visualization section for more details of the algorithm. It consists of three rods and a number of disks of different sizes which can slide onto any rod. Problem Solution The tower of hanoi is a mathematical puzzle. Problem Description This C Program uses recursive function & solves the tower of hanoi. As you should be able to verify, to move 'n' disks, minimum number of moves needed are (2^n - 1). This is a C Program to demonstrate tower of hanoi. Time taken by this algorithm is exponential in nature. To do this, we again make a recursive call - solve(n = n-1, srcTower = intermedTower, intermedTower = srcTower, destTower = destTower).Here again, roles of intermedTower and srcTower are exchanged. Lastly, we want to move 'n-1' disks from intermedTower to destTower using srcTower. At this step, we are basically hitting the base case for recursion.ĥ. After completion of step #3, we make a recursive call solve(n = 1, srcTower = srcTower, intermedTower = intermedTower, destTower = destTower) to move the last disk sitting at srcTower to destTower using intermedTower though intermedTower is not needed in this case. Note how the roles of intermedTower and destTower have changed for solving this puzzle for 'n-1' disks.Ĥ. To do this, we make a recursive call - solve(n = n-1, srcTower = srcTower, intermedTower = destTower, destTower = intermedTower). We want to first move top 'n-1' disks from srcTower to intermedTower using destTower. This is the base case for this recursion.ģ. We move that disk to destTower and return to the calling function. if (n = 1) then the only disk sitting at srcTower can be moved to destTower. Initial call is made to function solve(n = n, srcTower = A, intermedTower = B, destTower = C)Ģ. If function 'solve' takes arguments as number of disks to be moved, source tower(A), intermediate tower(B) and destination tower(C) thenġ. Once we do that following state is reached and we are done.īy careful observation of above steps we can formulate the recursive algorithm to solve this problem. Now from this state, we need to move the two disks sitting at the tower 'B' to tower 'C' using tower 'A'.
#Hanoi towers recursive function free
Once we get to this state, the last disk in tower A is free to move to tower 'C' and once we move that disk to tower 'C' we reach the following state. Where top two disks are moved to tower 'B' by making use of tower 'C'(if required). Without caring too much about the details, from the above given state we first want to reach to the following shown state. Let's first try to understand the abstract procedure that would help to solve this problem. This is a classic example to understand the intuition of recursion.
