Anda di halaman 1dari 26

AP Computer Science

Recursion

What is the output?


Public class Test {
public void methodA(){
System.out.println(A);
}
public void methodB(){
methodA();
System.out.println(B);
}
public void methodC() {
methodB();
System.out.println(C);
}
public static void main(String[] args) {
Test t = new Test();
t.methodC();
System.out.println(Main);
}
}

Calling Methods
Can a method call itself?
public void methodB(){
methodB();
System.out.println(B);
}

Recursion
A method that calls itself is called a
recursive method.
A recursive computation solves a problem
by using the solution of the same problem
with simpler values.

Classic Factorial Problem

Evaluate the following expressions:


3!
4!
0!

Classic Factorial Problem

Evaluate the following expressions:


3! = 3 * 2 * 1 = 6
4! = 4 * 3 * 2 * 1 = 24
0! = 1. We define 0! = 1. There is nothing to
understand about this. It is just a definition.
Write code to calculate 4!

Factorial Problem Iterative


Approach

One solution to solve 4!:

int answer = 1;
for(int j=1; j<=4; j++)
{
answer = answer * j;
}
System.out.println(answer);

Factorial Problem A Different


Approach
Consider 6!
Means: 6 * 5 * 4 * 3 * 2 * 1
Rewrite: 6 * (5 * 4 * 3 * 2 * 1). We recognize
that what is in the parentheses is the same a
5!. We could rewrite the problem as
6! = 6 * 5!

Factorial Problem A Different


Approach
In general, we can write:
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
Compute factorial(3);

Computing Factorials
factorial(3) = 3 * factorial(2)
= 3 * (2 * factorial(1))
= 3 * ( 2 * (1 * factorial(0)))
= 3 * ( 2 * ( 1 * 1)))
= 3 * ( 2 * 1)
=3*2
=6

Computing Factorials

Your turn! Trace factorial(4)

Computing Factorials
factorial(4) = 4 * factorial(3)
= 4 * (3 * factorial(2)) )
= 4 * (3 * (2 * factorial(1)))
= 4 * (3 * ( 2 * (1 *
factorial(0))))
= 4 * (3 * ( 2 * ( 1 * 1)))
= 4 * (3 * ( 2 * 1) )
= 4 * (3 * 2 )
=4*6
= 24

Factorial A Recursive
Solution
public static int factorial(int n)
{
if(n==1)
return 1;
else
return n* factorial(n-1);
}
Type in the code and test factorial(4).

Other Recursive Examples


Suppose we want to print a triangle pattern
that looks like this:
[]
[][]
[][][]
[][][][]

We write the following method:


public static void printTriangle(int length)

Recursive Example (cont)


public static void printTriangle(int length)

To print the triangle on the previous slide, we


would call the method printTriangle with the
value 4.
printTriangle(4);

Recursive Example (cont)


We can solve this problem by printing a triangle of
length 3 and then print a line with four [][].
[]
[][]
[][][]
[][][][]
I can solve a triangle of length 3 by printing a
triangle of length 2. We can repeat this process
until we get to a triangle of length 1. This is a
special case.

Recursive Example (cont)


public static void printTriangle(int length)
{
if(length < 1) // if length == 0, print nothing
return;
printTriangle(length 1); //print smaller triangle
for(int i = 0; i < length; i++)
{
System.out.print([]);
}
System.out.println();
}

Requirements for
Recursion
1.

2.

Every recursive call must simplify the task


in some way by calling itself.
There must be a special case to handle the
simplest task. (base case)

The printTriangle method calls itself again


with smaller and smaller side lengths.
Eventually the side length must reach 0 and
the method stops calling itself.

Tracing a Recursive
Method

printTriangle(4) calls printTriangle(3)


PrintTriangle(3) calls printTriangle(2)

printTriangle(2) calls printTriangle(1)

printTriangle(1) calls printTriangle(0)

printTriangle(0), prints nothing returns

printTriangle(1) prints[]

printTriangle(2) prints[][]
printTriangle(3) prints[][][]
printTriangle(4) prints[][][][]

What would happen when we


run the following code?
public class Test {
public void recurse() {
recurse();
}
public static void main(String[] args)
Test t = new Test();
t.recurse();
}
}

What would happen when we


run the following code?
public class Test {
public void recurse(int count) {
System.out.println(count + recurse);
if (count <= 0 ) {
System.out.println(Done);
}
else {
recurse(count 1);
}
}
public static void main(String[] args){
Test t = new Test();
t.recurse(5);
}
}

What would happen when we


run the following code?
public class Test {
public void recurse(int count) {
System.out.println(count + recurse);
if (count < = 0 ) {
System.out.println(Done);
Base Case
}
else {
recurse(count 1);
Recursive Call
}
}
public static void main(String[] args)
Test t = new Test();
t.recurse(5);
}
}

Framework for Recursive


Method
public void recursiveMethod(){
if (base case)
<perform some action>
else
{
<perform some other action>
recursiveMethod( );
}
}

Example of Recursive
Function
f(0) = 3
f(n) = 2f(n-1) + 3 for n >= 1

What is the value of f(3)?

Solution of Recursive
Function
f(0) = 3
f(n) = 2f(n-1) + 3 for n >= 1

What is the value of f(3)?


f(3) = 2f(2) + 3
2(21) + 3 = 45
f(2) = 2f(1) + 3
2(9) + 3 = 21
f(1) = 2f(0) + 3
2(3) + 3 = 9

Practice

Recursion exercise #1

Homework
Barrons Chapter 3 Inheritance &
Polymorphism Question #11-#27

Anda mungkin juga menyukai