11/2/18 Motivations Chapter 18 Recursion Suppose you want to find all the files under a directory that contains a particular word. How do you solve this problem? There are several ways to solve this problem. An intuitive solution is to use CS1: Java Programming recursion by searching the files in the subdirectories recursively. Colorado State University Original slides by Daniel Liang Modified slides by Chris Wilcox Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 1 2 rights reserved. rights reserved. Motivations Objectives q To describe what a recursive method is and the benefits of using recursion H-trees, depicted in Figure 18.1, are used in a very large- (§18.1). scale integration (VLSI) design as a clock distribution q To develop recursive methods for recursive mathematical functions (§§18.2– network for routing timing signals to all parts of a chip 18.3). with equal propagation delays. How do you write a q To explain how recursive method calls are handled in a call stack (§§18.2–18.3). program to display H-trees? A good approach is to use q To solve problems using recursion (§18.4). recursion. q To use an overloaded helper method to derive a recursive method (§18.5). q To implement a selection sort using recursion (§18.5.1). q To implement a binary search using recursion (§18.5.2). q To get the directory size using recursion (§18.6). q To solve the Tower of Hanoi problem using recursion (§18.7). q To draw fractals using recursion (§18.8). q To discover the relationship and difference between recursion and iteration (§18.9). q To know tail-recursive methods and why they are desirable (§18.10). Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 3 4 rights reserved. rights reserved. animation Computing Factorial Computing Factorial factorial(0) = 1; factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(4) factorial(n) = n*factorial(n-1); n! = n * (n-1)! 0! = 1 Run ComputeFactorial Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 5 6 rights reserved. rights reserved. 1
11/2/18 animation animation Computing Factorial Computing Factorial factorial(0) = 1; factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(n) = n*factorial(n-1); factorial(4) = 4 * factorial(3) factorial(4) = 4 * factorial(3) = 4 * 3 * factorial(2) Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 7 8 rights reserved. rights reserved. animation animation Computing Factorial Computing Factorial factorial(0) = 1; factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(n) = n*factorial(n-1); factorial(4) = 4 * factorial(3) factorial(4) = 4 * factorial(3) = 4 * 3 * factorial(2) = 4 * 3 * factorial(2) = 4 * 3 * (2 * factorial(1)) = 4 * 3 * (2 * factorial(1)) = 4 * 3 * ( 2 * (1 * factorial(0))) Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 9 10 rights reserved. rights reserved. animation animation Computing Factorial Computing Factorial factorial(0) = 1; factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(n) = n*factorial(n-1); factorial(4) = 4 * factorial(3) factorial(4) = 4 * factorial(3) = 4 * 3 * factorial(2) = 4 * 3 * factorial(2) = 4 * 3 * (2 * factorial(1)) = 4 * 3 * (2 * factorial(1)) = 4 * 3 * ( 2 * (1 * factorial(0))) = 4 * 3 * ( 2 * (1 * factorial(0))) = 4 * 3 * ( 2 * ( 1 * 1))) = 4 * 3 * ( 2 * ( 1 * 1))) = 4 * 3 * ( 2 * 1) Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 11 12 rights reserved. rights reserved. 2
11/2/18 animation animation Computing Factorial Computing Factorial factorial(0) = 1; factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(n) = n*factorial(n-1); factorial(4) = 4 * factorial(3) factorial(4) = 4 * factorial(3) = 4 * 3 * factorial(2) = 4 * (3 * factorial(2)) = 4 * 3 * (2 * factorial(1)) = 4 * (3 * (2 * factorial(1))) = 4 * 3 * ( 2 * (1 * factorial(0))) = 4 * (3 * ( 2 * (1 * factorial(0)))) = 4 * 3 * ( 2 * ( 1 * 1))) = 4 * (3 * ( 2 * ( 1 * 1)))) = 4 * 3 * ( 2 * 1) = 4 * (3 * ( 2 * 1)) = 4 * 3 * 2 = 4 * (3 * 2) = 4 * (6) Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 13 14 rights reserved. rights reserved. animation animation Trace Recursive factorial Computing Factorial Executes factorial(4) factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(4) = 4 * factorial(3) factorial(4) = 4 * (3 * factorial(2)) Step 0: executes factorial(4) Step 9: return 24 = 4 * (3 * (2 * factorial(1))) return 4 * factorial(3) Step 1: executes factorial(3) Step 8: return 6 = 4 * (3 * ( 2 * (1 * factorial(0)))) return 3 * factorial(2) = 4 * (3 * ( 2 * ( 1 * 1)))) Step 2: executes factorial(2) Step 7: return 2 Stack return 2 * factorial(1) = 4 * (3 * ( 2 * 1)) Step 3: executes factorial(1) Step 6: return 1 = 4 * (3 * 2) return 1 * factorial(0) Step 4: executes factorial(0) Step 5: return 1 = 4 * (6) Space Required for factorial(4) return 1 Main method = 24 Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 15 16 rights reserved. rights reserved. animation animation Trace Recursive factorial Trace Recursive factorial factorial(4) factorial(4) Executes factorial(2) Step 0: executes factorial(4) Step 0: executes factorial(4) Step 9: return 24 Step 9: return 24 Executes factorial(3) return 4 * factorial(3) return 4 * factorial(3) Step 1: executes factorial(3) Step 1: executes factorial(3) Step 8: return 6 Step 8: return 6 return 3 * factorial(2) return 3 * factorial(2) Step 2: executes factorial(2) Step 2: executes factorial(2) Step 7: return 2 Step 7: return 2 Stack Stack return 2 * factorial(1) return 2 * factorial(1) Step 3: executes factorial(1) Step 3: executes factorial(1) Step 6: return 1 Step 6: return 1 Space Required return 1 * factorial(0) return 1 * factorial(0) for factorial(2) Space Required for factorial(3) Space Required Step 4: executes factorial(0) Step 4: executes factorial(0) for factorial(3) Step 5: return 1 Step 5: return 1 Space Required for factorial(4) Space Required for factorial(4) return 1 Main method return 1 Main method Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 17 18 rights reserved. rights reserved. 3
Recommend
More recommend