- By
**lotus** - Follow User

- 213 Views
- Uploaded on

Download Presentation
## Recursion

**An Image/Link below is provided (as is) to download presentation**

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript

Recursion

- similar to iteration in that you repeatedly do a little bit of the task and then “loop” again and work on a smaller piece - eventually reaching your goal
- accomplished by a method invoking itself!
- so, if it invokes itself - then let’s think about that - how do you think it’s going to stop???

Ch. 7

Recursion

can you write this another way?

Iteration

public void pickAll() {

while (nextToABeeper())

pickBeeper();

}

Recursion

public void pickAll() {

if (! nextToABeeper()

return;

pickBeeper();

pickAll();

}

This recursive definition goes like this: Are there any beepers left on this corner? No - then we’re done. Yes - then pick one beeper and then pickAll.

Ch. 7

Recursion

- The key to recursion
- we don’t define a problem in terms of exactly itself - we define the problem in terms of a smaller/simpler (yet very closely related) problem - also, we define the simplest/smallest situation separately.
- in the previous problem we define the simplest situation to be: there are no beepers on the corner - this becomes our base case
- otherwise, we have more work to do - so we solve a tiny part of the problem (pickBeeper) and then invoke ourself to finish the smaller (yet related) problem - this becomes our general/recursive case.

Ch. 7

You Try!

- write a recursive method called goToSouthWall
- precondition: bot is already facing south - there are no walls in the way
- postcondition: bot is at the south wall still facing south - it is still on the same avenue
- What are preconditions & postconditions?

the things that need to be true in order for the method to perform its task - the contract between the client and the method

what will be true after the method has finished executing - only guaranteed if the preconditions are met

Ch. 7

Writing Recursive Methods - the Process

- The process for writing recursive robot instructions is very similar to that for writing loops:
- Step 1: Consider the stopping condition (also called the base case)--what is the simplest case of the problem that can be solved? In the pickAll problem, the simplest, or base, case is when the robot is already on an empty corner.

Ch. 7

the Process - cont’d

- Step 2: What does the robot have to do in the base case? In this example there's nothing to do.
- Step 3: Find a way to solve a small piece of the larger problem if not in the base case. This is called "reducing the problem in the general case." In the pickAll problem, the general case is when the robot is on a corner with one or more beepers and the reduction is to pick up a beeper.

Ch. 7

the Process - cont’d

- Step 4: Make sure the reduction leads to the base case. Again, in the above example of pickAll, by picking up one beeper at a time, the robot must eventually clear the corner of beepers, regardless of the original number present.

Ch. 7

Iteration vs. Recursion

- An iterative loop must complete each iteration before beginning the next one.
- A recursive method typically begins a new instance before completing the current one. When that happens, the current instance is temporarily suspended, pending the completion of the new instance. Of course, this new instance might not complete before generating another one. Each successive instance must be completed in turn, last to first.

Ch. 7

Iteration vs. Recursion

- Since EACH recursive instance is supposed to make some (often minimal) progress toward the base case, we should not use loops to control recursive calls. Thus, we will usually see an IF or an IF/ELSE in the body of a recursive method, but not a WHILE.

Ch. 7

Follow the steps and write the method

- Use recursion to move a robot named Karel to a beeper. Follow the steps presented earlier:
- Step 1: What is the base case?
- Karel is on the beeper.
- Step 2: What does the robot do in the base case?
- Nothing.
- Step 3: What is the general case?
- The robot is not on the beeper.

Ch. 7

writing the recursive method - cont’d

- Step 4: What is the reduction (what do we do in the general case)?
- Move toward the beeper and make the recursive call.
- Does the reduction lead to termination? Yes, assuming the beeper is directly in front of the robot, the distance will get shorter by one block for each recursive call. By the way, this “assumption” becomes the precondition(i.e., there is a beeper somewhere in front of the bot).

Ch. 7

Why Recursion?

- there are problems which are much easier to solve using recursion
- Problem: the “Lost Beeper Mine”
- Karel is facing East and there is a beeper somewhere in front of her. Directly North of that beeper there is a “Beeper Mine” (a corner with a large number of beepers on it). The Beeper Mine is exactly the same number of corners North of the single beeper as Karel is away from the single beeper at the beginning. Get Karel to the Beeper Mine! Try this without recursion.

Ch. 7

let’s write it

- Step 1: What is the base case?
- Karel is on the beeper
- Step 2: What does Karel have to do in the base case?
- turnLeft (this will face Karel north)
- Step 3: What is the general case?
- Karel is not on the beeper

Ch. 7

let’s write it

- Step 4: What is the reduction?
- Move one block forward, make the recursive call and have Karel execute a second move after the recursive call. This second move will be executed in all instances but the base case, causing Karel to make as many moves north after the base case as it did in getting to the base case.
- Now, code it before we go on…

Ch. 7

Download Presentation

Connecting to Server..