wood burning stoves 2.0*
The moose likes Java in General and the fly likes Help with recursion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Help with recursion" Watch "Help with recursion" New topic
Author

Help with recursion

Steven Alvarez
Ranch Hand

Joined: Nov 01, 2006
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

Joined: Mar 08, 2007
Posts: 20
Steven Alvarez
Ranch Hand

Joined: Nov 01, 2006
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

Joined: Nov 01, 2006
Posts: 66
For instance if I call MTH(5,1,3,2)
Mike Mc Afee
Greenhorn

Joined: Jan 31, 2007
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 ]
 
wood burning stoves
 
subject: Help with recursion
 
Similar Threads
How to read/write directly to/from a class???
Simple logic is kicking my butt.
Recursion
Tag Library for Date submission
output recursive to text file