Recursion - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Recursion

Description:

Palindromes ... example, Madame I'm Adam is a palindrome. Our method signature will look like this: public boolean palindrome(String s) We will not worry about ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 18
Provided by: facul62
Category:

less

Transcript and Presenter's Notes

Title: Recursion


1
Recursion
  • A method is recursive if it makes a call to
    itself.
  • For example
  • public void printDown (int n)
  • System.out.println( n)
  • 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.
  • Lets 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.
  • For example
  • Suppose we want to write a method power(x,n),
    which raises x to the nth power. And suppose we
    dont know how to compute Xn , but we know that
    Xn X Xn-1 .
  • We have start of a recursive solution!!

4
  • Xn X Xn-1
  • We might try writing the power method
  • public int power (int x, int n)
  • 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.
  • There is only one problem

5
  • Lets 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
  • power(3,2) returns 3 power (3,1) need to
    call power(3,1)

  • to find result
  • power(3,1) returns 3 power (3,0) need to
    call power(3,0)

  • to find result
  • power(3,0) returns 3 power (3,-1) need to
    call power(3,1)

  • 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 X1
    X.
  • public int power (int x, int n)
  • if ( n 1)
  • return x
  • else
  • return x power(x, n-1)

7
  • Lets 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
  • power(3,2) returns 3 power (3,1) need to
    call power(3,1)

  • to find result

    power(3,1) returns 3
  • So, that means
  • power(3,2) return 3 3 (or 9)
  • 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.
  • To avoid infinite recursion we need to think of
    something we know.
  • We know 0! 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)
  • return 1
  • else
  • 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.
  • 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.
  • print n
  • call printDown(n-1) to print the rest of the
    values

11
  • public void printDown( int n)
  • if ( n 1)
  • System.out.println(1)
  • else
  • System.out.println(n)
  • 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.
  • 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.
  • call printRev(rest of the string) to print
    the rest of
  • the string in reverse
  • print the first character

13
  • public void printRev( String s)
  • if ( s.length() 1)
  • System.out.print(s)
  • else
  • printRev(s.substring(1))
  • 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 Im Adam is a palindrome.
  • Our method signature will look like this
  • 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??
  • if the string has one character it is 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
  • then
  • if the substring in between those chars is
    a
  • palindrome
  • the string is a palindrome
  • else
  • it string is not a palindrome

17
Palindromes continued
  • public boolean palindrome(String s)
  • if (s.length() lt 1)
  • return true
  • else
  • char first s.charAt(0)
  • char last s.charAt(s.length() -1)
  • if (first last)
  • return palindrome(s.substring(1,
    s.length() -1)
  • else
  • return false
Write a Comment
User Comments (0)
About PowerShow.com