Recursion Overview l Introduction to recursion and recursive

  • Slides: 10
Download presentation
Recursion Overview l Introduction to recursion and recursive methods l Simple popular recursive algorithms

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

Recursion l l l We have mentioned earlier that a method can call itself

Recursion l l l 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. l The code of a recursive method must be structured to handle both the base case and the recursive case. l Each call to the method sets up a new execution environment, with new parameters and local variables. l As always, when the method completes, control returns to the method that invoked it (which may be an earlier invocation of itself). 2

Recursion: The Factorial Function l The factocial function is defined as: » n! =

Recursion: The Factorial Function l The factocial function is defined as: » n! = n*(n-1)*(n-2)*…*1 import java. io. IOException; import Text. IO; class Factorial { static Text. IO input. Stream = new Text. IO(System. in); public static int factorial(int num) { if (num == 0) return 1; else return num*factorial(num-1); } public static void main (String[] args) throws IOException { int n, answer; System. out. println("Enter an integer: "); n = input. Stream. read. Int(); answer = factorial(n); System. out. println("The factorial of” + n + " is ” + answer); } } 3

Recursion: Sample Execution Trace l Factorial: A Sample Trace factorial (6) = =(6*Factorial(5)) =(6*(5*Factorial(4)))

Recursion: Sample Execution Trace l Factorial: A Sample Trace factorial (6) = =(6*Factorial(5)) =(6*(5*Factorial(4))) =(6*(5*(4*Factorial(3)))) =(6*(5*(4*(3*Factorial(2))))) =(6*(5*(4*(3*(2*Factorial(1)))))) =(6*(5*(4*(3*(2*1))))) =(6*(5*(4*(3*2)))) =(6*(5*(4*6))) =(6*(5*24)) =(6*120) =720 4

Recursion: sum. Of. Squares l sum. Of. Squares: Specified as: sum. Squares(m, n)=m^2+(m+1)^2+(m+2)^3+…+n^2 import

Recursion: sum. Of. Squares l sum. Of. Squares: Specified as: sum. Squares(m, n)=m^2+(m+1)^2+(m+2)^3+…+n^2 import java. io. *; import Text. IO; class Sum. Of. Squares { static Text. IO input. Stream = new Text. IO(System. in); static int sum. Squares(int from, int to) { if (from < to) return from*from + sum. Squares(from+1, to); else return from*from; } 5

Recursion: sum. Of. Squares (cont. ) l sum. Of. Squares (continued) public static void

Recursion: sum. Of. Squares (cont. ) l sum. Of. Squares (continued) public static void main (String[] args) throws IOException { int from, to, answer; System. out. println("Enter the smaller integer: "); from = input. Stream. read. Int(); System. out. println("Enter the bigger integer: "); to = input. Stream. read. Int(); answer = sum. Squares(from, to); System. out. println("Sum of Squares from "+ from + " to " +to+" is "+ answer); } } 6

Recursion: sum. Of. Squares Execution Trace l sum. Squares: A Sample Trace sum. Squares

Recursion: sum. Of. Squares Execution Trace l sum. Squares: A Sample Trace sum. Squares (5, 10) = =(25+sum. Squares(6, 10)) =(25+(36+sum. Squares(7, 10))) =(25+(36+(49+sum. Squares(8, 10)))) =(25+(36+(49+(64+sum. Squares(9, 10))) )) =(25+(36+(49+(64+(81+(sum. Squares(10 , 10))))))) =(25+(36+(49+(64+(81+100))))) =(25+(36+(49+(64+181)))) =(25+(36+(49+245))) =(25+(36+294)) =(25+330) =355 7

Recursion: The Fibonacci Function l The famous Fibonacci function is defined as fib 0

Recursion: The Fibonacci Function l The famous Fibonacci function is defined as fib 0 = 1 fib 1 = 1 fib n = fib(n-1)+fib(n-2), n>=2. import java. io. *; import Text. IO; class Fibonacci { static Text. IO input. Stream = new Text. IO(System. in); static int fibonacci (int num) { if (num == 0 || num == 1) return 1; else return (fibonacci(num-1) + fibonacci(num-2)); } 8

Recursion: The Fiboacci Function l Fibonacci public static void main (String[] args) throws IOException

Recursion: The Fiboacci Function l Fibonacci public static void main (String[] args) throws IOException { int n, answer; System. out. println("Enter an integer: "); n = input. Stream. read. Int(); answer = fibonacci(n); System. out. println("The "+n+ "th Fibonacci number is: "+ answer); } } 9

Recursion: Simple Popular Recursive Algorithms (cont. ) l Exercises: Write complete recursive programs for

Recursion: Simple Popular Recursive Algorithms (cont. ) l Exercises: Write complete recursive programs for the following algorithms 1 power(x, y) that implements x^y 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 Euclid’s algorithm of finding the greatest common divisor of m and n. Assume m and n to be positive integers. 3. is. Plaindrome() 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). 10