This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: CSCI1200 Data Structures — Fall 2010 Lecture 12 — Advanced Recursion Announcements: Test 2 Information • Test 2 will be held Tuesday, October 19th, 2010 from 23:50pm in West Hall Auditorium . No make ups will be given except for emergency situations, and even then a written excuse from the Dean of Students or the Office of Student Experience will be required. • Coverage: Lectures 112, Labs 17, HW 15. • Closedbook and closednotes except for 1 sheet of notes on 8.5x11 inch paper (front & back) that may be handwritten or printed . Computers, cellphones, palm pilots, calculators, PDAs, music players, etc. are not permitted and must be turned off. • All students must bring their Rensselaer photo ID card. • Practice problems from previous exams are available on the course website. Solutions to the problems will be posted a day or two before the exam. Review from Lecture 11 & Lab 7 • Limitations of singlylinked lists • Doublylinked lists: – Structure – Insert – Remove • Our own version of the STL list<T> class, named dslist • Implementing list<T>::iterator • Importance of destructors & using valgrind to find memory errors • Decrementing the end() iterator Today: Advanced Recursion Examples • “Rules” for writing recursive functions 1. Handle the base case(s). 2. Define the problem solution in terms of smaller instances of the problem. Use wishful thinking , i.e., if someone else solves the problem of fact(4) I can extend that solution to solve fact(5) . This defines the necessary recursive calls. It is also the hardest part! 3. Figure out what work needs to be done before making the recursive call(s). 4. Figure out what work needs to be done after the recursive call(s) complete(s) to finish the computation. (What are you going to do with the result of the recursive call?) 5. Assume the recursive calls work correctly, but make sure they are progressing toward the base case(s)! • In the examples we saw the first week of class — factorial, fibonacci and even binary search — the problems could be easily solved without recursion. The same is not true for all problems. For example, the functions we will consider today cannot be easily solved using iteration (for or while loops): – Merge sort – Nonlinear maze search 12.1 Recursion Example: Merge Sort • Idea: – Split a vector in half – Recursively sort each half – Merge the two sorted halves into a single sorted vector • Suppose we have a vector called values having two halves that are each already sorted. In particular, the values in subscript ranges [low..mid] (the lower interval) and [mid+1..high] (the upper interval) are each in increasing order....
View
Full
Document
This note was uploaded on 02/21/2012 for the course CSCI 1200 taught by Professor Cutler during the Fall '08 term at Rensselaer Polytechnic Institute.
 Fall '08
 CUTLER
 Data Structures, Recursion

Click to edit the document details