Recursion
Recursion
Introduction to Recursion
• So far, we have seen methods that call other
functions.
– For example, the main() method calls the square()
function.
main()
square()
• Recursive Method:
– A recursive method is a method that calls itself.
compute()
Why use Recursive Methods?
• In computer science, some problems are more
easily solved by using recursive functions.
• If you go on to take a computer science algorithms
course, you will see lots of examples of this.
• For example:
– Traversing through a directory or file system.
– Traversing through a tree of search results.
• For today, we will focus on the basic structure of
using recursive methods.
World’s Simplest Recursion Program
World’s Simplest Recursion Program
public class Recursion
{
public static void main (String args[])
{
count(0);
This program simply counts from 0-2:
System.out.println();
}
012
public static void count (int index)
{
System.out.print(index);
if (index < 2)
count(index+1); This is where the recursion occurs.
} You can see that the count() function
} calls itself.
Visualizing Recursion
• To understand how recursion works, it helps to
visualize what’s going on.
• To help visualize, we will use a common concept
called the Stack.
• A stack basically operates like a container of trays
in a cafeteria. It has only two operations:
– Push: you can push something onto the stack.
– Pop: you can pop something off the top of the stack.
• Let’s see an example stack in action.
Stacks
The diagram below shows a stack over time.
We perform two pushes and one pop.
8
2 2 2
square()
main() main() main()
Empty Stack Push: main() Push: square() Pop: square() Pop: main()
count(2)
count(1) count(1)
public static int findFactorial (int n) This is an iterative solution to finding a factorial.
{ It’s iterative because we have a simple for loop.
int i, factorial = n;
for (i = n - 1; i >= 1; i--) Note that the for loop goes from n-1 to 1.
factorial = factorial * i;
return factorial;
}
}
Factorials
• Computing factorials are a classic problem for
examining recursion.
• A factorial is defined as follows:
n! = n * (n-1) * (n-2) …. * 1;
• For example:
1! = 1 (Base Case)
2! = 2 * 1 = 2 If you study this table closely, you
3! = 3 * 2 * 1 = 6 will start to see a pattern.
4! = 4 * 3 * 2 * 1 = 24
5! = 5 * 4 * 3 * 2 * 1 = 120
Seeing the Pattern
• Seeing the pattern in the factorial example is
difficult at first.
• But, once you see the pattern, you can apply this
pattern to create a recursive solution to the
problem.
• Divide a problem up into:
– What it can do (usually a base case)
– What it cannot do
• What it cannot do resembles original problem
• The function launches a new copy of itself (recursion step) to
solve what it cannot do.
Factorials
• Computing factorials are a classic problem for
examining recursion.
• A factorial is defined as follows:
n! = n * (n-1) * (n-2) …. * 1; If you study this table closely, you
4! = 4 * 3 * 2 * 1 = 24
For example:
5! = 5 * 4 * 3 * 2 * 1 = 120
5! = 5 * 4!
(which translates to 5! = 5 * 24 = 120)
Recursive Solution
public class FindFactorialRecursive
{
public static void main (String args[])
{
for (int i = 1; i < 10; i++)
System.out.println ( i + "! = " + findFactorial(i));
}
fact(1)
1
fact(2) fact(2) fact(2)
2
fact(3) fact(3) fact(3) fact(3) fact(3)
6
main() main() main() main() main() main()
else return (3 * factorial (2)); else return (2 * factorial (1)); else return (1 * factorial (0));
27
fibonacci(0) = 0
fibonacci(1) = 1
fibonacci(n) = fibonacci(n - 1) + fibonacci( n – 2 )
System.out.println (fibonacciValue);
System.exit (0);