280 likes | 392 Views
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).
E N D
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).
Computing Factorial factorial(0) = 1; factorial(n) = n*factorial(n-1); ComputeFactorial
animation Computing Factorial factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(3)
animation Computing Factorial factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(3) = 3 * factorial(2)
animation Computing Factorial factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(3) = 3 * factorial(2) = 3 * (2 * factorial(1))
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)))
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)))
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)
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
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
animation Trace Recursive factorial Executes factorial(4)
animation Trace Recursive factorial Executes factorial(3)
animation Trace Recursive factorial Executes factorial(2)
animation Trace Recursive factorial Executes factorial(1)
animation Trace Recursive factorial Executes factorial(0)
animation Trace Recursive factorial returns 1
animation Trace Recursive factorial returns factorial(0)
animation Trace Recursive factorial returns factorial(1)
animation Trace Recursive factorial returns factorial(2)
animation Trace Recursive factorial returns factorial(3)
animation Trace Recursive factorial returns factorial(4)
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.
Other Examples f(0) = 0; f(n) = n + f(n-1);
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
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.