This week's book giveaways are in the Refactoring and Agile forums.We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

# Help with recursion

Steven Alvarez
Ranch Hand
Posts: 66
here is the function:

public static void MTH(int d, int from, int to, int extra)

{

if(d!=0);
else
{
MTH(d-1, from, extra, to);
System.out.println("Move disk # " + d + "From" + from + "To" + to);
MTH(d-1,extra,to,from);
}
}

I need help tracing it. You know the variables and steps and things of that nature. Just to let you know, MTH stands for Move Tower of Hinoi. It a game. So I just need help understanding and tracing this function. Thanks.

Clark Johnson
Greenhorn
Posts: 20

Steven Alvarez
Ranch Hand
Posts: 66
Thanks for fixing the problem, but thats what I needed. I need some help understanding recursion. I was hoping someone can trace this function for me. Like keep track of the variables, what step are we at, etc. Any help would be appriciated. Thanks.

Steven Alvarez
Ranch Hand
Posts: 66
For instance if I call MTH(5,1,3,2)

Mike Mc Afee
Greenhorn
Posts: 19
Originally posted by Steven Alvarez:
For instance if I call MTH(5,1,3,2)

I'm not sure what you're asking, but to understand what's going on, a paper and pencil will help. You need to keep track of the stack, as the calls to MTH are made.

i.e.

Initial call:
D=5, from=1 , to=3 , extra=2

Second call:
D= 4, from= 1, to=2 , extra=3

etc.

Do this until your conditional is false, then the stack will pop and the execution of MTH will continue.
[ March 15, 2007: Message edited by: Mike Mc Afee ]