CSC212 Data Structure - Section AB Lecture 13 Recursive Thinking Instructor: Edgardo Molina Department of Computer Science City College of New York.

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.
Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.
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.
Copyright © 2003 Pearson Education, Inc. Slide 1.
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.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
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 Lecture 16 Heaps and Priority Queues Instructor: George Wolberg Department of Computer Science City College of New York.
1 CSC212 Data Structure Lecture 4 Container Classes Instructor: George Wolberg Department of Computer Science City College of New York.
Xiaoyan Li CSC211 Data Structures Lecture 2 ADT and C++ Classes (I) Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
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.
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
CSC212 Data Structure - Section AB
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Using a Stack Chapter 6 introduces the stack data type.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
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.
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.
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.
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:

CSC212 Data Structure - Section AB Lecture 13 Recursive Thinking Instructor: Edgardo Molina Department of Computer Science City College of New York

Outline of This Lecture p Start with an Example of Recursion p “racing car” – not in the textbook p using slides (provided by the authors) p Recursive Thinking: General Form p Tracing Recursive Calls p using blackboard to show the concepts p A Closer Look at Recursion p activation record and runtime stack

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

Presentation copyright 1997 Addison Wesley Longman, For use with Data Structures and Other Objects Using C++ by Michael Main and Walter Savitch. 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 C++ are welcome to use this presentation however they see fit, so long as this copyright notice remains intact. The Car Racer Slides were modified from

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

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

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

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

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

A Car Class class Car { public:... }; p p To start the example, think about your favorite family car p p Imagine that the car is controlled by a radio signal from a computer p p The radio signals are generated by activating member functions of a Car object

