Lecture15-2 - DataStructures CSCI 132, Spring 2012 Lecture...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Data Structures   CSCI 132, Spring 2012 Lecture 15 Recursion
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Recall Divide and Conquer Play DicePoker Roll Dice Compute Rank Find Winner
Background image of page 2
3 Recursion Skate 4 laps Skate 1 lap Skate 3 laps Skate 1 lap Skate 2 laps When the subproblems are smaller versions of the original problem, we can use recursion to solve the problem.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Example: Skate n laps How to skate n laps using recursion: 1) Skate 1 lap 2) Skate (n - 1) laps The first step is trivial, and we can do it in one step The second step is a smaller version of the original problem. A function that can skate n laps can also skate n-1 laps.
Background image of page 4
5 Recursive Skating void skateLaps( int n) { if ( n < = 0 ) { //do nothing } else { cout << "Skating 1 lap." << endl; skateLaps( n - 1); } } Base Case General Case
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Recursive Function Call A recursive call is a function call in which the called function is the same as the one making the call. In other words,
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 19

Lecture15-2 - DataStructures CSCI 132, Spring 2012 Lecture...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online