Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fundamentals of Java: AP Computer Science Essentials, 4th Edition

Similar presentations


Presentation on theme: "Fundamentals of Java: AP Computer Science Essentials, 4th Edition"— Presentation transcript:

1 Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Sections 13.1 Recursion Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne

2 Introduction Searching and sorting can involve recursion.
Recursive algorithm: refers to itself by name in a manner that appears to be circular. Common in computer science. 2 2

3 Recursion Adding integers 1 to n iteratively:
Another way to look at the problem: Seems to yield a circular definition, but it doesn’t. Example: calculating sum(4): 3 3

4 Recursion (continued)
Recursive functions: the fact that sum(1) is defined to be 1 without making further invocations of sum saves the process from going on forever and the definition from being circular. Iterative: factorial(n) = 1*2*3* n, where n>=1 Recursive: factorial(1)=1; factorial(n)=n*factorial(n-1) if n>1 4 4

5 Recursion (continued)
Recursion involves two factors: Some function f(n) is expressed in terms of f(n-1) and perhaps f(n-2) and so on. To prevent the definition from being circular, f(1) and perhaps f(2) and so on are defined explicitly. Implementing Recursion: Recursive method: one that calls itself. 5 5

6 Recursion (continued)
Recursive: Iterative: 6 6

7 Recursion (continued)
Tracing Recursive Calls: When the last invocation completes, it returns to its predecessor, etc. until the original invocation reactivates and finishes the job. 7 7

8 Recursion (continued)
Guidelines for Writing Recursive Methods: Must have a well-defined stopping state. Recursive step must lead to the stopping state. If not, infinite recursion occurs. Program runs until user terminates, or stack overflow error occurs when Java interpreter runs out of money. 8 8

9 Recursion (continued)
Run-Time Support for Recursive Methods: Call stack: large storage area created at start-up. Activation record: added to top of call stack when a method is called. Space for parameters passed to the method, method’s local variables, and value returned by method. When a method returns, its activation record is removed from the top of the stack. 9 9

10 Recursion (continued)
Run-Time Support for Recursive Methods (cont): Example: an activation record for this method includes: Value of parameter n. The return value of factorial. 10 10

11 Recursion (continued)
Run-Time Support for Recursive Methods (cont): Activation records on the call stack during recursive calls to factorial 11 11

12 Recursion (continued)
Run-Time Support for Recursive Methods (cont): Activation records on the call stack during returns from recursive calls to factorial 12 12

13 Recursion (continued)
When to Use Recursion: Can be used in place of iteration and vice versa. There are many situations in which recursion is the clearest, shortest solution. Examples: Tower of Hanoi, Eight Queens problem. Tail recursive: no work done until after a recursive call. 13 13

14


Download ppt "Fundamentals of Java: AP Computer Science Essentials, 4th Edition"

Similar presentations


Ads by Google