class Car { public: Car(int car_number); void move( ); void turn_around( ); bool is_blocked( ); private: { We don't need to know the private fields! }... }; Member Functions for the Car Class

int main( ) { Car racer(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.

int main( ) { Car racer(7); racer.turn_around( );... The turn_around Function When we activate turn_around, the computer signals the car to turn 180 degrees.

int main( ) { Car racer(7); racer.turn_around( );... The turn_around Function When we activate turn_around, the computer signals the car to turn 180 degrees.

int main( ) { Car racer(7); racer.turn_around( ); racer.move( );... The move Function When we activate move, the computer signals the car to move forward one foot.

int main( ) { Car racer(7); racer.turn_around( ); racer.move( );... The move Function When we activate move, the computer signals the car to move forward one foot.

int main( ) { Car racer(7); racer.turn_around( ); racer.move( ); if (racer.is_blocked( ) ) cout << "Cannot move!";... The is_blocked( ) Function The is_blocked member function detects barriers.

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

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

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

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

Your Mission p p Write a function which will move a Car forward until it reaches a barrier... p p...then the car is turned around... p p...and returned to its original location, facing the opposite way.

Your Mission p p Write a function which will move a Car forward until it reaches a barrier... p p...then the car is turned around... p p...and returned to its original location, facing the opposite way.

Your Mission p p Write a function which will move a Car forward until it reaches a barrier... p p...then the car is turned around... p p...and returned to its original location, facing the opposite way. void ricochet(Car& moving_car );

Pseudocode for ricochet ¶ ¶if moving_car.is_blocked( ), then the car is already at the barrier. In this case, just turn the car around. void ricochet(Car& moving_car );

Pseudocode for ricochet ¶ ¶if moving_car.is_blocked( ), 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: void ricochet(Car& moving_car); moving_car.move( );...

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

moving_car.move( );... Pseudocode for ricochet ¶ ¶if moving_car.is_blocked( ), 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: void ricochet(Car& moving_car); 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.

moving_car.move( );... Pseudocode for ricochet ¶ ¶if moving_car.is_blocked( ), 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: void ricochet(Car& moving_car); We now have a smaller version of the same problem that we started with. 99 ft.

moving_car.move( ); ricochet(moving_car);... Pseudocode for ricochet ¶ ¶if moving_car.is_blocked( ), 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: void ricochet(Car& moving_car); Make a recursive call to solve the smaller problem. 99 ft.

moving_car.move( ); ricochet(moving_car);... Pseudocode for ricochet ¶ ¶if moving_car.is_blocked( ), 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: void ricochet(Car& moving_car); The recursive call will solve the smaller problem. 99 ft.

moving_car.move( ); ricochet(moving_car);... Pseudocode for ricochet ¶ ¶if moving_car.is_blocked( ), 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: void ricochet(Car& moving_car); The recursive call will solve the smaller problem.

moving_car.move( ); ricochet(moving_car);... Pseudocode for ricochet ¶ ¶if moving_car.is_blocked( ), 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: void ricochet(Car& moving_car); The recursive call will solve the smaller problem.

moving_car.move( ); ricochet(moving_car);... Pseudocode for ricochet ¶ ¶if moving_car.is_blocked( ), 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: void ricochet(Car& moving_car); The recursive call will solve the smaller problem.

moving_car.move( ); ricochet(moving_car);... Pseudocode for ricochet ¶ ¶if moving_car.is_blocked( ), 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: void ricochet(Car& moving_car); The recursive call will solve the smaller problem.

moving_car.move( ); ricochet(moving_car);... Pseudocode for ricochet ¶ ¶if moving_car.is_blocked( ), 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: void ricochet(Car& moving_car); The recursive call will solve the smaller problem.

moving_car.move( ); ricochet(moving_car);... Pseudocode for ricochet ¶ ¶if moving_car.is_blocked( ), 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: void ricochet(Car& moving_car); The recursive call will solve the smaller problem.

moving_car.move( ); ricochet(moving_car);... Pseudocode for ricochet ¶ ¶if moving_car.is_blocked( ), 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: void ricochet(Car& moving_car); The recursive call will solve the smaller problem.

moving_car.move( ); ricochet(moving_car);... Pseudocode for ricochet ¶ ¶if moving_car.is_blocked( ), 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: void ricochet(Car& moving_car); The recursive call will solve the smaller problem.

moving_car.move( ); ricochet(moving_car);... Pseudocode for ricochet ¶ ¶if moving_car.is_blocked( ), 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: void ricochet(Car& moving_car); The recursive call will solve the smaller problem. 99 ft.

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

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

moving_car.move( ); ricochet(moving_car); moving_car.move( ); Pseudocode for ricochet ¶ ¶if moving_car.is_blocked( ), 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: void ricochet(Car& moving_car); This recursive function follows a common pattern that you should recognize.

moving_car.move( ); ricochet(moving_car); moving_car.move( ); Pseudocode for ricochet ¶ ¶if moving_car.is_blocked( ), 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: void ricochet(Car& moving_car); When the problem is simple, solve it with no recursive call. This is the base case or the stopping case.

moving_car.move( ); ricochet(moving_car); moving_car.move( ); Pseudocode for ricochet ¶ ¶if moving_car.is_blocked( ), 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: void ricochet(Car& moving_car); When the problem is more complex, start by doing work to create a smaller version of the same problem...

moving_car.move( ); ricochet(moving_car); moving_car.move( ); Pseudocode for ricochet ¶ ¶if moving_car.is_blocked( ), 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: void ricochet(Car& moving_car);...use a recursive call to completely solve the smaller problem...

moving_car.move( ); ricochet(moving_car); moving_car.move( ); Pseudocode for ricochet ¶ ¶if moving_car.is_blocked( ), 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: void ricochet(Car& moving_car);...and finally do any work that's needed to complete the solution of the original problem..

Implementation of ricochet void ricochet(Car& moving_car) { if (moving_car.is_blocked( )) moving_car.turn_around( ); // Base case else { // Recursive pattern moving_car.move( ); ricochet(moving_car); moving_car.move( ); } Look for this pattern in the other examples of Chapter 9.

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

An Exercise void Car::ricochet( ) { if (is_blocked( )) turn_around( ); // Base case else { // Recursive pattern move( ); ricochet( ); move( ); } One solution:

Recursive Thinking: General Form p Recursive Calls p Suppose a problem has one or more cases in which some of the subtasks are simpler versions of the original problem. These subtasks can be solved by recursive calls p Stopping Cases /Base Cases p A function that makes recursive calls must have one or more cases in which the entire computation is fulfilled without recursion. These cases are called stopping cases or base cases

Tracing Recursive Calls: Ricochet void Car::ricochet( ) { if (is_blocked( )) A. turn_around( ); // Base case else { // Recursive pattern B. move( ); C. ricochet( ); D. move( ); E } } Do it by hand if car is 4 feet away from the barrier

A Close Look at Ricochet Recursion p The recursive case and the stopping case p Activation record p The return location only in this example – other information is kept in the object racer p The running stack p The collection of the activation records is stored in a stack data structure

Example 2: Write Number Vertically p Task p Write a non-negative integer to the screen with its decimal digits stacked vertically p for example: Input 1234 Output:

A possible function void write_vertical (unsigned int number) // precondition: number >=0 // Postcondition: The digits of number have been written, stacked vertically. { assert(number>=0); do { cout << number % 10 << endl; // Write a digit number = number / 10; } while (number !=0); } Write an integer number vertically Input 1234 Output:

Approach 1: using a stack void stack_write_vertical (unsigned int number) // Postcondition: The digits of number have been written, stacked vertically. { stack s; do { s.push(number % 10); // push a digit in the stack number = number / 10; } while (number !=0); while (!(s.empty())) { cout << s.top()<< endl; //print a digit from the stack s.pop(); } Write an integer number vertically

Approach 2: Using Recursion void recursive_write_vertical(unsigned int number) // Postcondition: The digits of number have been written, stacked vertically. { if (number < 10) // stopping case cout << number << endl; // Write the one digit else // including recursive calls { recursive_write_vertical(number/10); // Write all but the last digit cout << number % 10 << endl; // Write the last digit } Write an integer number vertically

Tracing Recursive Calls void recursive_write_vertical_2(unsigned int number) // Postcondition: The digits of number have been written, stacked vertically. { if (number < 10) // stopping case A cout << number << endl; // Write the one digit else // including recursive calls { B recursive_write_vertical(number/10); // Write all but the last digit C cout << number % 10 << endl; // Write the last digit D } } Write an integer number vertically

A Closer Look at the Recursion p Recursive Function p Recursive calls p Stopping (Base) cases p Run-time Stack p the collection of activation records is stored in the stack p Activation Record - a special memory block including p return location of a function call p values of the formal parameters and local variables

Recursive Thinking: General Form p Recursive Calls p Suppose a problem has one or more cases in which some of the subtasks are simpler versions of the original problem. These subtasks can be solved by recursive calls p Stopping Cases /Base Cases p A function that makes recursive calls must have one or more cases in which the entire computation is fulfilled without recursion. These cases are called stopping cases or base cases

Self-Tests and More Complicated Examples p An Extension of write_vertical (page 436) p handles all integers including negative ones p Hints: you can have more than one recursive calls or stopping cases in your recursive function p Homework p Reading: Section 9.1 p Self-Test: Exercises 1-8 p Advanced Reading: Section 9.2 p Assignment 4 online

super_write_vertical void super_write_vertical(int number) // Postcondition: The digits of the number have been written, stacked vertically. // If number is negative, then a negative sign appears on top. // Library facilities used: iostream.h, math.h { if (number < 0) { cout << '-' << endl; // print a negative sign super_write_vertical(abs(number)); // abs computes absolute value // This is Spot #1 referred to in the text. } else if (number < 10) cout << number << endl; // Write the one digit else { super_write_vertical(number/10); // Write all but the last digit // This is Spot #2 referred to in the text. cout << number % 10 << endl; // Write the last digit } Write any integer number vertically