#Can someone tell me how did the tower of hanoi code generate the destination to helper output?

20 messages · Page 1 of 1 (latest)

crude cypress
#
public class practicer2 { //tower of hanoi

    public static void towerOfHanoi(int n, String src, String helper, String dest) {
    
    if (n == 1) {
      System.out.println("Transfer Disk " + n + " from " + src + " to " + dest);
      return;
    }
    towerOfHanoi(n - 1, src, dest, helper);
    System.out.println("Transfer Disk " + n + " from " + src + " to " + dest);
    towerOfHanoi(n - 1, helper, src, dest);
  }

  public static void main(String args[]) {
    int n = 3;
    towerOfHanoi(n, "S", "H", "D");
  }
}
``` I dont understand, when we use n as 3, which part of code prints the D to H output and why
lime tapirBOT
#

This post has been reserved for your question.

Hey @crude cypress! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

crude cypress
#

Hello

crude cypress
#

someone here?

crude cypress
#

eh

visual plaza
#

It's a recursive function, break it down step by step and see exactly what is going on with each call to the function

lime tapirBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.

crude cypress
#

@visual plaza can you explain?

lime tapirBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.

crude cypress
#

@visual plaza Hello?

visual plaza
#

So now walk through the function and see how those parameters interact with the body of the function

#

i.e., it will not trigger the n == 1 and will call the towerOfHanoi(n - 1, src, dest, helper);

crude cypress
#

I m here

lime tapirBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.

buoyant plover
#

@crude cypress recursive just means the method calls itself. Towers of Hanoi is a famous problem for teaching recursion because a big solution can be found by solving two smaller versions of the same problem. At some point the smaller version of the problem is just move one disk. So when you call from main with a big problem, it just call itself on two smaller problems which each do the same and so on. It's critical that at some point, the problem is so small it's just done in one step at which point the call of the call of the call ... returns. Then the whole spiral unwinds.

#

I'm guessing you've been given this as homework to teach you exactly this idea.

lime tapirBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.