1. Recursion - examples - tree recursive process Example: Fibonacci numbers The Fibonacci numbers (-1, 1, 0, 1, 1, 2, 3, 5, 8, 13, 21, ...) are another classic example where recursion is a natural choice, since the definition of Fibonacci numbers includes self-reference, i.e. the n-th Fibonacci number is defined in terms of the (n-1)-st and (n-2)-nd Fibonacci number.
Let F(n) denote the n-th Fibonacci number Base Case: F(1) = -1 # the first two numbers are given F(2) = 1 # (cannot be evaluated by the rule) Recursive Step: F(n) = F(n-1) + F(n-2)
Here is the Java translation. Both versions work the same way (i.e. both generate the same type of recursive process) but one of the versions shows the steps more explicitly.

// computes the n-th fibonacci number

int fib( int n )
{
    if ( n == 1 )
    {
        return -1;
    }
    else if ( n == 2 )
    {
        return 1;          
    }
    else 
    {
        return fib( n - 1 ) + fib( n - 2 );   
    }
}

// computes the n-th fibonacci number

int fib( int n )
{
    if ( n == 1 )
    {
        return -1;
    }
    else if ( n == 2 )
    {
        return 1;          
    }
    else 
    {
        int prevFib1 = fib( n - 1 );
        int prevFib2 = fib( n - 2 );
        int curFib = prevFib1 + prevFib2;

        return curFib;   
    }
}
Here is a trace of this function for n = 4:

                     fib(5)
                  /          \
                 /            \
             fib(4)           fib(3)
           /     \           /    \
          /       \         /      \
      fib(3)     fib(2)   fib(2)   fib(1)
      /   \        ||       ||        ||
     /     \        1        1         0
 fib(2)   fib(1)
   ||       ||
    1        0
We call this process TREE recursive. The shape of the computation looks like a tree growing upside down.
At each step of the computation of a Fibonacci number Java needs to split into two recursive calls, each of which in turn splits into two recursive calls, each of which in turn splits into recursive calls, and so on, until Java gets to one of the easy cases and can start reporting the values back up the recursion tree to the deferred computations. Example: Account Balance
A savings account yields 2% interest compounded annually. Assuming that an account is always opened with an initial deposit of $100 how much money is in the account after n years? Write a recursive method balance(n) that calculates the balance in the account after n years.
This problem has a natural recursive solution that corresponds to the rules of calculating the interest.
To find the amount of money at the end of year N: 1. find the amount of money at the end of year N-1 2. compute the interest on that amount 3. report the sum of 1. and 2. The easy case is when N = 0: after 0 years all we have in the account is the initial deposit.
Here is a more compact way of expressing the rules above:
Let Balance(n) denote the money in the bank after n years. Base Case: Balance(0) = 100 # in the beginning only the initial deposit Recursive Step: Balance(n) = Balance(n-1) + 0.02*Balance(n-1) || || previous year + interest on previous year
Here is the Java translation:

double balance( int n )
{
    if ( n == 0 )
    {
        return 100;
    }
    else
    {
        return balance( n - 1 ) + 0.02*balance( n - 1 );
    }
}
Here is a trace through the execution for year 2. Notice that due to the way we have expressed the computation this function generates a tree-recursive process (in order to compute the return value, Java needs to evaluate two recursive calls even though they will produce the same result.)

                              B(3)
                         /            \    
                      /                   \
               B(2)                         0.02*B(2)
            /        \                       /       \
           /           \                    /         \     
        B(1)         0.02*B(1)          B(1)         0.02*B(1)
      /       \        /    \          /    \         /      \
     /         \      /      \        /      \       /        \
  B(0)   0.02*B(0)  B(0) 0.02*B(0)  B(0) 0.02*B(0)  B(0)   0.02*B(0)
    ||         ||    ||       ||     ||         ||   ||          ||
   100        100   100      100    100        100   100         100 
Here is the code above rewritten in more explicit form. Both versions work the same way (i.e. both generate the same type of recursive process) but one of the versions shows the steps more explicitly.

double balance( int n )
{
    if ( n == 0 )
    {
        return 100;
    }
    else
    {
        double prevBalance = balance( n - 1 );         // 1. still calls itself twice
        double interest = 0.02*balance( n - 1 );       // 2. just as previous version
        double curBalance = prevBalance + interest;    // 3.

        return curBalance;
    }
}
Account Balance: Linear Recursive
If we rewrite the formula slightly we get a linear recursive process: Instead of: Balance(n) = Balance(n-1) + 0.02*Balance(n-1) we can take out a common factor and simplify to: Balance(n) = 1.02*Balance(n-1)
Now the code is:

double balance(int n)
{
    if (n == 0)
    {
        return 100;
    }
    else
    {
        return 1.02*balance(n-1);
    }
}
The code trace for n = 3 shows a linear recursive process:

B(3)
  |
1.02*B(2)
       |
     1.02*B(1)
            |
          1.02*B(0)
                 |
                100
This is an example where a minor change in what we intend to say can have a significant impact on the execution time. The second version will run much faster since it does not need to re-compute values over and over. Account Balance: Linear Recursive Here is another version that is also linear recursive. This version follows verbatim the initial description on how to compute the account balance: To find the amount of money at the end of year N: 1. find the amount of money at the end of year N-1 2. compute the interest on that amount 3. report the sum of 1. and 2.

double balance(int n)
{
    if ( n == 0 )
    {
        return 100;
    }
    else
    {
        double prevBalance = balance( n - 1 );         // 1.
        double interest = 0.02*prevBalance;            // 2.
        double curBalance = prevBalance + interest;    // 3.

        return curBalance;
    }
}