12
Recursion Recursion AP Computer Science A AP Computer Science A Mr. Langner Mr. Langner By: Thomas Robbins By: Thomas Robbins

Recursion AP Computer Science A Mr. Langner By: Thomas Robbins

Embed Size (px)

Citation preview

Page 1: Recursion AP Computer Science A Mr. Langner By: Thomas Robbins

RecursionRecursion

AP Computer Science AAP Computer Science A

Mr. LangnerMr. Langner

By: Thomas RobbinsBy: Thomas Robbins

Page 2: Recursion AP Computer Science A Mr. Langner By: Thomas Robbins

Recursion happens whenever a method has a Recursion happens whenever a method has a statement inside it that causes the method to statement inside it that causes the method to call itself.call itself.

Example:Example:Public int recursion(int a, int b)Public int recursion(int a, int b){{

//other body statements//other body statementsreturn 1 + recursion(int a, int b-1);return 1 + recursion(int a, int b-1);

}}

OverviewOverview

Page 3: Recursion AP Computer Science A Mr. Langner By: Thomas Robbins

Two Important ThingsTwo Important Things

There are two things to know when coding There are two things to know when coding with recursion;with recursion; 1)If the problem is simple and easy, solve it 1)If the problem is simple and easy, solve it

right away.right away. 2)If the problem isn’t so simple and appears 2)If the problem isn’t so simple and appears

very difficult, you need to break it down into very difficult, you need to break it down into smaller parts. smaller parts. Then solve these.Then solve these.

Page 4: Recursion AP Computer Science A Mr. Langner By: Thomas Robbins

ExampleExample

Say you want to divide a piece of wood Say you want to divide a piece of wood into x amount of parts. into x amount of parts.

Page 5: Recursion AP Computer Science A Mr. Langner By: Thomas Robbins

ExampleExample

You will use recursion until all the pieces You will use recursion until all the pieces of wood are small enough.of wood are small enough.

Remember the two important things:Remember the two important things: 1)If the wood is small enough, stop dividing1)If the wood is small enough, stop dividing 2)Else, divide the wood in two pieces and do 2)Else, divide the wood in two pieces and do

the same for the next pieces.the same for the next pieces.

Page 6: Recursion AP Computer Science A Mr. Langner By: Thomas Robbins

ExampleExample

Etc, etc, until the pieces are small enough

Page 7: Recursion AP Computer Science A Mr. Langner By: Thomas Robbins

Coding ExamplesCoding Examples

Now that we understand the principle behind Now that we understand the principle behind recursion, we can take a look at some recursion, we can take a look at some specific examples. specific examples.

Probably the easiest way to describe Probably the easiest way to describe recursion to someone would be to relate recursion to someone would be to relate factorials. factorials.

Ex: 5! = 5 * 4 * 3 * 2 * 1 = 120Ex: 5! = 5 * 4 * 3 * 2 * 1 = 120

Ex: 3! = 3 * 2 * 1 = 6Ex: 3! = 3 * 2 * 1 = 6

Page 8: Recursion AP Computer Science A Mr. Langner By: Thomas Robbins

PsuedocodePsuedocode

1. If the number is equal to one, then the 1. If the number is equal to one, then the factorial equals one.factorial equals one.

2. Else, the factorial of the number equals 2. Else, the factorial of the number equals itself times itself minus one. itself times itself minus one.

factorial(0) = 1 , factorial(1) = 1factorial(0) = 1 , factorial(1) = 1 factorial(N) = N * factorial( N-1 )factorial(N) = N * factorial( N-1 )

Page 9: Recursion AP Computer Science A Mr. Langner By: Thomas Robbins

Simple CodeSimple Code

The Recursion that progresses towards the base case.

Code similar to this is nearly mandatory on all recursive methods. It’s called a “Base case” and prevents the method from entering an infinite loop.

static int fact(int n) {static int fact(int n) {

if (n <= 1) {if (n <= 1) { return 1;return 1;}}else {else { return n * fact(n-1);return n * fact(n-1);

}}

Page 10: Recursion AP Computer Science A Mr. Langner By: Thomas Robbins

Break it down!Break it down!

Terrible pun aside, here’s a good break down of Terrible pun aside, here’s a good break down of the factorial method’s process: the factorial method’s process:

factorial(5) = 5 * factorial(4)factorial(5) = 5 * factorial(4)

= 5 * ( 4 * factorial(3))= 5 * ( 4 * factorial(3))

= 5 * ( 4 * (3 * factorial(2)))= 5 * ( 4 * (3 * factorial(2)))

= 5 * ( 4 * (3 * (2 * factorial(1)))) = 5 * ( 4 * (3 * (2 * factorial(1))))

= 5 * ( 4 * (3 * (2 * (1 * factorial(0))))) = 5 * ( 4 * (3 * (2 * (1 * factorial(0)))))

= 5 * ( 4 * (3 * (2 * (1 * 1)))) = 5 * ( 4 * (3 * (2 * (1 * 1))))

= 5 * 4 * 3 * 2 * 1 * 1 = 5 * 4 * 3 * 2 * 1 * 1 = 120= 120

Page 11: Recursion AP Computer Science A Mr. Langner By: Thomas Robbins

Another example of Recursion!Another example of Recursion!

Counting the number of bowling pins in a Counting the number of bowling pins in a triangle: triangle:

trianglePins(int N) //N is the number of rowstrianglePins(int N) //N is the number of rows

{{

if ( N == 1 )if ( N == 1 )

return 1;return 1;

elseelse

return N + Triangle( N-1 );return N + Triangle( N-1 );

}}

Page 12: Recursion AP Computer Science A Mr. Langner By: Thomas Robbins

The breakdownThe breakdown

trianglePins(5 ) = 5 + trianglePins (4)trianglePins(5 ) = 5 + trianglePins (4)

= 5 + (4 + trianglePins(3))= 5 + (4 + trianglePins(3))

= 5 + (4 + (3 + trianglePins(2)))= 5 + (4 + (3 + trianglePins(2)))

= 5 + (4 + (3 + (2 + trianglePins(1))))= 5 + (4 + (3 + (2 + trianglePins(1))))

= 5 + (4 + (3 + (2 + 1)))= 5 + (4 + (3 + (2 + 1)))

= 5 + (4 + (3 + 3))= 5 + (4 + (3 + 3))

= 5 + (4 + 6)= 5 + (4 + 6)

= 5 + 10= 5 + 10

= 15= 15