lec16

# lec16 - CSE 12 Recursion What is recursion Recursive...

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

16 What is recursion? Recursive definitions Recursion as a problem solving strategy 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
What is recursion? A recursive function is a function that calls itself Writing recursive functions is a very useful way to program… sometimes However, it can be tricky to do it right
Why use recursion? Recursion can be a very useful programming technique If you have a problem that you can think of this way: Instances of the problem come in different sizes Very small instances of the problem can be solved directly Larger instances of the problem can be broken into smaller instances of the same kind of problem Then writing a recursive function to solve the problem can be the way to go

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

View Full Document
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”)
“base part” and “recursive part” The base part of a recursive definition is important: it keeps the definition from being completely circular But it is also 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: the base part involves the smallest possible version of the problem, and the recursive part involves a smaller version of the problem than the original In the definition of the N th Fibonacci number, the recursive part involves definitions of the N-1 st and N-2 st Fibonacci numbers, which are “smaller” versions of the same concept

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

View Full Document
Recursive definitions in computer science 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 ?
Parts of a Recursive Definition a base case , which is non- recursive and, consequently, terminates the recursive application of the definition a recursive case , which reapplies the definition, but to an instance that is closer to the base case Always keep in mind that a recursive definition contains two parts:

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

View Full Document
Defining data structures recursively: linked lists It is possible to define the concept “linked list” using a recursive definition: A linked list is: 1). null, or 2). a node followed by a linked list
A singly linked list viewed from a recursive perspective

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 03/27/2012 for the course CSE 12 taught by Professor Gary during the Fall '08 term at UCSD.

### Page1 / 29

lec16 - CSE 12 Recursion What is recursion Recursive...

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

View Full Document
Ask a homework question - tutors are online