This preview shows pages 1–3. Sign up to view the full content.
Last Updated 120124 10:12 AM
CSE 2011
Prof. J. Elder
 11 
Linear Recursion Design Pattern
•
Test for base cases
– Begin by testing for a set of base cases (there should be at least
one).
– Every possible chain of recursive calls
must
eventually reach a
base case, and the handling of each base case should not use
recursion.
•
Recurse once
– Perform a single recursive call. (This recursive step may involve
a test that decides which of several possible recursive calls to
make, but it should ultimately choose to make just one of these
calls each time we perform this step.)
– Define each possible recursive call so that it makes
progress
towards a base case.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentLast Updated 120124 10:12 AM
CSE 2011
Prof. J. Elder
 12 
Example 2:
Computing Powers
• The power function
,
p(x,n) = x
n
, can be
defined recursively:
• Assume multiplication takes constant time
(independent of value of arguments).
• This leads to a power function that runs in O
This is the end of the preview. Sign up
to
access the rest of the document.
 Fall '11
 Elder
 Data Structures, Recursion

Click to edit the document details