Home > Net >  why does the result of this recursive function in java is like that
why does the result of this recursive function in java is like that

Time:05-20

1. static void h2(int n){
2.        if(n<2)
3.            h2(n 1);
4.        System.out.print(n " ");
5.        if(n<2)
6.            h2(n 1);
7. }

the function h2() is a recursive function
if I give n=0 the result of this function is : 2 1 2 0 2 1 2
can someone explain to me how does this function work ?
and what I wanna know also is next ==> for example in line 3 when the function h2(n 1); is called does the code continue to the next line and print the value on n or it goes and start over the function again ?

I added some print so I can see what happen :


1. static void h2(int n){
2.        int i=0;
3.        if(n<2){
4.            System.out.println("i=" (  i) " first n=" n " ");
5.            h2(n 1);
6.        }
7.        System.out.println("i=" (  i) " n=" n);
8.        if(n<2){
9.            System.out.println("\ni=" (  i) " second n=" n " ");
10.            h2(n 1);
11.       }
12. }

and this is the output of this code :

i=1 first n=0 
i=1 first n=1 
i=1 n=2
i=2 n=1

i=3 second n=1 
i=1 n=2
i=2 n=0

i=3 second n=0 
i=1 first n=1 
i=1 n=2
i=2 n=1

i=3 second n=1 
i=1 n=2

CodePudding user response:

Here is a test application to show you the trace of the program's execution (requires Java 11 due to the use of String#repeat(int)):

public class Main {

  private static int depth;

  public static void main(String[] args) {
    foo(0);
  }

  public static void foo(int n) {
    printDebug("Invoked foo("   n   ")");

    if (n < 2) {
      printDebug("Recursively call foo("   n   "   1)");
      depth  ;
      foo(n   1);
      depth--;
      printDebug("Returned from recursive call of foo("   n   "   1)");
    } else {
      printDebug("Skip recursive call");
    }


    // this is the 'System.out.print(n   " "); line'
    printDebug("PRINT VALUE OF n AND A SPACE (n="   n   ")");

    if (n < 2) {
      printDebug("Recursively call foo("   n   "   1)");
      depth  ;
      foo(n   1);
      depth--;
      printDebug("Returned from recursive call of foo("   n    "   1)");
    } else {
      printDebug("Skip recursive call");
    }
  }

  private static void printDebug(String msg) {
    System.out.println("    ".repeat(depth)   msg);
  }
}

The recursive function was renamed to foo and was slightly modified to accommodate debug statements. When you run the above, the output will be:

Invoked foo(0)
Recursively call foo(0   1)
    Invoked foo(1)
    Recursively call foo(1   1)
        Invoked foo(2)
        Skip recursive call
        PRINT VALUE OF n AND A SPACE (n=2)
        Skip recursive call
    Returned from recursive call of foo(1   1)
    PRINT VALUE OF n AND A SPACE (n=1)
    Recursively call foo(1   1)
        Invoked foo(2)
        Skip recursive call
        PRINT VALUE OF n AND A SPACE (n=2)
        Skip recursive call
    Returned from recursive call of foo(1   1)
Returned from recursive call of foo(0   1)
PRINT VALUE OF n AND A SPACE (n=0)
Recursively call foo(0   1)
    Invoked foo(1)
    Recursively call foo(1   1)
        Invoked foo(2)
        Skip recursive call
        PRINT VALUE OF n AND A SPACE (n=2)
        Skip recursive call
    Returned from recursive call of foo(1   1)
    PRINT VALUE OF n AND A SPACE (n=1)
    Recursively call foo(1   1)
        Invoked foo(2)
        Skip recursive call
        PRINT VALUE OF n AND A SPACE (n=2)
        Skip recursive call
    Returned from recursive call of foo(1   1)
Returned from recursive call of foo(0   1)

The indent of each line shows the current depth of the recursive calls (more indent means deeper recursion). If you look at the order of the PRINT VALUE OF n AND A SPACE (n=#) lines, you'll see the output of the original recursive function will be:

2 1 2 0 2 1 2
  • Related