Post on 30-Dec-2015
description
transcript
1Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
L09 (Chapter 19) Recursion 1
Chapter 9 Inheritance and Polymorphism
Chapter 18 Binary I/O
Chapter 17 Exceptions and Assertions
Chapter 6 Arrays
Chapter 19 Recursion
2Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
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).
3Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
Computing Factorialfactorial(0) = 1;
factorial(n) = n*factorial(n-1);
ComputeFactorialComputeFactorial Run
4Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
Computing Factorial
factorial(3)
animation
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
5Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
Computing Factorial
factorial(3) = 3 * factorial(2)
animation
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
6Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
Computing Factorial
factorial(3) = 3 * factorial(2)
= 3 * (2 * factorial(1))
animation
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
7Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
Computing Factorial
factorial(3) = 3 * factorial(2)
= 3 * (2 * factorial(1))
= 3 * ( 2 * (1 * factorial(0)))
animation
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
8Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
Computing Factorial
factorial(3) = 3 * factorial(2)
= 3 * (2 * factorial(1))
= 3 * ( 2 * (1 * factorial(0)))
= 3 * ( 2 * ( 1 * 1)))
animation
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
9Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
Computing Factorial
factorial(3) = 3 * factorial(2)
= 3 * (2 * factorial(1))
= 3 * ( 2 * (1 * factorial(0)))
= 3 * ( 2 * ( 1 * 1)))
= 3 * ( 2 * 1)
animation
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
10Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
Computing Factorial
factorial(3) = 3 * factorial(2)
= 3 * (2 * factorial(1))
= 3 * ( 2 * (1 * factorial(0)))
= 3 * ( 2 * ( 1 * 1)))
= 3 * ( 2 * 1)
= 3 * 2
animation
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
11Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
Computing Factorial
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
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
12Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
Trace Recursive factorialanimation
return 1
factorial(4)
return 4 * factorial(3)
return 3 * factorial(2)
return 2 * factorial(1)
return 1 * factorial(0)
Step 9: return 24 Step 0: executes factorial(4)
Step 1: executes factorial(3)
Step 2: executes factorial(2)
Step 3: executes factorial(1)
Step 5: return 1
Step 6: return 1
Step 7: return 2
Step 8: return 6
Step 4: executes factorial(0)
Executes factorial(4)
Main method
Stack
13Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
Trace Recursive factorialanimation
return 1
factorial(4)
return 4 * factorial(3)
return 3 * factorial(2)
return 2 * factorial(1)
return 1 * factorial(0)
Step 9: return 24 Step 0: executes factorial(4)
Step 1: executes factorial(3)
Step 2: executes factorial(2)
Step 3: executes factorial(1)
Step 5: return 1
Step 6: return 1
Step 7: return 2
Step 8: return 6
Step 4: executes factorial(0)
Executes factorial(3)
Main method
Space Required for factorial(4)
Stack
14Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
Trace Recursive factorialanimation
return 1
factorial(4)
return 4 * factorial(3)
return 3 * factorial(2)
return 2 * factorial(1)
return 1 * factorial(0)
Step 9: return 24 Step 0: executes factorial(4)
Step 1: executes factorial(3)
Step 2: executes factorial(2)
Step 3: executes factorial(1)
Step 5: return 1
Step 6: return 1
Step 7: return 2
Step 8: return 6
Step 4: executes factorial(0)
Executes factorial(2)
Main method
Space Required for factorial(4)
Space Required for factorial(3)
Stack
15Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
Trace Recursive factorialanimation
return 1
factorial(4)
return 4 * factorial(3)
return 3 * factorial(2)
return 2 * factorial(1)
return 1 * factorial(0)
Step 9: return 24 Step 0: executes factorial(4)
Step 1: executes factorial(3)
Step 2: executes factorial(2)
Step 3: executes factorial(1)
Step 5: return 1
Step 6: return 1
Step 7: return 2
Step 8: return 6
Step 4: executes factorial(0)
Executes factorial(1)
Main method
Space Required for factorial(4)
Space Required for factorial(3)
Space Required for factorial(2)
Stack
16Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
Trace Recursive factorialanimation
return 1
factorial(4)
return 4 * factorial(3)
return 3 * factorial(2)
return 2 * factorial(1)
return 1 * factorial(0)
Step 9: return 24 Step 0: executes factorial(4)
Step 1: executes factorial(3)
Step 2: executes factorial(2)
Step 3: executes factorial(1)
Step 5: return 1
Step 6: return 1
Step 7: return 2
Step 8: return 6
Step 4: executes factorial(0)
Executes factorial(0)
Main method
Space Required for factorial(4)
Space Required for factorial(3)
Space Required for factorial(2)
Space Required for factorial(1)
Stack
17Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
Trace Recursive factorialanimation
return 1
factorial(4)
return 4 * factorial(3)
return 3 * factorial(2)
return 2 * factorial(1)
return 1 * factorial(0)
Step 9: return 24 Step 0: executes factorial(4)
Step 1: executes factorial(3)
Step 2: executes factorial(2)
Step 3: executes factorial(1)
Step 5: return 1
Step 6: return 1
Step 7: return 2
Step 8: return 6
Step 4: executes factorial(0)
returns 1
Main method
Space Required for factorial(4)
Space Required for factorial(3)
Space Required for factorial(2)
Space Required for factorial(1)
Space Required for factorial(0)
Stack
18Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
Trace Recursive factorialanimation
return 1
factorial(4)
return 4 * factorial(3)
return 3 * factorial(2)
return 2 * factorial(1)
return 1 * factorial(0)
Step 9: return 24 Step 0: executes factorial(4)
Step 1: executes factorial(3)
Step 2: executes factorial(2)
Step 3: executes factorial(1)
Step 5: return 1
Step 6: return 1
Step 7: return 2
Step 8: return 6
Step 4: executes factorial(0)
returns factorial(0)
Main method
Space Required for factorial(4)
Space Required for factorial(3)
Space Required for factorial(2)
Space Required for factorial(1)
Stack
19Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
Trace Recursive factorialanimation
return 1
factorial(4)
return 4 * factorial(3)
return 3 * factorial(2)
return 2 * factorial(1)
return 1 * factorial(0)
Step 9: return 24 Step 0: executes factorial(4)
Step 1: executes factorial(3)
Step 2: executes factorial(2)
Step 3: executes factorial(1)
Step 5: return 1
Step 6: return 1
Step 7: return 2
Step 8: return 6
Step 4: executes factorial(0)
returns factorial(1)
Main method
Space Required for factorial(4)
Space Required for factorial(3)
Space Required for factorial(2)
Stack
20Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
Trace Recursive factorialanimation
return 1
factorial(4)
return 4 * factorial(3)
return 3 * factorial(2)
return 2 * factorial(1)
return 1 * factorial(0)
Step 9: return 24 Step 0: executes factorial(4)
Step 1: executes factorial(3)
Step 2: executes factorial(2)
Step 3: executes factorial(1)
Step 5: return 1
Step 6: return 1
Step 7: return 2
Step 8: return 6
Step 4: executes factorial(0)
returns factorial(2)
Main method
Space Required for factorial(4)
Space Required for factorial(3)
Stack
21Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
Trace Recursive factorialanimation
return 1
factorial(4)
return 4 * factorial(3)
return 3 * factorial(2)
return 2 * factorial(1)
return 1 * factorial(0)
Step 9: return 24 Step 0: executes factorial(4)
Step 1: executes factorial(3)
Step 2: executes factorial(2)
Step 3: executes factorial(1)
Step 5: return 1
Step 6: return 1
Step 7: return 2
Step 8: return 6
Step 4: executes factorial(0)
returns factorial(3)
Main method
Space Required for factorial(4)
Stack
22Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
Trace Recursive factorialanimation
return 1
factorial(4)
return 4 * factorial(3)
return 3 * factorial(2)
return 2 * factorial(1)
return 1 * factorial(0)
Step 9: return 24 Step 0: executes factorial(4)
Step 1: executes factorial(3)
Step 2: executes factorial(2)
Step 3: executes factorial(1)
Step 5: return 1
Step 6: return 1
Step 7: return 2
Step 8: return 6
Step 4: executes factorial(0)
returns factorial(4)
Main method
Stack
23Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
factorial(4) Stack Trace
Space Requiredfor factorial(4)
1 Space Requiredfor factorial(4)
2 Space Requiredfor factorial(3)
Space Requiredfor factorial(4)
3
Space Requiredfor factorial(3)
Space Requiredfor factorial(2)
Space Requiredfor factorial(4)
4
Space Requiredfor factorial(3)
Space Requiredfor factorial(2)
Space Requiredfor factorial(1)
Space Requiredfor factorial(4)
5
Space Requiredfor factorial(3)
Space Requiredfor factorial(2)
Space Requiredfor factorial(1)
Space Requiredfor factorial(0)
Space Requiredfor factorial(4)
6
Space Requiredfor factorial(3)
Space Requiredfor factorial(2)
Space Requiredfor factorial(1)
Space Requiredfor factorial(4)
7
Space Requiredfor factorial(3)
Space Requiredfor factorial(2)
Space Requiredfor factorial(4)
8 Space Requiredfor factorial(3)
Space Requiredfor factorial(4)
9
24Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
See Recursive Calls in JBuilder Debugger
You can see the chain of recursive method invocations in the stack view of the JBuilder debugger.
Stack view
Invoke factorial(0)
Invoke factorial(1)
Invoke factorial(2)
Invoke factorial(3)
Invoke factorial(4)
Invoke main method
25Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
Other Examples
f(0) = 0;
f(n) = n + f(n-1);
26Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
Fibonacci NumbersFinonacci 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
ComputeFibonacciComputeFibonacci Run
27Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
Fibonnaci Numbers, cont.
return fib(3) + fib(2)
return fib(2) + fib(1)
return fib(1) + fib(0)
return 1
return fib(1) + fib(0)
return 0
return 1
return 1 return 0
1: call fib(3)
2: call fib(2)
3: call fib(1)
4: return fib(1)
7: return fib(2)
5: call fib(0)
6: return fib(0)
8: call fib(1)
9: return fib(1)
10: return fib(3) 11: call fib(2)
16: return fib(2)
12: call fib(1) 13: return fib(1) 14: return fib(0)
15: return fib(0)
fib(4) 0: call fib(4) 17: return fib(4)
28Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6
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.