Lec11 - CSE 12 Recursion What is recursion Recursive definitions Recursive programming Relationship between recursion and iteration Time and space

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

View Full Document Right Arrow Icon
11-1/43 What is recursion? Recursive definitions Recursive programming Relationship between recursion and iteration Time and space costs of recursion CSE 12 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
11-2/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!
Background image of page 2
11-3/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
Background image of page 3

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

View Full DocumentRight Arrow Icon
11-4/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 N-1 and N-2 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”)
Background image of page 4
11-5/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 N-1 st and N-2 st Fibonacci numbers, which are closer to the base case of the 1 st and 2 nd Fibonacci numbers
Background image of page 5

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

View Full DocumentRight Arrow Icon
11-6/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)?
Background image of page 6
11-7/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
Background image of page 7

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

View Full DocumentRight Arrow Icon
11-8/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 ?
Background image of page 8
11-9/43 Levels in Programming Languages In defining a programming language, usually there are two
Background image of page 9

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

View Full DocumentRight Arrow Icon
Image of page 10
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.

Page1 / 44

Lec11 - CSE 12 Recursion What is recursion Recursive definitions Recursive programming Relationship between recursion and iteration Time and space

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

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