Recursion - Recursion Page 1 of 10 RECURSION Contents...

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

View Full Document Right Arrow Icon
R ECURSION Contents z Introduction { Test Yourself #1 z How Recursion Really Works { Test Yourself #2 z Recursion vs Iteration { Factorial { Fibonacci { Test Yourself #3 z Recursive Data Structures { Test Yourself #4 z Analyzing Runtime for Recursive Methods { Informal Reasoning { Using Recurrence Equations ± Test Yourself #5 z Using Mathematical Induction to Prove the Correctness of Recursive Code z Summary Introduction Recursion is: z A way of thinking about problems. z A method for solving problems. z Related to mathematical induction. A method is recursive if it can call itself; either directly: or indirectly: You might wonder about the following issues: Q: Does using recursion usually make your code faster ? A: No. Q: Does using recursion usually use less memory ? A: No. Q: Then why use recursion? A: It sometimes makes your code much simpler! One way to think about recursion: z When a recursive call is made, the method clones itself , making new copies of: { the code { the local variables (with their initial values), { the parameters z Each copy of the code includes a marker indicating the current position. When a recursive call is made, the marker in the old copy of the code is just after the call; the marker in the "cloned" copy is at the beginning of the method. z When the method returns, that clone goes away, but the previous ones are still there, and know what to execute next because their current position in the code was saved (indicated by the marker). void f() { ... f() . .. } void f() { ... g() . .. } void g() { ... f() . .. } Page 1 of 10 Recursion 2008/3/27
Background image of page 1

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

View Full Document Right Arrow Icon
Here's an example of a simple recursive method: If the call printInt(2) is made, three "clones" are created, as illustrated below: The original call causes 2 to be output, and then a recursive call is made, creating a clone with k == 1. That clone executes line 1: the if condition is false; line 4: prints 1; and line 5: makes another recursive call, creating a clone with k == 0. That clone just returns (goes away) because the "if" condition is true. The previous clone executes line 6 (the line after its "marker") then returns, and similarly for the original clone. Now let's think about what we have to do to make sure that recursive methods work correctly. First, consider the following recursive method: Note that a runtime error will occur when the call badPrint(2) is made (in particular, an error message like "java.lang.StackOverflowError" will be printed, and the program will stop). This is because there is no code that prevents the recursive call from being made again and again and . ... and eventually the program runs out of memory (to store all the clones). This is an example of an infinite recursion . It inspires: RECURSION RULE #1 Every recursive method must have a base case -- a condition under which no recursive call is made -- to prevent infinite recursion.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 10

Recursion - Recursion Page 1 of 10 RECURSION Contents...

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

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