1 / 14

Chapter 11 Recursion

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

thais
Download Presentation

Chapter 11 Recursion

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 11 Recursion University Of Ha’il

  2. 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

  3. 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

  4. 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

  5. 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

  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

  7. Recursive Factorial Method

  8. Recursive Factorial Method public static int fact(int num){ if (num = = 0) return 1; else return num * fact(num – 1);}

  9. Fibonacci numbers publicintfib(intn) {if(n <= 1) {returnn;    } else {returnfib(n - 1) + fib(n - 2);    }} University Of Ha’il

  10. 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

  11. 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

  12. 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

  13. 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.

  14. 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

More Related