1 / 28

L09 (Chapter 19) Recursion 1

L09 (Chapter 19) Recursion 1. Objectives. To know what is a recursive method and the benefits of using recursive methods (§19.1). To determine the base cases in a recursive method (§§19.2-19.5). To understand how recursive method calls are handled in a call stack (§§19.2-19.5).

Download Presentation

L09 (Chapter 19) Recursion 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. L09 (Chapter 19) Recursion 1

  2. Objectives • To know what is a recursive method and the benefits of using recursive methods (§19.1). • To determine the base cases in a recursive method (§§19.2-19.5). • To understand how recursive method calls are handled in a call stack (§§19.2-19.5). • To solve problems using recursion (§§19.2-19.5). • To use an overloaded helper method to derive a recursive method (§19.4). • To understand the relationship and difference between recursion and iteration (§19.6).

  3. Computing Factorial factorial(0) = 1; factorial(n) = n*factorial(n-1); ComputeFactorial

  4. animation Computing Factorial factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(3)

  5. animation Computing Factorial factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(3) = 3 * factorial(2)

  6. animation Computing Factorial factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(3) = 3 * factorial(2) = 3 * (2 * factorial(1))

  7. animation Computing Factorial factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(3) = 3 * factorial(2) = 3 * (2 * factorial(1)) = 3 * ( 2 * (1 * factorial(0)))

  8. animation Computing Factorial factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(3) = 3 * factorial(2) = 3 * (2 * factorial(1)) = 3 * ( 2 * (1 * factorial(0))) = 3 * ( 2 * ( 1 * 1)))

  9. animation Computing Factorial factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(3) = 3 * factorial(2) = 3 * (2 * factorial(1)) = 3 * ( 2 * (1 * factorial(0))) = 3 * ( 2 * ( 1 * 1))) = 3 * ( 2 * 1)

  10. animation Computing Factorial factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(3) = 3 * factorial(2) = 3 * (2 * factorial(1)) = 3 * ( 2 * (1 * factorial(0))) = 3 * ( 2 * ( 1 * 1))) = 3 * ( 2 * 1) = 3 * 2

  11. animation Computing Factorial factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(3) = 3 * factorial(2) = 3 * (2 * factorial(1)) = 3 * ( 2 * (1 * factorial(0))) = 3 * ( 2 * ( 1 * 1))) = 3 * ( 2 * 1) = 3 * 2 = 6

  12. animation Trace Recursive factorial Executes factorial(4)

  13. animation Trace Recursive factorial Executes factorial(3)

  14. animation Trace Recursive factorial Executes factorial(2)

  15. animation Trace Recursive factorial Executes factorial(1)

  16. animation Trace Recursive factorial Executes factorial(0)

  17. animation Trace Recursive factorial returns 1

  18. animation Trace Recursive factorial returns factorial(0)

  19. animation Trace Recursive factorial returns factorial(1)

  20. animation Trace Recursive factorial returns factorial(2)

  21. animation Trace Recursive factorial returns factorial(3)

  22. animation Trace Recursive factorial returns factorial(4)

  23. factorial(4) Stack Trace

  24. Stack view Invoke factorial(0) Invoke factorial(1) Invoke factorial(2) Invoke factorial(3) Invoke factorial(4) Invoke main method See Recursive Calls in JBuilder Debugger You can see the chain of recursive method invocations in the stack view of the JBuilder debugger.

  25. Other Examples f(0) = 0; f(n) = n + f(n-1);

  26. Fibonacci Numbers Finonacci series: 0 1 1 2 3 5 8 13 21 34 55 89… indices: 0 1 2 3 4 5 6 7 8 9 10 11 fib(0) = 0; fib(1) = 1; fib(index) = fib(index -1) + fib(index -2); index >=2 fib(3) = fib(2) + fib(1) = (fib(1) + fib(0)) + fib(1) = (1 + 0) +fib(1) = 1 + fib(1) = 1 + 1 = 2 ComputeFibonacci

  27. Fibonnaci Numbers, cont.

  28. Characteristics of Recursion All recursive methods have the following characteristics: • One or more base cases (the simplest case) are used to stop recursion. • Every recursive call reduces the original problem, bringing it increasingly closer to a base case until it becomes that case. In general, to solve a problem using recursion, you break it into subproblems. If a subproblem resembles the original problem, you can apply the same approach to solve the subproblem recursively. This subproblem is almost the same as the original problem in nature with a smaller size.

More Related