This preview shows pages 1–10. Sign up to view the full content.
111/43
•
What is recursion?
•
Recursive definitions
•
Recursive programming
•
Relationship between recursion and iteration
•
Time and space costs of recursion
CSE 12
Recursion
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document 112/43
What is recursion?
•
A recursive
function
is a function that calls itself
•
A recursive
definition
is a definition that defines a concept
in terms of itself
•
Important: The recursion has to stop at some point,
otherwise you have a function that never returns, or you
have a completely circular definition
... and neither of those would be very useful!
113/43
Uses for recursion
•
Recursion has applications in
–
defining mathematical concepts
–
specification of programming language elements
–
defining data structures
–
designing algorithms
Let's look at examples of each
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document 114/43
Recursive definitions in mathematics
•
Recursive definitions are common in mathematics. For
example:
The Nth Fibonacci number is
a) 1, if N is equal to 1 or 2; or,
b) the sum of the N1 and N2 Fibonacci numbers, if N > 2
•
a) is called the “base part” of the definition:
it keeps the
definition from being completely circular, because it makes no
mention of the concept that is being defined
•
b) is called the “recursive part” of the definition: it mentions the
concept that is being defined (“Fibonacci number”)
115/43
“base part” and “recursive part”
•
Obviously the base part of a recursive definition is important: it
keeps the definition from being completely circular
•
But for this to work, it is important that the recursive part of the
definition brings the definition closer to the base part, so that
the base part will eventually apply
•
That is: if you think of the base part as applying to the smallest
possible version of the problem, the recursive part should
involve smaller versions of the problem than the original
•
For example, in the definition of the Nth Fibonacci number, the
recursive part involves definitions of the N1
st
and N2
st
Fibonacci numbers, which are closer to the base case of the 1
st
and 2
nd
Fibonacci numbers
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document 116/43
Applying a recursive definition
•
What is the first Fibonacci number (N=1)?
Apply rule a).
•
What is the second Fibonacci number (N=2)?
Apply rule a).
•
What is the third Fibonacci number (N=3)?
Apply rule b) once, and a) twice.
•
What is the fourth Fibonacci number (N=4)?
117/43
Recursive definitions in computer
science
•
Some data structures have natural recursive
definitions
–
Examples:
lists and trees
•
Also, features of programming languages are
often defined recursively
–
Examples:
you can define “integer literal constant” or
“identifier” or “statement” recursively
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document 118/43
A recursive definition
•
In Java or C or C++, you can define “integer
literal constant” this way:
An integer literal constant is
a) a digit, or
b) a digit followed by an integer literal constant
A digit is
one of 0,1,2,3,4,5,6,7,8, or 9
Does
8
fit the definition?
does
82
?
does
82X
?
119/43
Levels in Programming Languages
•
In defining a programming language, usually there are two
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview. Sign up
to
access the rest of the document.
This note was uploaded on 06/15/2011 for the course ECON 1 taught by Professor Aben during the Fall '07 term at City College of San Francisco.
 Fall '07
 Aben

Click to edit the document details