OOP-Lecture 7 2013

advertisement
OOP-Lecture 7
2013
Recursion
Process of solving a problem by reducing it to smaller versions of itself
Recursive definition
 Definition in which a problem is expressed in terms of a smaller version of itself
 Has one or more base cases
Recursive algorithm
 Algorithm that finds the solution to a given problem by reducing the problem to
smaller versions of itself
 Has one or more base cases
 Implemented using recursive method

Recursive method
 Method that calls itself
Base case
 Case in recursive definition in which the solution is obtained directly
 Stops the recursion
Recursion or Iteration?
Two ways to solve particular problem

Iteration
 Recursion
Both iteration and recursion use a control statement
 Iteration uses a repetition statement
 Recursion uses a selection statement




Iteration and recursion both involve a termination test
Iteration terminates when the loop-continuation condition fails
Recursion terminates when a base case is reached
Recursion can be expensive in terms of processor time and memory space, but
usually provides a more intuitive solution
Types of recursive:
• Directly recursive: a method that calls itself
• Indirectly recursive: a method that calls another method and eventually results
in the original method call
1
Lecturer: Hawraa Sh.
OOP-Lecture 7
2013
• Tail recursive method: recursive method in which the last statement executed
is the recursive call
• Infinite recursion: the case where every recursive call results in another
recursive call
Recursive method
 Logically, you can think of a recursive method having unlimited copies of itself
 Every recursive call has its own
• Code
• Set of parameters
• Set of local variables
After completing a recursive call
 Control goes back to the calling environment
 Recursive call must execute completely before control goes back to previous
call
 Execution in previous call begins from point immediately following recursive
call.
Designing Recursive Methods
•
•
•
•
Understand problem requirements
Determine limiting conditions
Identify base cases
Provide direct solution to each base case
Examples about recursion:
1- Summation
public int sum (int num)
{
int result;
if (num == 1)
result = 1;
else
result = num + sum (num-1);
return result;
}
2
Lecturer: Hawraa Sh.
OOP-Lecture 7
2013
2- Factorial
0!
n!
3!
2!
1!
0!
1!
2!
3!
=
=
=
=
=
=
=
=
=
1 (By Definition!)
n x (n – 1) ! If n > 0
3 x 2!
2 x 1!
1 x 0!
1 (Base Case!)
1 x 0! = 1 x 1 = 1
2 x 1! = 2 x 1 = 2
3 x 2! = 3 x 2 = 6
public static int fact(int num) {
if (num = = 0)
return 1;
else
return num * fact(num – 1);
}
3
Lecturer: Hawraa Sh.
OOP-Lecture 7
4
2013
Lecturer: Hawraa Sh.
OOP-Lecture 7
2013
3- Recursive Fibonacci
public static int rFibNum(int a, int b, int n) {
if (n == 1)
return a;
else if (n == 2)
return b;
else
return rFibNum(a, b, n -1) + rFibNum(a, b, n - 2);
}
5
Lecturer: Hawraa Sh.
OOP-Lecture 7
2013
4- Convert Decimal to binary
public static void decToBin(int num, int base) {
if (num > 0) {
decToBin(num / base, base);
System.out.print(num % base);
}
}
6
Lecturer: Hawraa Sh.
Download