Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.

Slides:



Advertisements
Similar presentations
Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common operations on linked lists.
Advertisements

CSC211 Data Structures Lecture 9 Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach to the.
Container Classes A container class is a data type that is capable of holding a collection of items. In C++, container classes can be implemented as.
L l One of the tree applications in Chapter 9 is binary search trees. l l In Chapter 9, binary search trees are used to implement bags and sets. l l This.
 One of the tree applications in Chapter 10 is binary search trees.  In Chapter 10, binary search trees are used to implement bags and sets.  This presentation.
CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.
 Chapter 2 introduces Object Oriented Programming.  OOP is a relatively new approach to programming which supports the creation of new data types and.
 Chapter 11 has several programming projects, including a project that uses heaps.  This presentation shows you what a heap is, and demonstrates two.
CSC212 Data Structure - Section AB Lecture 20 Hashing Instructor: Edgardo Molina Department of Computer Science City College of New York.
Introduction This chapter explores graphs and their applications in computer science This chapter explores graphs and their applications in computer science.
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
P Chapter 6 introduces the stack data type. p Several example applications of stacks are given in that chapter. p This presentation shows another use called.
  Chapter 12 presents several common algorithms for sorting an array of integers.   Two slow but simple algorithms are Selectionsort and Insertionsort.
P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates.
CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York.
 Chapter 10 introduces trees.  This presentation illustrates the simplest kind of trees: Complete Binary Trees. Complete Binary Trees Data Structures.
CSC212 Data Structure - Section FG Lecture 9 Linked Lists Instructor: Zhigang Zhu Department of Computer Science City College of New York.
Copyright © 1999, Carnegie Mellon. All Rights Reserved. Chapter 12 presents several common algorithms for sorting an array of integers. Two slow but simple.
L l Chapter 11 discusses several ways of storing information in an array, and later searching for the information. l l Hash tables are a common approach.
 An important topic: preconditions and postconditions.  They are a method of specifying what a function accomplishes. Preconditions and Postconditions.
L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked.
 Chapter 9 introduces the technique of recursive programming.  As you have seen, recursive programming involves spotting smaller occurrences of a problem.
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
CSC212 Data Structure - Section FG Lecture 12 Stacks and Queues Instructor: Zhigang Zhu Department of Computer Science City College of New York.
 Chapter 8 introduces the queue data type.  Several example applications of queues are given in that chapter.  This presentation describes the queue.
Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)
P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Preconditions and Postconditions.
 A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with.
P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Illinois State University Department.
CSC211 Data Structures Lecture 20 Hashing Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
 An important topic: preconditions and postconditions.  They are a method of specifying what a method accomplishes. Preconditions and Postconditions.
Software Development p Data structure p A collection of data organized so that the data can be accessed using a set of specific techniques p Object-oriented.
CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
CSC211 Data Structures Lecture 13 Recursive Thinking Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
CSC211 Data Structures Lecture 18 Heaps and Priority Queues Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
P p Chapter 10 introduces trees. p p This presentation illustrates the simplest kind of trees: Complete Binary Trees. Complete Binary Trees Data Structures.
Container Classes  A container class is a data type that is capable of holding a collection of items.  In C++, container classes can be implemented as.
CSC212 Data Structure - Section AB Lecture 13 Recursive Thinking Instructor: Edgardo Molina Department of Computer Science City College of New York.
CSC212 Data Structure Lecture 16 Heaps and Priority Queues Instructor: George Wolberg Department of Computer Science City College of New York.
Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 2 ADT and C++ Classes (I) Instructor: Zhigang Zhu Department of Computer Science City.
 Chapter 6 introduces templates, which are a C++ feature that easily permits the reuse of existing code for new purposes.  This presentation shows how.
