57
Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem within the problem itself. This presentation gives an additional example, which is not in the book. Recursive Thinking Data Structures Data Structures and Other Objects and Other Objects Using Java Using Java

Recursive Thinking

Embed Size (px)

DESCRIPTION

Recursive Thinking. Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem within the problem itself. This presentation gives an additional example, which is not in the book. Data Structures - PowerPoint PPT Presentation

Citation preview

Page 1: Recursive Thinking

Chapter 8 introduces the technique of recursive programming.

As you have seen, recursive programming involves spotting smaller occurrences of a problem within the problem itself.

This presentation gives an additional example, which is not in the book.

Recursive ThinkingRecursive Thinking

Data StructuresData Structuresand Other Objectsand Other ObjectsUsing JavaUsing Java

Page 2: Recursive Thinking

First ExampleFirst Example

Task to write a non-negative integer to Task to write a non-negative integer to screen with decimal digits stackedscreen with decimal digits stacked

Number 7895Number 7895 OutputOutput

77

88

99

55

Page 3: Recursive Thinking

One digit solutionOne digit solution

Print that digit!Print that digit! Number 7Number 7 Output Output

77

Page 4: Recursive Thinking

Many digit solutionMany digit solution

Print all digits except the last, stacked Print all digits except the last, stacked verticallyvertically

Then print the last digitThen print the last digit

77

88

99 Now print the 5 which is 7895%10Now print the 5 which is 7895%10

Page 5: Recursive Thinking

PseudocodePseudocode

//printing the digits of a non-negative number stacked vertically

if (the number has only one digit)

write that digit.

else

write the digits of the number/10 stacked vertically

Write the single digit of number %10.

Page 6: Recursive Thinking

Recursive solutionRecursive solution

Always has a stopping case (base case)Always has a stopping case (base case)if (number < 10)if (number < 10)

System.out.println(number);System.out.println(number); Always has a recursive callAlways has a recursive callelseelse

{{

writeVertical(number/10);writeVertical(number/10);

system.out.println(number%10);system.out.println(number%10);

}}

Page 7: Recursive Thinking

Recursively callRecursively call

Number is 7895

Call 17895

Call 17895

Call 2789

Call 17895

Call 2789

Call 17895

Call 2789

Call 378

Call 17895

Call 2789

Call 378

Call47

Page 8: Recursive Thinking

Now come backNow come back

Call 17895

Call 2789

Call 378

Call47

Call 4 with a 7 is a one digit numberTherefore, print out that single digit and that call is complete

7Call 17895

Call 2789

Call 378

Page 9: Recursive Thinking

Now return from 4th callNow return from 4th call

Call 17895

Call 2789

Call 378

Returning from that 4th call the next statement if System.out.println(number%10)

Since that number is 78 : print out 78%10 or 8

78

Call 17895

Call 2789

Page 10: Recursive Thinking

Now return from third callNow return from third call

Call 17895

Call 2789

Returning from that 3rd call the next statement if System.out.println(number%10)

Since that number is 789 : print out 789%10 or 9

789

Call 17895

Page 11: Recursive Thinking

Returning from 2nd callReturning from 2nd call

Returning from that 2nd call the next statement if System.out.println(number%10)

Since that number is 7895 : print out 789%10 or 5

7895

Call 17895

Page 12: Recursive Thinking

Return from first callReturn from first call

We’re Done!!!!

Page 13: Recursive Thinking

A Car ObjectA Car Object

To start the example, think about your favorite family car

Page 14: Recursive Thinking

A Car ObjectA Car Object

To start the example, think about your favorite family car

Page 15: Recursive Thinking

A Car ObjectA Car Object

To start the example, think about your favorite family car

Page 16: Recursive Thinking

A Car ObjectA Car Object

To start the example, think about your favorite family car

Page 17: Recursive Thinking

A Car ObjectA Car Object

To start the example, think about your favorite family car

Imagine that the car is controlled by a radio signal from a computer

Page 18: Recursive Thinking

A Car ClassA Car Class

public class Car { . . .}

To start the example, think about your favorite family car

Imagine that the car is controlled by a radio signal from a computer

