Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 205 Programming II Lecture 8 Recursion.

Similar presentations


Presentation on theme: "CSC 205 Programming II Lecture 8 Recursion."— Presentation transcript:

1 CSC 205 Programming II Lecture 8 Recursion

2 Recursive vs. Iterative
Divide and conquer Decomposition: Complex systems  subsystems  objects Procedural Task  repeat manageable steps a number of times (iterative)  small tasks which can be solved in a similar manor (recursive)

3 An Example Due to the nature of the task, you should select from the two alternatives which best fit your need An example: Use a book Scenario I – read your textbook Suggested solution: read from the first page to the last which are assigned by the teacher Scenario II – look up a dictionary for a word Suggested solution: use the alphabetic order to find it as soon as you can

4 The Factorial of n Definition (the iterative version)
Factorial(n) = n * (n-1) * (n-2) * … * 1, n>0 Factorial(0) = 1 Consider an iterative solution to the problem of computing the factorial of n A loop is needed int factorial(int n) { int f = 1; for (int i=2; i<=n; i++) f *= i; return f; }

5 The Recursive Approach
n! is based on (n-1)! We can solve a reduced task, (n-1)!, in the same way as we solve n! The task can reduce to a very manageable size: 1! = 1 or 0! = 1 A recursive approach would look like Reduce task size Solve the entire task Solve a base-case directly

6 A Recursive Solution Recursive definition of factorial
0!  1! = 1 * 0! = 1 1!  2! = 2 * 1! = 2 … … n!  n * (n-1)! Problem solved! A recursive method int fact(int n) { if (n==0) return 1; else return fact(n-1); } //(A)

7 How Does It Work? The sequence of computation can be depicted as below
Number and order of recursive calls Parameter(s) passed in each recursive call Result returned caller return 3*fact(2) 3*2 return 2*fact(1) 2*1 return 1*fact(0) 1*1 return 1

8 Four Questions to Ask How can you define the problem in terms a smaller problem of the same type? How does each recursive call diminish the size of the problem? What instance of the problem can serve as the base case? As the problem diminishes, can you reach the base case?

9 The Trace Box A trace box is needed for each recursive call to store its local environment, including The values of parameters passed in The method’s local variables A placeholder the value returned from calls made by the current box The value to be returned n = 3 A: fact(n-1) = ? return ?

10 Tracing the Computation – I

11 Tracing the Computation – II

12 Tracing the Computation – III

13 A Recursive void Method
Writing a string backward: CAT  TAC A recursive strategy Strip away the first/last letter to reduce problem size Length of remaining reduced by one per call The base case: reverse an empty string An empty string is reachable

14 Strip away the Last Letter
The following recursive method will write a string backward, without changing the string passed in. void writeBackward(String s, int size) { if (size > 0) { System.out.print(s.substring(size-1,size)); writeBackward(s, size-1); } //base case is implied: // doing nothing when (size == 0)

15 Strip away the First Letter
The following recursive method will write a string backward, without changing the string passed in. void writeBackward2(String s) { if (s.length() > 0) { System.out.print(s.charAt(0)); writeBackward(s.substring(1)); } //base case is implied: // doing nothing when (size == 0)


Download ppt "CSC 205 Programming II Lecture 8 Recursion."

Similar presentations


Ads by Google