Public Static Void Mr(Int N)

Public Static Void Mr(Int N)

Recursion CSE160, Computer Science A: Honors Stony Brook University http://www.cs.stonybrook.edu/~cse160 1 Motivation Suppose you want to find all the files under a directory that contains a particular word. The directory contains subdirectories that also contain subdirectories, and so on. The solution is to use recursion by searching the files in the subdirectories recursively. 2 (c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook) Motivation The Eight Queens puzzle is to place eight queens on a chessboard such that no two queens are on the same row, same column, or same diagonal: We solve this problem using recursion: we place the 8th queen after we placed 7 queens on the chessboard. we place the 7th queen after we placed 6 queens on the chessboard. 3 … (c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook) Computing Factorial n! = 1 * 2 * 3 * 4 * 5 * ... * (n-1) * n (n-1)! = 1 * 2 * 3 * 4 * 5 * ... * (n-1) So: n! = (n-1)! * n Recursive definition: factorial(0) = 1 factorial(n) = n*factorial(n-1); for n > 0 4 (c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook) import java.util.Scanner; public class ComputeFactorial { public static void main(String[] args) { // Create a Scanner Scanner input = new Scanner(System.in); System.out.print("Enter a non-negative integer: "); int n = input.nextInt(); // Display factorial System.out.println("Factorial of "+n+" is "+factorial(n)); } /** Return the factorial for a specified number */ public static int factorial(int n) { if (n == 0) // Base case return 1; else return n * factorial(n - 1); // Recursive call } } 5 (c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook) Computing Factorial factorial(3) = factorial(0) = 1; factorial(n) = n*factorial(n-1); 6 (c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook) Computing Factorial factorial(3) = 3 * factorial(2) factorial(0) = 1; factorial(n) = n*factorial(n-1); 7 (c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook) Computing Factorial factorial(3) = 3 * factorial(2) factorial(0) = 1; = 3 * (2 * factorial(1)) factorial(n) = n*factorial(n-1); 8 (c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook) Computing Factorial factorial(3) = 3 * factorial(2) factorial(0) = 1; = 3 * (2 * factorial(1)) factorial(n) = n*factorial(n-1); = 3 * ( 2 * (1 * factorial(0))) 9 (c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook) Computing Factorial factorial(3) = 3 * factorial(2) factorial(0) = 1; = 3 * (2 * factorial(1)) factorial(n) = n*factorial(n-1); = 3 * ( 2 * (1 * factorial(0))) = 3 * ( 2 * ( 1 * 1))) 10 (c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook) Computing Factorial factorial(3) = 3 * factorial(2) factorial(0) = 1; = 3 * (2 * factorial(1)) factorial(n) = n*factorial(n-1); = 3 * ( 2 * (1 * factorial(0))) = 3 * ( 2 * ( 1 * 1))) = 3 * ( 2 * 1) 11 (c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook) Computing Factorial factorial(3) = 3 * factorial(2) factorial(0) = 1; = 3 * (2 * factorial(1)) factorial(n) = n*factorial(n-1); = 3 * ( 2 * (1 * factorial(0))) = 3 * ( 2 * ( 1 * 1))) = 3 * ( 2 * 1) = 3 * 2 12 (c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook) Computing Factorial factorial(3) = 3 * factorial(2) factorial(0) = 1; = 3 * (2 * factorial(1)) factorial(n) = n*factorial(n-1); = 3 * ( 2 * (1 * factorial(0))) = 3 * ( 2 * ( 1 * 1))) = 3 * ( 2 * 1) = 3 * 2 = 6 13 (c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook) Trace Recursive factorial Executes factorial(4) factorial(4) Step 0: executes factorial(4) Step 9: return 24 return 4 * factorial(3) Stack Space Required Step 1: executes factorial(3) 5 Step 8: return 6 Space Required for factorial(0) for factorial(2) 4 Space Required Space Required return 3 * factorial(2) for factorial(1) for factorial(1) Step 2: executes factorial(2) 3 Space Required Space Required Step 7: return 2 for factorial(2) for factorial(2) return 2 * factorial(1) Space Required Space Required Space Required for factorial(3) for factorial(3) for factorial(3) Step 3: executes factorial(1) Step 6: return 1 Space Required Space Required for factorial(4) for factorial(4) return 1 * factorial(0) Main method Step 4: executes factorial(0) Step 5: return 1 return 1 14 (c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook) Trace Recursive factorial factorial(4) Step 0: executes factorial(4) Step 9: return 24 Executes factorial(3) return 4 * factorial(3) Step 1: executes factorial(3) Step 8: return 6 Stack return 3 * factorial(2) 5 Space Required Step 2: executes factorial(2) for factorial(2) Space Required Step 7: return 2 4 for factorial(1) return 2 * factorial(1) Space Required 3 for factorial(2) Step 3: executes factorial(1) Step 6: return 1 Space Required Space Required for factorial(3) for factorial(3) return 1 * factorial(0) Space Required Step 4: executes factorial(0) Step 5: return 1 Space Required for factorial(4) for factorial(4) return 1 Main method 15 (c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook) Trace Recursive factorial factorial(4) Executes factorial(2) Step 0: executes factorial(4) Step 9: return 24 return 4 * factorial(3) Step 1: executes factorial(3) Step 8: return 6 Stack 5 return 3 * factorial(2) Space Required for factorial(2) Step 2: executes factorial(2) 4 Space Required Step 7: return 2 for factorial(1) return 2 * factorial(1) 3 Space Required for factorial(2) Step 3: executes factorial(1) Step 6: return 1 Space Required Space Required return 1 * factorial(0) Space Required for factorial(3) for factorial(3) for factorial(3) Step 4: executes factorial(0) Space Required Step 5: return 1 Space Required for factorial(4) return 1 for factorial(4) Main method 16 (c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook) Trace Recursive factorial factorial(4) Executes factorial(1) Step 0: executes factorial(4) Step 9: return 24 return 4 * factorial(3) Step 1: executes factorial(3) Stack Step 8: return 6 5 Space Required return 3 * factorial(2) for factorial(2) 4 Space Required Step 2: executes factorial(2) for factorial(1) Step 7: return 2 3 Space Required return 2 * factorial(1) Space Required for factorial(2) for factorial(2) Space Required Space Required Step 3: executes factorial(1) Step 6: return 1 Space Required for factorial(3) for factorial(3) for factorial(3) return 1 * factorial(0) Space Required Space Required for factorial(4) Step 4: executes factorial(0) Step 5: return 1 for factorial(4) return 1 Main method 17 (c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook) Trace Recursive factorial factorial(4) Executes factorial(0) Step 0: executes factorial(4) Step 9: return 24 return 4 * factorial(3) Stack Step 1: executes factorial(3) 5 Step 8: return 6 Space Required for factorial(2) return 3 * factorial(2) Space Required 4 for factorial(1) Step 2: executes factorial(2) Space Required Step 7: return 2 for factorial(1) Space Required 3 return 2 * factorial(1) Space Required for factorial(2) for factorial(2) Step 3: executes factorial(1) Space Required Space Required Step 6: return 1 Space Required for factorial(3) for factorial(3) return 1 * factorial(0) for factorial(3) Space Required Step 4: executes factorial(0) Space Required for factorial(4) Step 5: return 1 for factorial(4) return 1 Main method 18 (c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook) Trace Recursive factorial factorial(4) returns 1 Step 0: executes factorial(4) Step 9: return 24 return 4 * factorial(3) Step 1: executes factorial(3) Step 8: return 6 Stack 5 Space Required return 3 * factorial(2) Space Required for factorial(2) for factorial(0) Step 2: executes factorial(2) 4 Space Required Step 7: return 2 Space Required for factorial(1) return 2 * factorial(1) for factorial(1) 3 Space Required for factorial(2) Step 3: executes factorial(1) Space Required Step 6: return 1 for factorial(2) Space Required Space Required return 1 * factorial(0) Space Required for factorial(3) for factorial(3) Step 4: executes factorial(0) for factorial(3) Space Required Step 5: return 1 Space Required for factorial(4) return 1 for factorial(4) Main method 19 (c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook) Trace Recursive factorial factorial(4) returns factorial(0) Step 0: executes factorial(4) Step 9: return 24 return 4 * factorial(3) Stack Step 1: executes factorial(3) 5 Step 8: return 6 Space Required for factorial(2) return 3 * factorial(2) Space Required 4 for factorial(1) Step 2: executes factorial(2) Space Required Step 7: return 2 for factorial(1) Space Required 3 return 2 * factorial(1) Space Required for factorial(2) for factorial(2) Step 3: executes factorial(1) Space Required Space Required Step 6: return 1 Space Required for factorial(3) for factorial(3) return 1 * factorial(0) for factorial(3) Space Required Step 4: executes factorial(0) Space Required for factorial(4) Step 5: return 1 for factorial(4) return 1 Main method 20 (c) Pearson Education, Inc. & Paul Fodor (CS Stony Brook) Trace Recursive factorial factorial(4) returns factorial(1) Step 0: executes factorial(4) Step 9: return 24 return 4 * factorial(3) Step 1: executes factorial(3) Step 8: return 6 Stack 5 Space Required return 3 * factorial(2) for factorial(2) Space Required Step 2: executes factorial(2) 4 Step 7: return 2 for factorial(1) return 2 * factorial(1) 3 Space Required Space Required for factorial(2) Step 3: executes factorial(1) Step 6: return 1 for factorial(2) Space Required Space Required for factorial(3) for factorial(3) return 1 * factorial(0) Space Required for factorial(3) Space Required Step 4: executes factorial(0) Step 5: return 1 Space Required for factorial(4) return 1 for factorial(4) Main method 21 (c) Pearson Education, Inc.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    54 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us