Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursion A method is recursive if it makes a call to itself. A method is recursive if it makes a call to itself. For example: For example: public void.

Similar presentations


Presentation on theme: "Recursion A method is recursive if it makes a call to itself. A method is recursive if it makes a call to itself. For example: For example: public void."— Presentation transcript:

1 Recursion A method is recursive if it makes a call to itself. A method is recursive if it makes a call to itself. For example: For example: public void printDown (int n){ public void printDown (int n){ System.out.println( n); System.out.println( n); printDown(n-1); printDown(n-1);} Is a recursive method. In fact, it ‘recurses’ infinately….

2 A method which recurses infinately is not of much use. There are many problems which can be solved recursively and, in fact, be coded more simply by the use of recursion. Let’s first learn to PROPERLY use recursion on familiar applications…

3 Designing a recursive solution The idea behind using recursion to solve a problem is to simplify the solution by defining the problem in terms of itself. defining the problem in terms of itself. For example: For example: Suppose we want to write a method power(x,n), which raises x to the nth power. And suppose we don’t know how to compute X n, but we know that X n = X * X n-1. Suppose we want to write a method power(x,n), which raises x to the nth power. And suppose we don’t know how to compute X n, but we know that X n = X * X n-1. We have start of a recursive solution!! We have start of a recursive solution!!

4 X n = X * X n-1 We might try writing the power method: We might try writing the power method: public int power (int x, int n) { public int power (int x, int n) { return x * power(x, n-1); return x * power(x, n-1); } This is logical, because IF we had a power method, the return statement is correct …. And we ARE writing a power method. This is logical, because IF we had a power method, the return statement is correct …. And we ARE writing a power method. There is only one problem …………… There is only one problem ……………

5 Let’s try our method by tracing a call to power(3,3) power(3,3) returns 3 * power (3,2) need to call power(3,2) to find result to find result power(3,2) returns 3 * power (3,1) need to call power(3,1) to find result to find result power(3,1) returns 3 * power (3,0) need to call power(3,0) to find result to find result power(3,0) returns 3 * power (3,-1) need to call power(3,1) to find result to find result We could do this all day …. Infinite recursion again!!

6 A method which uses recursion should ALWAYS have at least one path which does not recurse in order to avoid infinite recursion. Where do we come up with that??? We must find ONE case of the problem in which we know the solution. For example, we KNOW X 1 = X. We must find ONE case of the problem in which we know the solution. For example, we KNOW X 1 = X. public int power (int x, int n) { if ( n == 1) if ( n == 1) return x; return x; else else return x * power(x, n-1); return x * power(x, n-1); }

7 Let’s try our method by tracing a call to power(3,3) power(3,3) returns 3 * power (3,2) need to call power(3,2) to find result to find result power(3,2) returns 3 * power (3,1) need to call power(3,1) power(3,2) returns 3 * power (3,1) need to call power(3,1) to find result power(3,1) returns 3 to find result power(3,1) returns 3 So, that means power(3,2) return 3 * 3 (or 9) power(3,2) return 3 * 3 (or 9) power(3,3) returns 3 * 9 (or 27) power(3,3) returns 3 * 9 (or 27)

8 Another recursive solution Suppose we want to write a method factorial(n) which computes n! (mathematical symbol defined as n* n- 1* n-2 *.. * 1, and 0! = 1. Suppose we want to write a method factorial(n) which computes n! (mathematical symbol defined as n* n- 1* n-2 *.. * 1, and 0! = 1. To avoid infinite recursion we need to think of something we know. To avoid infinite recursion we need to think of something we know. We know: 0! = 1 We know: 0! = 1 Now, define the unknown part of the solution in terms of the solution itself. N! = N * (N-1)! Now, define the unknown part of the solution in terms of the solution itself. N! = N * (N-1)!

9 public int factorial ( int n) { if ( n == 0) if ( n == 0) return 1; return 1; else else return n * factorial( n-1); return n * factorial( n-1); } Trace it for factorial(4) and check if it works!! The solution should be 24.

10 Another recursive solution Suppose we want to write a method which printDown(n) which prints the values from n downto 1 … without using a loop. Suppose we want to write a method which printDown(n) which prints the values from n downto 1 … without using a loop. We know: If n is 1, we just want to print 1 Define the unknown part of the solution in terms of the method itself. Define the unknown part of the solution in terms of the method itself. print n print n call printDown(n-1) to print the rest of the values call printDown(n-1) to print the rest of the values

11 public void printDown( int n) { if ( n == 1) if ( n == 1) System.out.println(1); System.out.println(1); else{ else{ System.out.println(n); System.out.println(n); printDown(n-1); printDown(n-1); } Try a call to printDown(5). What is the maximum number of calls to printDown that were active at once?? I counted 5.

12 Another recursive solution Suppose we want to write a method named printRev(s) which prints the characters in the String s in reverse … without using a loop. Suppose we want to write a method named printRev(s) which prints the characters in the String s in reverse … without using a loop. We know: If s has a length of 1, we just print s Define the unknown part of the solution in terms of the method itself. Define the unknown part of the solution in terms of the method itself. call printRev(rest of the string) to print the rest of call printRev(rest of the string) to print the rest of the string in reverse the string in reverse print the first character print the first character

13 public void printRev( String s) { if ( s.length() == 1) if ( s.length() == 1) System.out.print(s); System.out.print(s); else{ else{ printRev(s.substring(1)); printRev(s.substring(1)); System.out.print(s.charAt(0)); System.out.print(s.charAt(0)); }} Try a call to printRev(“java”). What is the maximum number of calls to printRev that were active at once?? I counted 4.

14 Clearly, these solutions we have been looking at can be much more simply written using a loop. Additionally, the effect of a method call on execution time makes these solutions highly inefficient …. These problems were not best solved using recursion. But, there ARE problems that are much more simply solved, often at no cost to efficiency, using recursion.

15 Palindromes Design a method that will determine if a String is a palindrome (reads the same forward and backward). For example, Madame I’m Adam is a palindrome. Our method signature will look like this: public boolean palindrome(String s) public boolean palindrome(String s) We will not worry about punctuation for now … assume that the String has been scanned and all punctuation removed, and letters are all lower case.

16 Palindromes continued What do we know?? What do we know?? if the string has one character it is a palindrome if the string has one character it is a palindrome if the string is empty it is implicitly a palindrome if the string is empty it is implicitly a palindrome What if we Had a method palindrome …. if the first and last character are the same if the first and last character are the same then then if the substring in between those chars is a if the substring in between those chars is a palindrome palindrome the string is a palindrome the string is a palindrome else else it string is not a palindrome it string is not a palindrome

17 Palindromes continued public boolean palindrome(String s){ if (s.length() <= 1) if (s.length() <= 1) return true; return true; else{ else{ char first = s.charAt(0); char first = s.charAt(0); char last = s.charAt(s.length() -1); char last = s.charAt(s.length() -1); if (first == last) if (first == last) return palindrome(s.substring(1, s.length() -1); return palindrome(s.substring(1, s.length() -1); else else return false; return false; }}


Download ppt "Recursion A method is recursive if it makes a call to itself. A method is recursive if it makes a call to itself. For example: For example: public void."

Similar presentations


Ads by Google