Upload
marian-west
View
215
Download
0
Embed Size (px)
DESCRIPTION
Programming With Java ICS201 University Of Ha’il3 Constructing Recursion o To construct a recursive algorithm you have to find out: 1.Recursive step 2.Base step o A selection structure is then used to determine which step to take.
Citation preview
Prog
ram
min
g W
ith Ja
vaIC
S201
University Of Ha’il 1
Chapter 11
Recursion
Prog
ram
min
g W
ith Ja
vaIC
S201
University Of Ha’il 2
Recursive Methodso A recursive method is a method that calls itself directly
or indirectly.o A recursive method has two major steps:
1. recursive step in which the method calls itself2.base step which specifies a case with a known
solution
o 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
Prog
ram
min
g W
ith Ja
vaIC
S201
University Of Ha’il 3
Constructing Recursion
o To construct a recursive algorithm you have to find out:
1.Recursive step
2.Base step
o A selection structure is then used to determine which
step to take.
Prog
ram
min
g W
ith Ja
vaIC
S201
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
Prog
ram
min
g W
ith Ja
vaIC
S201
Recursive Methods0! = 1 (By Definition!)n! = n x (n – 1) ! If n > 03! = 3 x 2!2! = 2 x 1!1! = 1 x 0!
0! = 1 (Base Case!)
1! = 1 x 0! = 1 x 1 = 12! = 2 x 1! = 2 x 1 = 23! = 3 x 2! = 3 x 2 = 6
Prog
ram
min
g W
ith Ja
vaIC
S201
University Of Ha’il 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
Prog
ram
min
g W
ith Ja
vaIC
S201
7
Recursive Factorial Method
Prog
ram
min
g W
ith Ja
vaIC
S201
8
Recursive Factorial Method
public static int fact(int num){ if (num = = 0) return 1; else return num * fact(num – 1);}
Prog
ram
min
g W
ith Ja
vaIC
S201
Fibonacci numbers
n 0 1 2 3 4 5 6 7 8 9 10 11Fib(n) 0 1 1 2 3 5 8 13 21 34 55 89
University Of Ha’il 9
public int fib(int n) { if(n <= 1) { return n; } else { return fib(n - 1) + fib(n - 2); }}
Prog
ram
min
g W
ith Ja
vaIC
S201
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 nelse
dec2bin(n / 2) Print n mod 2
Prog
ram
min
g W
ith Ja
vaIC
S201
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){ int i=10;
dec2bin(i);}
}
Output:1010
Prog
ram
min
g W
ith Ja
vaIC
S201
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);}
Prog
ram
min
g W
ith Ja
vaIC
S201
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.
Prog
ram
min
g W
ith Ja
vaIC
S201
University Of Ha’il 14
Exercise
1. Write a recursive method to find the greatest common
divisor (GCD) of two integer n and m.
2. Write a recursive method to find Xn given the double X
and the integer n.
3. Consider a Boolean array b filled with Boolean values.
Write a recursive method boolean allTrue() that returns
true if all values are true and returns false otherwise.