Using a Stack Chapter 6 introduces the stack data type.
Linked Lists in Action Chapter 5 introduces the often-used data public classure of linked lists. This presentation shows how to implement the most common.
Lecture 11 Recursive Thinking CSC212 Data Structure
Collection Classes A Collection class is a data type that is capable of holding a group of items. In Java, Collection classes can be implemented as a class,
Using a Stack Chapter 6 introduces the stack data type.
Recursive Thinking Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Hash Tables Chapter 11 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach.
Using a Stack Chapter 6 introduces the stack data type.
Quadratic Sorting Chapter 12 presents several common algorithms for sorting an array of integers. Two slow but simple algorithms are Selectionsort and.
CSC212 Data Structure - Section AB
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Using a Stack Chapter 6 introduces the stack data type.
Binary Search Trees One of the tree applications in Chapter 9 is binary search trees. In Chapter 9, binary search trees are used to implement bags and.
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
Using a Stack Chapter 6 introduces the stack data type.
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
Hash Tables Chapter 11 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach.
Hash Tables Chapter 11 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach.
CSC212 Data Structure - Section KL
Binary Search Trees One of the tree applications in Chapter 9 is binary search trees. In Chapter 9, binary search trees are used to implement bags and.
Using a Queue Chapter 8 introduces the queue data type.
Linked Lists in Action Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common.
Using a Queue Chapter 8 introduces the queue data type.
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Hash Tables Chapter 11 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach.
Presentation transcript:

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

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

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

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

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

A 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

A 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

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 Class

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

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

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

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

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

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

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

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

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

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

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

Your 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)

Pseudocode 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)

Pseudocode 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( );...

movingCar.move( );... Pseudocode 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 ft.

movingCar.move( );... Pseudocode 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.

movingCar.move( );... Pseudocode 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.

movingCar.move( ); ricochet(movingCar);... Pseudocode 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.

movingCar.move( ); ricochet(movingCar);... Pseudocode 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.

movingCar.move( ); ricochet(movingCar);... Pseudocode 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.

movingCar.move( ); ricochet(movingCar);... Pseudocode 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.

movingCar.move( ); ricochet(movingCar);... Pseudocode 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.

movingCar.move( ); ricochet(movingCar);... Pseudocode 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.

movingCar.move( ); ricochet(movingCar);... Pseudocode 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.

movingCar.move( ); ricochet(movingCar);... Pseudocode 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.

movingCar.move( ); ricochet(movingCar);... Pseudocode 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.

movingCar.move( ); ricochet(movingCar);... Pseudocode 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.

movingCar.move( ); ricochet(movingCar);... Pseudocode 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.

movingCar.move( ); ricochet(movingCar);... Pseudocode 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.

movingCar.move( ); ricochet(movingCar); movingCar.move( ); Pseudocode 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.

movingCar.move( ); ricochet(movingCar); movingCar.move( ); Pseudocode 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.

movingCar.move( ); ricochet(movingCar); movingCar.move( ); Pseudocode 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.

movingCar.move( ); ricochet(movingCar); movingCar.move( ); Pseudocode 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...

movingCar.move( ); ricochet(movingCar); movingCar.move( ); Pseudocode 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...

movingCar.move( ); ricochet(movingCar); movingCar.move( ); Pseudocode 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..

Implementation 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.

An Exercise Can you write ricochet as a new method of the Car class, instead of a separate method? You have 2 minutes to write the implementation. public void ricochet( ) {...

An Exercise public void ricochet( ) { if (isBlocked( )) turnAround( ); // Base case else { // Recursive pattern move( ); ricochet( ); move( ); } One solution:

T HE E ND Presentation copyright 1999 Addison Wesley Longman, For use with Data Structures and Other Objects Using Java by Michael Main. Some artwork in the presentation is used with permission from Presentation Task Force (copyright New Vision Technologies Inc) and Corel Gallery Clipart Catalog (copyright Corel Corporation, 3G Graphics Inc, Archive Arts, Cartesia Software, Image Club Graphics Inc, One Mile Up Inc, TechPool Studios, Totem Graphics Inc). Students and instructors who use Data Structures and Other Objects Using Java are welcome to use this presentation however they see fit, so long as this copyright notice remains intact.