The radio signals are generated by activating methods of a Car object

Page 19: Recursive Thinking

public class Car{ public Car(int carNumber); public public void move( ); public void turnAround( ); public boolean isBlocked( );

... We don't need to know the private fields! ...}

methods for the Car Classmethods for the Car Class

Page 20: Recursive Thinking

public static void main(...){ Car racer = new Car(7);

. . .

The ConstructorThe Constructor

When we declare a Car

and activate the constructor, the computer makes a radio link with a car that has a particular number.

Page 21: Recursive Thinking

public static void main(...){ Car racer = new Car(7);

racer.turnAround( ); . . .

The turnAround methodThe turnAround method

When we activate turnAround, the computer signals the car to turn 180 degrees.

Page 22: Recursive Thinking

public static void main(...){ Car racer = new Car(7);

racer.turnAround( ); racer.move( ); . . .

The move methodThe move method

When we activate move, the computer signals the car to move forward one foot.

Page 23: Recursive Thinking

public static void main(...){ Car racer = new Car(7);

racer.turnAround( ); racer.move( ); . . .

The move methodThe move method

When we activate move, the computer signals the car to move forward one foot.

Page 24: Recursive Thinking

public static void main(...){ Car racer = new Car(7);

racer.turnAround( ); racer.move( ); if (racer.isBlocked( ) ) System.out.println (“Cannot move!”); . . .

The isBlocked( ) methodThe isBlocked( ) method

The isBlocked method detects barriers.

Page 25: Recursive Thinking

Your MissionYour Mission

Write a method which will move a Car forward until it reaches a barrier...

Page 26: Recursive Thinking

Your MissionYour Mission

Write a method which will move a Car forward until it reaches a barrier...

Page 27: Recursive Thinking

Your MissionYour Mission

Write a method which will move a Car forward until it reaches a barrier...

Page 28: Recursive Thinking

Your MissionYour Mission

Write a method which will move a Car forward until it reaches a barrier...

...then the car is turned around...

Page 29: Recursive Thinking

Your MissionYour Mission

Write a method which will move a Car forward until it reaches a barrier...

...then the car is turned around... ...and returned to its original location, facing the

opposite way.

Page 30: Recursive Thinking

Your MissionYour Mission

Write a method which will move a Car forward until it reaches a barrier...

...then the car is turned around... ...and returned to its original location, facing the

opposite way.

Page 31: Recursive Thinking

Your MissionYour Mission

Write a method which will move a Car forward until it reaches a barrier...

...then the car is turned around... ...and returned to its original location, facing the

opposite way.

public void ricochet(Car movingCar)

Page 32: Recursive Thinking

Pseudocode for ricochetPseudocode for ricochet

if movingCar.isBlocked( ), then the car is already at the barrier. In this case, just turn the car around.

public void ricochet(Car movingCar)

Page 33: Recursive Thinking

Pseudocode for ricochetPseudocode for ricochet

if movingCar.isBlocked( ), then the car is already at the barrier. In this case, just turn the car around.

Otherwise, the car has not yet reached the barrier, so start with:

public void ricochet(Car movingCar)

movingCar.move( );. . .

Page 34: Recursive Thinking

movingCar.move( );. . .

Pseudocode for ricochetPseudocode for ricochet

if movingCar.isBlocked( ), then the car is already at the barrier. In this case, just turn the car around.

Otherwise, the car has not yet reached the barrier, so start with:

public void ricochet(Car movingCar);This makes the problem a bit smaller. For example, if the car started 100 feet from the barrier...

100 ft.

Page 35: Recursive Thinking

movingCar.move( );. . .

Pseudocode for ricochetPseudocode for ricochet

if movingCar.isBlocked( ), then the car is already at the barrier. In this case, just turn the car around.

Otherwise, the car has not yet reached the barrier, so start with:

public void ricochet(Car movingCar);This makes the problem a bit smaller. For example, if the car started 100 feet from the barrier... then after activating move once, the distance is only 99 feet.

99 ft.

Page 36: Recursive Thinking

movingCar.move( );. . .

Pseudocode for ricochetPseudocode for ricochet

if movingCar.isBlocked( ), then the car is already at the barrier. In this case, just turn the car around.

Otherwise, the car has not yet reached the barrier, so start with:

public void ricochet(Car movingCar);

We now have a smaller version of the same problem that we started with.

99 ft.

Page 37: Recursive Thinking

movingCar.move( );ricochet(movingCar);. . .

Pseudocode for ricochetPseudocode for ricochet

if movingCar.isBlocked( ), then the car is already at the barrier. In this case, just turn the car around.

Otherwise, the car has not yet reached the barrier, so start with:

public void ricochet(Car movingCar);

Make a recursive call to solve the smaller problem.

99 ft.

Page 38: Recursive Thinking

movingCar.move( );ricochet(movingCar);. . .

Pseudocode for ricochetPseudocode for ricochet

if movingCar.isBlocked( ), then the car is already at the barrier. In this case, just turn the car around.

Otherwise, the car has not yet reached the barrier, so start with:

public void ricochet(Car movingCar);

The recursive call will solve the smaller problem.

99 ft.

Page 39: Recursive Thinking

movingCar.move( );ricochet(movingCar);. . .

Pseudocode for ricochetPseudocode for ricochet

if movingCar.isBlocked( ), then the car is already at the barrier. In this case, just turn the car around.

Otherwise, the car has not yet reached the barrier, so start with:

public void ricochet(Car movingCar);

The recursive call will solve the smaller problem.

Page 40: Recursive Thinking

movingCar.move( );ricochet(movingCar);. . .

Pseudocode for ricochetPseudocode for ricochet

if movingCar.isBlocked( ), then the car is already at the barrier. In this case, just turn the car around.

Otherwise, the car has not yet reached the barrier, so start with:

public void ricochet(Car movingCar);

The recursive call will solve the smaller problem.

Page 41: Recursive Thinking

movingCar.move( );ricochet(movingCar);. . .

Pseudocode for ricochetPseudocode for ricochet

if movingCar.isBlocked( ), then the car is already at the barrier. In this case, just turn the car around.

Otherwise, the car has not yet reached the barrier, so start with:

public void ricochet(Car movingCar);

The recursive call will solve the smaller problem.

Page 42: Recursive Thinking

movingCar.move( );ricochet(movingCar);. . .

Pseudocode for ricochetPseudocode for ricochet

if movingCar.isBlocked( ), then the car is already at the barrier. In this case, just turn the car around.

Otherwise, the car has not yet reached the barrier, so start with:

public void ricochet(Car movingCar);

The recursive call will solve the smaller problem.

Page 43: Recursive Thinking

movingCar.move( );ricochet(movingCar);. . .

Pseudocode for ricochetPseudocode for ricochet

if movingCar.isBlocked( ), then the car is already at the barrier. In this case, just turn the car around.

Otherwise, the car has not yet reached the barrier, so start with:

public void ricochet(Car movingCar);

The recursive call will solve the smaller problem.

Page 44: Recursive Thinking

movingCar.move( );ricochet(movingCar);. . .

Pseudocode for ricochetPseudocode for ricochet

if movingCar.isBlocked( ), then the car is already at the barrier. In this case, just turn the car around.

Otherwise, the car has not yet reached the barrier, so start with:

public void ricochet(Car movingCar);

The recursive call will solve the smaller problem.

Page 45: Recursive Thinking

movingCar.move( );ricochet(movingCar);. . .

Pseudocode for ricochetPseudocode for ricochet

if movingCar.isBlocked( ), then the car is already at the barrier. In this case, just turn the car around.

Otherwise, the car has not yet reached the barrier, so start with:

public void ricochet(Car movingCar);

The recursive call will solve the smaller problem.

Page 46: Recursive Thinking

movingCar.move( );ricochet(movingCar);. . .

Pseudocode for ricochetPseudocode for ricochet

if movingCar.isBlocked( ), then the car is already at the barrier. In this case, just turn the car around.

Otherwise, the car has not yet reached the barrier, so start with:

public void ricochet(Car movingCar);

The recursive call will solve the smaller problem.

Page 47: Recursive Thinking

movingCar.move( );ricochet(movingCar);. . .

Pseudocode for ricochetPseudocode for ricochet

if movingCar.isBlocked( ), then the car is already at the barrier. In this case, just turn the car around.

Otherwise, the car has not yet reached the barrier, so start with:

public void ricochet(Car movingCar);

The recursive call will solve the smaller problem.

99 ft.

Page 48: Recursive Thinking

movingCar.move( );ricochet(movingCar);. . .

Pseudocode for ricochetPseudocode for ricochet

if movingCar.isBlocked( ), then the car is already at the barrier. In this case, just turn the car around.

Otherwise, the car has not yet reached the barrier, so start with:

public void ricochet(Car movingCar);

What is the last step that's needed to return to our original location ?

99 ft.

Page 49: Recursive Thinking

movingCar.move( );ricochet(movingCar);movingCar.move( );

Pseudocode for ricochetPseudocode for ricochet

if movingCar.isBlocked( ), then the car is already at the barrier. In this case, just turn the car around.

Otherwise, the car has not yet reached the barrier, so start with:

public void ricochet(Car movingCar);

What is the last step that's needed to return to our original location ?

100 ft.

Page 50: Recursive Thinking

movingCar.move( );ricochet(movingCar);movingCar.move( );

Pseudocode for ricochetPseudocode for ricochet

if movingCar.isBlocked( ), then the car is already at the barrier. In this case, just turn the car around.

Otherwise, the car has not yet reached the barrier, so start with:

public void ricochet(Car movingCar)

This recursive method follows a common pattern that you should recognize.

Page 51: Recursive Thinking

movingCar.move( );ricochet(movingCar);movingCar.move( );

Pseudocode for ricochetPseudocode for ricochet

if movingCar.isBlocked( ), then the car is already at the barrier. In this case, just turn the car around.

Otherwise, the car has not yet reached the barrier, so start with:

public void ricochet(Car movingCar)

When the problem is simple, solve it with no recursive call. This is the base case.

Page 52: Recursive Thinking

movingCar.move( );ricochet(movingCar);movingCar.move( );

Pseudocode for ricochetPseudocode for ricochet

if movingCar.isBlocked( ), then the car is already at the barrier. In this case, just turn the car around.

Otherwise, the car has not yet reached the barrier, so start with:

public void ricochet(Car movingCar)

When the problem is more complex, start by doing work to create a smaller version of the same problem...

Page 53: Recursive Thinking

movingCar.move( );ricochet(movingCar);movingCar.move( );

Pseudocode for ricochetPseudocode for ricochet

if movingCar.isBlocked( ), then the car is already at the barrier. In this case, just turn the car around.

Otherwise, the car has not yet reached the barrier, so start with:

public void ricochet(Car movingCar)

...use a recursive call to completely solve the smaller problem...

Page 54: Recursive Thinking

movingCar.move( );ricochet(movingCar);movingCar.move( );

Pseudocode for ricochetPseudocode for ricochet

if movingCar.isBlocked( ), then the car is already at the barrier. In this case, just turn the car around.

Otherwise, the car has not yet reached the barrier, so start with:

public void ricochet(Car movingCar)

...and finally do any work that's needed to complete the solution of the original problem..

Page 55: Recursive Thinking

Implementation of ricochetImplementation of ricochet

public void ricochet(Car movingCar){ if (movingCar.isBlocked( )) movingCar.turnAround( ); // Base case else { // Recursive pattern movingCar.move( ); ricochet(movingCar); movingCar.move( ); }}

Look for this pattern in the other examples of Chapter 8.

Page 56: Recursive Thinking

An ExerciseAn Exercise

Can you write Can you write ricochet as a new ricochet as a new method of the Car method of the Car class, instead of a class, instead of a separate method?separate method?

You have 2 minutes towrite the implementation.

public void ricochet( ){ . . .

Page 57: Recursive Thinking

An ExerciseAn Exercise

public void ricochet( ){ if (isBlocked( )) turnAround( ); // Base case else { // Recursive pattern move( ); ricochet( ); move( ); }}

One solution: