160 likes | 299 Views
Chapter 11 Recursion. Recursive Methods. A recursive method is a method that calls itself directly or indirectly. A recursive method has two major steps: recursive step in which the method calls itself base step which specifies a case with a known solution
E N D
Chapter 11 Recursion University Of Ha’il
Recursive Methods • A recursive method is a method that calls itself directly or indirectly. • A recursive method has two major steps: • recursive step in which the method calls itself • base step which specifies a case with a known solution • The method should select one of two steps based on a criteria: • Example: • recursive step: fact(n) = n * fact(n-1) • base step: fact(0) = 1 University Of Ha’il
Constructing Recursion • To construct a recursive algorithm you have to find out: • Recursive step • Base step • A selection structure is then used to determine which step to take. University Of Ha’il
General Algorithm • if (stopping condition) then • solve simple problem (base) • else • use recursion to solve smaller problem • combine solutions from smaller problem University Of Ha’il
Recursive Methods 0! = 1 (By Definition!) n! = n x (n – 1) ! If n > 0 3! = 3 x 2! 2! = 2 x 1! 1! = 1 x 0! 0! = 1 (Base Case!) 1! = 1 x 0! = 1 x 1 = 1 2! = 2 x 1! = 2 x 1 = 2 3! = 3 x 2! = 3 x 2 = 6
Recursive Methods • recursive step: fact(n) = n * fact(n-1) • base step: fact(0) = 1 • fact(4) = 4 * fact(3) = 4 * (3 * fact(2)) = 4 * (3 * (2 * fact(1))) = 4 * (3 * (2 * (1 * fact(0)))) = 4 * (3 * (2 * (1 * 1))) = 4 * (3 * (2 * 1)) = 4 * (3 * 2) = 4 * 6 = 24 University Of Ha’il
Recursive Factorial Method public static int fact(int num){ if (num = = 0) return 1; else return num * fact(num – 1);}
Fibonacci numbers publicintfib(intn) {if(n <= 1) {returnn; } else {returnfib(n - 1) + fib(n - 2); }} University Of Ha’il
Convert from decimal to binary • This method converts an integer number to its binary equivalent. • Base step: • dec2bin(n) = n if n is 0 or 1 • Recursive step: • dec2bin(n) = dec2bin (n/2) , (n mod 2) • Algorithm dec2bin(n): • If n < 2 • Print n • else • dec2bin(n / 2) • Print n mod 2 University Of Ha’il
Example (Recursion) • class Method { • public static void dec2bin(int n){ • if( n < 2 ) • System.out.print( n ); • else { • dec2bin( n / 2 ); • System.out.print( n % 2 ); • } • } • } • class Dec2Bin{ • public static void main(String[] arg){ inti=10; • dec2bin(i); • } • } Output: 1010 University Of Ha’il
Example (iterative) • public static void dec2bin(int n){ • String binary =""; • while ( n >= 1 ) • { • binary = n%2 + binary; • n /= 2; • } • System.out.print(binary); • } University Of Ha’il
Recursion or Iteration? • Two ways to solve particular problem: • Iteration • Recursion • Iterative control structures use looping to repeat a set of statements. • Tradeoffs between two options: • Sometimes recursive solution is easier. • Recursive solution is often slower.
Exercise • Write a recursive method to find the greatest common divisor (GCD) of two integer n and m. • Write a recursive method to find Xn given the double X and the integer n. • Consider a Boolean array b filled with Boolean values. Write a recursive method booleanallTrue() that returns true if all values are true and returns false otherwise. University Of Ha’il