The code for the Towers of Hanoi is here, the problem is what will the rest of the recursive calls be?

//provided in the main method that String Src="Src", Aux="Aux", Dst="Dst";

public static void solve (int diskNumber, String Src, String Aux, String Dst) {

		if (diskNumber ==0)
			return;

		else
		{
			solve(diskNumber-1, Src, Dst, Aux);
			System.out.println("Move the disk from "+Src +" to "+Dst);
			solve(diskNumber-1, Aux, Src, Dst);


		}
	}

For example, we solve the 3 disks, we call solve(3, Src, Aux, Dst), the next recursive call will be (2, Src, Dst, Aux). I understand this so far, but what will happen from then on? What are the rest of the recursive calls?
What I don't get is when and how are the println and the "solve(diskNumber-1, Aux, Src, Dst)" executed, why?

I first thought it might be
(3, A, B, C) Src= A; Aux=B; Dst=C;
(2, A, C, B)
(2, B, A, C)
(1, A, C, B)
(1, B, A, C)
N=0, exit

but I don't think it's right.

Thanks!

The call: "solve(diskNumber-1, Aux, Src, Dst)" will execute after the first call has finished: "solve(diskNumber-1, Src, Dst, Aux)" But don't forget that this is also a recursive call. don't expect this execution:

>solve(diskNumber-1, Aux, Src, Dst)
>solve(diskNumber-1, Src, Dst, Aux)

After the first call the same method will be executed, and it will execute:

>solve(diskNumber-2, Aux, Src, Dst)
>solve(diskNumber-2, Src, Dst, Aux)

But the second call again will not execute until the first, which is recursive, so you will get:

>solve(diskNumber-3, Aux, Src, Dst)
>solve(diskNumber-3, Src, Dst, Aux)

When the first finishes then this will be called:
>solve(diskNumber-3, Src, Dst, Aux)

And then the
>solve(diskNumber-2, Src, Dst, Aux)

The call: "solve(diskNumber-1, Aux, Src, Dst)" will execute after the first call has finished: "solve(diskNumber-1, Src, Dst, Aux)" But don't forget that this is also a recursive call. don't expect this execution:

>solve(diskNumber-1, Aux, Src, Dst)
>solve(diskNumber-1, Src, Dst, Aux)

After the first call the same method will be executed, and it will execute:

>solve(diskNumber-2, Aux, Src, Dst)
>solve(diskNumber-2, Src, Dst, Aux)

But the second call again will not execute until the first, which is recursive, so you will get:

>solve(diskNumber-3, Aux, Src, Dst)
>solve(diskNumber-3, Src, Dst, Aux)

When the first finishes then this will be called:
>solve(diskNumber-3, Src, Dst, Aux)

And then the
>solve(diskNumber-2, Src, Dst, Aux)

Thanks, I asked my teacher too, and now I'm still kind of confused. So my teacher came up with this so far
Src= A; Aux=B; Dst=C;
solve(3, A, B, C)
solve(2, A, C, B)
solve (1, A, B, C)
solve (0, A, C, B)
solve (0, B, A, C)
solve (1, C, A, B)
solve (2, B, A, C)
...this goes on, and i don't know the rest

I understand how the numbers are called, but I don't understand how the parameters, A, B, C are placed

Comments
This article has been dead for over six months. Start a new discussion instead.