recursion unit 15. recursion: recursion is defined as the process of a subprogram calling itself as...

22
Recursion Unit 15

Upload: byron-donald-booker

Post on 17-Jan-2016

246 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Recursion Unit 15. Recursion: Recursion is defined as the process of a subprogram calling itself as part of the solution to a problem. It is a problem

RecursionUnit 15

Page 2: Recursion Unit 15. Recursion: Recursion is defined as the process of a subprogram calling itself as part of the solution to a problem. It is a problem

Recursion:Recursion is defined as the process of

a subprogram calling itself as part of the solution to a problem. It is a problem solving technique that can turn a long and difficult solution into a compact and elegant answer. It can also solve problems that are very difficult to solve with a straightforward iterative solution.

Page 3: Recursion Unit 15. Recursion: Recursion is defined as the process of a subprogram calling itself as part of the solution to a problem. It is a problem

Students will be able to:

solve programming problems using recursion.

trace a recursion function to find errors and/or output.

Page 4: Recursion Unit 15. Recursion: Recursion is defined as the process of a subprogram calling itself as part of the solution to a problem. It is a problem

Vocabulary

Recursion Base Case

Page 5: Recursion Unit 15. Recursion: Recursion is defined as the process of a subprogram calling itself as part of the solution to a problem. It is a problem

Recursion

Recursion occurs when a method calls itself to solve a simpler version of the problem. With each recursive call, the problem is different from, and simpler than, the original problem.

Page 6: Recursion Unit 15. Recursion: Recursion is defined as the process of a subprogram calling itself as part of the solution to a problem. It is a problem

Recursion & Factorials

Remember factorials? Solve 6! 6! = 6*5*4*3*2*1 = 720

Remember This !?!

Page 7: Recursion Unit 15. Recursion: Recursion is defined as the process of a subprogram calling itself as part of the solution to a problem. It is a problem

Recursion

Recursion involves the internal use of a stack. A stack is a data abstraction that works like this: New data is "pushed," or added to the top of the stack.

Page 8: Recursion Unit 15. Recursion: Recursion is defined as the process of a subprogram calling itself as part of the solution to a problem. It is a problem

Recursion

When information is removed from the stack it is "popped," or removed from the top of the stack. The recursive calls of a method will be stored on a stack, and manipulated in a similar manner.

Page 9: Recursion Unit 15. Recursion: Recursion is defined as the process of a subprogram calling itself as part of the solution to a problem. It is a problem

Factorials

1! = 12! = 2 * 1 or 2 * 1!3! = 3 * 2 * 1 or 3 * 2!

4! = 4 * 3 * 2 * 1 or 4 * 3!

Page 10: Recursion Unit 15. Recursion: Recursion is defined as the process of a subprogram calling itself as part of the solution to a problem. It is a problem

Factorials

1! = 12! = 2 * 1 or 2 * 1!3! = 3 * 2 * 1 or 3 * 2!

4! = 4 * 3 * 2 * 1 or 4 * 3!

Notice e

ach fa

ctoria

l can

be writ

ten

in term

s of a

previous l

ine!

Page 11: Recursion Unit 15. Recursion: Recursion is defined as the process of a subprogram calling itself as part of the solution to a problem. It is a problem

The code:int fact(int n)

// returns the value of n!// precondition: n >= 1 { if (n == 1) return 1; else return n * fact(n - 1); }

Page 12: Recursion Unit 15. Recursion: Recursion is defined as the process of a subprogram calling itself as part of the solution to a problem. It is a problem

The code:int fact(int n)

// returns the value of n!// precondition: n >= 1 { if (n == 1) return 1; else return n * fact(n - 1); }

Base Case

Page 13: Recursion Unit 15. Recursion: Recursion is defined as the process of a subprogram calling itself as part of the solution to a problem. It is a problem

The code:int fact(int n)

// returns the value of n!// precondition: n >= 1 { if (n == 1) return 1; else return n * fact(n - 1); }

Base Case

Recursive Call

Page 14: Recursion Unit 15. Recursion: Recursion is defined as the process of a subprogram calling itself as part of the solution to a problem. It is a problem

How does it work?

Let’s try it!

Page 15: Recursion Unit 15. Recursion: Recursion is defined as the process of a subprogram calling itself as part of the solution to a problem. It is a problem

Pitfalls of Recursion

If the recursion never reaches the base case, the recursive calls will continue until the computer runs out of memory and the program crashes. Experienced programmers try to examine the remains of a crash. The message “stack overflow error” or “heap storage exhaustion” indicates a possible runaway recursion.

Page 16: Recursion Unit 15. Recursion: Recursion is defined as the process of a subprogram calling itself as part of the solution to a problem. It is a problem

Pitfalls of Recursion

When programming recursively, you need to make sure that the algorithm is moving toward the base case. Each successive call of the algorithm must be solving a simpler version of the problem.

Page 17: Recursion Unit 15. Recursion: Recursion is defined as the process of a subprogram calling itself as part of the solution to a problem. It is a problem

Pitfalls of Recursion

Any recursive algorithm can be implemented iteratively, but sometimes only with great difficulty. However, a recursive solution will always run more slowly than an iterative one because of the overhead of opening and closing the recursive calls.

Page 18: Recursion Unit 15. Recursion: Recursion is defined as the process of a subprogram calling itself as part of the solution to a problem. It is a problem

4U2DO

Write a recursive power method that raises a base to some exponent, n. We will use integers to keep things simple.

 double power(int base, int n)/* Recursively determines base raised to the nth power. Assumes 0 <= n <= 10. */

Page 19: Recursion Unit 15. Recursion: Recursion is defined as the process of a subprogram calling itself as part of the solution to a problem. It is a problem

ConclusionRecursion takes some time and practice to get used to. Eventually you want to be able to think recursively without the aid of props and handouts.

Page 20: Recursion Unit 15. Recursion: Recursion is defined as the process of a subprogram calling itself as part of the solution to a problem. It is a problem

ConclusionRecursion is a very powerful programming tool for solving difficult problems.

Study and practice the examples provided by yourself for understanding.

Page 21: Recursion Unit 15. Recursion: Recursion is defined as the process of a subprogram calling itself as part of the solution to a problem. It is a problem

4U2DoDue by the next class:

Piglatinator.java

BackToSchool.java

Page 22: Recursion Unit 15. Recursion: Recursion is defined as the process of a subprogram calling itself as part of the solution to a problem. It is a problem

4U2Do – Extra CreditA triangular number is figurate number obtained by adding all positive integers less than or equal to a given positive integer n.

T4 gives the number and arrangement of bowling pins, while T5 gives the number and arrangement of balls in billiards.

Write a recursive triangular number method that determines the nth triangular number.  

long triNum(int n){/* Recursively determines the nth triangular

number. Assumes n <= 1. */}