Lecture15-2

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

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

1 Data Structures   CSCI 132, Spring 2012 Lecture 15 Recursion

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

View Full Document
2 Recall Divide and Conquer Play DicePoker Roll Dice Compute Rank Find Winner
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.

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

View Full Document
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.
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

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

View Full Document
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,
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### 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
Ask a homework question - tutors are online