Recursion - PowerPoint PPT Presentation

About This Presentation
Title:

Recursion

Description:

... always get 'closer' to the base case from one invocation to another. ... new TextIO(System.in); public static int factorial(int num) { if (num == 0) return 1; ... – PowerPoint PPT presentation

Number of Views:15
Avg rating:3.0/5.0
Slides: 11
Provided by: facultyK
Category:
Tags: recursion | use

less

Transcript and Presenter's Notes

Title: Recursion


1
Recursion
  • Overview
  • Introduction to recursion and recursive methods
  • Simple popular recursive algorithms
  • Writing recursive methods
  • Preview Parameter passing

2
Recursion
  • We have mentioned earlier that a method can call
    itself directly or indirectly using an
    intervening method.
  • A recursive method is a method that calls itself.
  • A well-defined recursive method has
  • A base case
  • A recursive step which must always get closer
    to the base case from one invocation to another.
  • The code of a recursive method must be structured
    to handle both the base case and the recursive
    case.
  • Each call to the method sets up a new execution
    environment, with new parameters and local
    variables.
  • As always, when the method completes, control
    returns to the method that invoked it (which may
    be an earlier invocation of itself).

3
Recursion The Factorial Function
  • The factocial function is defined as
  • n! n(n-1)(n-2)1
  • import java.io.IOException
  • import TextIO
  • class Factorial
  • static TextIO inputStream
  • new TextIO(System.in)
  • public static int factorial(int num)
  • if (num 0) return 1
  • else return numfactorial(num-1)
  • public static void main (String args) throws
    IOException
  • int n, answer
  • System.out.println("Enter an integer")
  • n inputStream.readInt()
  • answer factorial(n)

4
Recursion Sample Execution Trace
  • Factorial A Sample Trace
  • factorial (6)
  • (6Factorial(5))
  • (6(5Factorial(4)))
  • (6(5(4Factorial(3))))
  • (6(5(4(3Factorial(2)))))
  • (6(5(4(3(2Factorial(1))))))
  • (6(5(4(3(21)))))
  • (6(5(4(32))))
  • (6(5(46)))
  • (6(524))
  • (6120)
  • 720

5
Recursion sumOfSquares
  • sumOfSquares Specified as
  • sumSquares(m,n)m2(m1)2(m2)3n2
  • import java.io.
  • import TextIO
  • class SumOfSquares
  • static TextIO inputStream
  • new TextIO(System.in)
  • static int sumSquares(int from,
  • int to)
  • if (from lt to)
  • return fromfrom
  • sumSquares(from1,to)
  • else return fromfrom

6
Recursion sumOfSquares (cont.)
  • sumOfSquares (continued)
  • public static void main (String args) throws
    IOException
  • int from, to, answer
  • System.out.println("Enter the smaller
    integer")
  • from inputStream.readInt()
    System.out.println("Enter the bigger integer")
  • to inputStream.readInt()
  • answer sumSquares(from,to)
  • System.out.println("Sum of Squares from "
    from " to " to" is " answer)

7
Recursion sumOfSquares Execution Trace
  • sumSquares A Sample Trace
  • sumSquares (5,10)
  • (25sumSquares(6,10))
  • (25(36sumSquares(7,10)))
    (25(36(49sumSquares(8,10))))
  • (25(36(49(64sumSquares(9,10)))))
  • (25(36(49(64(81(sumSquares(10,10)))))))
  • (25(36(49(64(81100)))))
  • (25(36(49(64181))))
  • (25(36(49245)))
  • (25(36294))
  • (25330)
  • 355

8
Recursion The Fibonacci Function
  • The famous Fibonacci function is defined as
  • fib 0 1
  • fib 1 1
  • fib n fib(n-1)fib(n-2), ngt2.
  • import java.io.
  • import TextIO
  • class Fibonacci
  • static TextIO inputStream new
    TextIO(System.in)
  • static int fibonacci (int num)
  • if (num 0 num 1)
  • return 1
  • else return (fibonacci(num-1)
    fibonacci(num-2))

9
Recursion The Fiboacci Function
  • Fibonacci
  • public static void main (String args) throws
    IOException
  • int n, answer
  • System.out.println("Enter an integer")
  • n inputStream.readInt()
  • answer fibonacci(n)
  • System.out.println("The "n "th Fibonacci
    number is " answer)

10
Recursion Simple Popular Recursive Algorithms
(cont.)
  • Exercises Write complete recursive programs
    for the following algorithms
  • 1 power(x,y) that implements xy using repeated
    additions and without using multiplication.
    Assume x to be a floating point value and y to be
    a nonnegative integer.
  • 2 gcd(m,n) that implements the Euclids algorithm
    of finding the greatest common divisor of m and
    n. Assume m and n to be positive integers.
  • 3. isPlaindrome() which given a string prints an
    informative error message saying whether or not
    the given string is a palindrome (reads the same
    when read from left to right or from right to
    left).
Write a Comment
User Comments (0)
About PowerShow.com