1 / 57

Recursive Thinking

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

Download Presentation

Recursive Thinking

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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. Data Structures and Other Objects Using Java

  2. First Example • Task to write a non-negative integer to screen with decimal digits stacked • Number 7895 • Output 7 8 9 5

  3. One digit solution • Print that digit! • Number 7 • Output 7

  4. Many digit solution • Print all digits except the last, stacked vertically • Then print the last digit 7 8 9 • Now print the 5 which is 7895%10

  5. Pseudocode //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.

  6. Recursive solution • Always has a stopping case (base case) if (number < 10) System.out.println(number); • Always has a recursive call else { writeVertical(number/10); system.out.println(number%10); }

  7. Recursively call Number is 7895 Call4 7 Call 3 78 Call 3 78 Call 2 789 Call 2 789 Call 2 789 Call 2 789 Call 1 7895 Call 1 7895 Call 1 7895 Call 1 7895 Call 1 7895

  8. Now come back Call4 7 Call 4 with a 7 is a one digit number Therefore, print out that single digit and that call is complete Call 3 78 Call 3 78 Call 2 789 Call 2 789 7 Call 1 7895 Call 1 7895

  9. Now return from 4th call 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 Call 3 78 7 8 Call 2 789 Call 2 789 Call 1 7895 Call 1 7895

  10. Now return from third call 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 7 8 9 Call 2 789 Call 1 7895 Call 1 7895

  11. Returning 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 7 8 9 5 Call 1 7895

  12. Return from first call We’re Done!!!!

  13. To start the example, think about your favorite family car A Car Object

  14. To start the example, think about your favorite family car A Car Object

  15. To start the example, think about your favorite family car A Car Object

  16. To start the example, think about your favorite family car A Car Object

  17. To start the example, think about your favorite family car Imagine that the car is controlled by a radio signal from a computer A Car Object

  18. 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 A Car Class public class Car { . . . }

  19. methods for the Car Class 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! ... }

  20. When we declare a Car and activate the constructor, the computer makes a radio link with a car that has a particular number. The Constructor public static void main(...) { Car racer = new Car(7); . . .

  21. When we activate turnAround, the computer signals the car to turn 180 degrees. The turnAround method public static void main(...) { Car racer = new Car(7); racer.turnAround( ); . . .

  22. When we activate move, the computer signals the car to move forward one foot. The move method public static void main(...) { Car racer = new Car(7); racer.turnAround( ); racer.move( ); . . .

  23. When we activate move, the computer signals the car to move forward one foot. The move method public static void main(...) { Car racer = new Car(7); racer.turnAround( ); racer.move( ); . . .

  24. The isBlocked method detects barriers. The isBlocked( ) method public static void main(...) { Car racer = new Car(7); racer.turnAround( ); racer.move( ); if (racer.isBlocked( ) ) System.out.println (“Cannot move!”); . . .

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

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

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

  28. Write a method which will move a Car forward until it reaches a barrier... ...then the car is turned around... Your Mission

  29. 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. Your Mission

  30. 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. Your Mission

  31. 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. Your Mission public void ricochet(Car movingCar)

  32. if movingCar.isBlocked( ), then the car is already at the barrier. In this case, just turn the car around. Pseudocode for ricochet public void ricochet(Car movingCar)

  33. 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: Pseudocode for ricochet public void ricochet(Car movingCar) movingCar.move( ); . . .

  34. 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: Pseudocode for ricochet public void ricochet(Car movingCar); This makes the problem a bit smaller. For example, if the car started 100 feet from the barrier... movingCar.move( ); . . . 100 ft.

  35. 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: Pseudocode for ricochet 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. movingCar.move( ); . . . 99 ft.

  36. 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: Pseudocode for ricochet public void ricochet(Car movingCar); We now have a smaller version of the same problem that we started with. movingCar.move( ); . . . 99 ft.

  37. 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: Pseudocode for ricochet public void ricochet(Car movingCar); Make a recursive call to solve the smaller problem. movingCar.move( ); ricochet(movingCar); . . . 99 ft.

  38. 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: Pseudocode for ricochet public void ricochet(Car movingCar); The recursive call will solve the smaller problem. movingCar.move( ); ricochet(movingCar); . . . 99 ft.

  39. 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: Pseudocode for ricochet public void ricochet(Car movingCar); The recursive call will solve the smaller problem. movingCar.move( ); ricochet(movingCar); . . .

  40. 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: Pseudocode for ricochet public void ricochet(Car movingCar); The recursive call will solve the smaller problem. movingCar.move( ); ricochet(movingCar); . . .

  41. 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: Pseudocode for ricochet public void ricochet(Car movingCar); The recursive call will solve the smaller problem. movingCar.move( ); ricochet(movingCar); . . .

  42. 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: Pseudocode for ricochet public void ricochet(Car movingCar); The recursive call will solve the smaller problem. movingCar.move( ); ricochet(movingCar); . . .

  43. 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: Pseudocode for ricochet public void ricochet(Car movingCar); The recursive call will solve the smaller problem. movingCar.move( ); ricochet(movingCar); . . .

  44. 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: Pseudocode for ricochet public void ricochet(Car movingCar); The recursive call will solve the smaller problem. movingCar.move( ); ricochet(movingCar); . . .

  45. 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: Pseudocode for ricochet public void ricochet(Car movingCar); The recursive call will solve the smaller problem. movingCar.move( ); ricochet(movingCar); . . .

  46. 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: Pseudocode for ricochet public void ricochet(Car movingCar); The recursive call will solve the smaller problem. movingCar.move( ); ricochet(movingCar); . . .

  47. 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: Pseudocode for ricochet public void ricochet(Car movingCar); The recursive call will solve the smaller problem. movingCar.move( ); ricochet(movingCar); . . . 99 ft.

  48. 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: Pseudocode for ricochet public void ricochet(Car movingCar); What is the last step that's needed to return to our original location ? movingCar.move( ); ricochet(movingCar); . . . 99 ft.

  49. 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: Pseudocode for ricochet public void ricochet(Car movingCar); What is the last step that's needed to return to our original location ? movingCar.move( ); ricochet(movingCar); movingCar.move( ); 100 ft.

  50. 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: Pseudocode for ricochet public void ricochet(Car movingCar) This recursive method follows a common pattern that you should recognize. movingCar.move( ); ricochet(movingCar); movingCar.move( );

More Related