What happens when recursion is called twice in a method?

梦想的初衷 提交于 2021-01-29 03:07:33

问题


I'm pretty new to programming and I have now come to the concept of recursion. I have solved a few basic assignments but when I come to multible recursion I get terribly lost. I have tried to solve the following recursion several times but just cant get it right.

A recursive method is called with the arguments "ABCD" and 2, i.e. recMethod("ABCD", 2);.

public void recMethod( String str, int n ) {
  if( n >= 0 ) {
     recMethod(str, n – 1);
     System.out.print(str.charAt(n));
     recMethod(str, n – 1);
  }
}

Is there somebody out there who can explain what is going on? The first recursive call really confuses me.


回答1:


The best way to understand recursion as for me is to write it on the paper line by line. What I did for your case now.

Please try to do the same, and don't hesitate to ask more question, I hope it helps!




回答2:


The most important thing to know about recursion is that it's nothing special. As with everything in a method it needs to finish a statement before the next statement can be executed:

public void someMethod() {
  someOtherMethod();
  someLastMethod();
}

Looking at my example it's obvious that someLastMethod will be called after someOtherMethod has finished. It really doesn't matter if you replace someOtherMethod with something recursive. It needs to finish before someLastMethod can be called. When looking at your recursive method again:

public void recMethod( String str, int n ) {
    if( n >= 0 ) {
        recMethod( str, n – 1 );
        System.out.print( str.charAt( n ) );
        recMethod( str, n – 1 );
    } else { // base case added for clarity
        return;
    }
}

For each call where n >= 0, before the System.out.print method is called the call to recMethod has to be called. Every call to recMethod has it's own n and str so they can be considered different methods entirely except their code is 'very similar'.

Every call will either match base case or need the result of the same method with n decremented so I like to start from the base case and work myself backwards, which is when n is -1. Imagine you call recMethod("ABCD",-1) what would happen? Well it prints nothing or "".

I then look at recMethod("ABCD",0) and it calls the base case, which we know does nothing, then prints "A" and then it calls the same as the first statement which again does nothing. Thus it prints "A"

If we look at recMethod("ABCD",1). We know it calls recMethod("ABCD",0) which prints "A", then it prints "B", then call recMethod("ABCD",0) which prints "A". Thus it prints "ABA"

If we look at recMethod("ABCD",2). We know it calls recMethod("ABCD",1) which prints "ABA", then it prints "C", then call recMethod("ABCD",1) which prints "ABA". Thus it prints "ABACABA"

If we look at recMethod("ABCD",3). We know it calls recMethod("ABCD",2) which prints "ABACABA", then it prints "D", then call recMethod("ABCD",2) which prints "ABACABA". Thus it prints "ABACABADABACABA"

Since "abcd".charAt(4) won't work it doesn't make sense to go on. Perhaps the code should have a test for this or perhaps it should be private and have a public one without n that guarantees n never goes beyond str bounds?

If you were to make a method that works recursively you do the same.

  1. What should happen for the base case. (How should it stop)

  2. What should happen if it's not the base case expressed as if the method works as intended. The catch here is that you need to make sure that each call to the same method here is a slightly simpler problem which the recursion is bound to hit a base case or else you get infinite recursion!

Thats it! It will work.



来源:https://stackoverflow.com/questions/37067354/what-happens-when-recursion-is-called-twice-in-a-method

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!