18 Recursion
18 Recursion
Objectives
❑ To describe what a recursive method is and the benefits of using recursion
(§18.1).
❑ To develop recursive methods for recursive mathematical functions (§§18.2–
18.3).
❑ To explain how recursive method calls are handled in a call stack (§§18.2–18.3).
❑ To solve problems using recursion (§18.4).
❑ To use an overloaded helper method to derive a recursive method (§18.5).
❑ To implement a selection sort using recursion (§18.5.1).
❑ To implement a binary search using recursion (§18.5.2).
❑ To get the directory size using recursion (§18.6).
❑ To solve the Tower of Hanoi problem using recursion (§18.7).
❑ To draw fractals using recursion (§18.8).
❑ To discover the relationship and difference between recursion and iteration
(§18.9).
❑ To know tail-recursive methods and why they are desirable (§18.10).
2
Computing Factorial
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
n! = n * (n-1)!
ComputeFactorial
3
animation
Computing Factorial
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
factorial(4)
4
animation
Computing Factorial
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
factorial(4) = 4 * factorial(3)
5
animation
Computing Factorial
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
factorial(4) = 4 * factorial(3)
= 4 * 3 * factorial(2)
6
animation
Computing Factorial
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
factorial(4) = 4 * factorial(3)
= 4 * 3 * factorial(2)
= 4 * 3 * (2 * factorial(1))
7
animation
Computing Factorial
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
factorial(4) = 4 * factorial(3)
= 4 * 3 * factorial(2)
= 4 * 3 * (2 * factorial(1))
= 4 * 3 * ( 2 * (1 * factorial(0)))
8
animation
Computing Factorial
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
factorial(4) = 4 * factorial(3)
= 4 * 3 * factorial(2)
= 4 * 3 * (2 * factorial(1))
= 4 * 3 * ( 2 * (1 * factorial(0)))
= 4 * 3 * ( 2 * ( 1 * 1)))
9
animation
Computing Factorial
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
factorial(4) = 4 * factorial(3)
= 4 * 3 * factorial(2)
= 4 * 3 * (2 * factorial(1))
= 4 * 3 * ( 2 * (1 * factorial(0)))
= 4 * 3 * ( 2 * ( 1 * 1)))
= 4 * 3 * ( 2 * 1)
10
animation
Computing Factorial
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
factorial(4) = 4 * factorial(3)
= 4 * 3 * factorial(2)
= 4 * 3 * (2 * factorial(1))
= 4 * 3 * ( 2 * (1 * factorial(0)))
= 4 * 3 * ( 2 * ( 1 * 1)))
= 4 * 3 * ( 2 * 1)
=4*3*2
11
animation
Computing Factorial
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
factorial(4) = 4 * factorial(3)
= 4 * 3 * factorial(2)
= 4 * 3 * (2 * factorial(1))
= 4 * 3 * ( 2 * (1 * factorial(0)))
= 4 * 3 * ( 2 * ( 1 * 1)))
= 4 * 3 * ( 2 * 1)
=4*3*2
=4*6
12
animation
Computing Factorial
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
factorial(4) = 4 * factorial(3)
= 4 * 3 * factorial(2)
= 4 * 3 * (2 * factorial(1))
= 4 * 3 * ( 2 * (1 * factorial(0)))
= 4 * 3 * ( 2 * ( 1 * 1)))
= 4 * 3 * ( 2 * 1)
=4*3*2
=4*6
= 24
13
animation
14
animation
Executes factorial(3)
15
animation
Executes factorial(2)
16
animation
Executes factorial(1)
17
animation
Executes factorial(0)
18
animation
returns 1
19
animation
returns factorial(0)
20
animation
returns factorial(1)
21
animation
returns factorial(2)
22
animation
returns factorial(3)
23
animation
24
factorial(4) Stack Trace
25
Other Examples
f(0) = 0;
f(n) = n + f(n-1);
26
Fibonacci Numbers
Fibonacci series: 0 1 1 2 3 5 8 13 21 34 55 89…
indices: 0 1 2 3 4 5 6 7 8 9 10 11
fib(0) = 0;
fib(1) = 1;
fib(index) = fib(index -1) + fib(index -2); index >=2
ComputeFibonacci
27
Fibonnaci Numbers, cont.
28
Characteristics of Recursion
All recursive methods have the following characteristics:
– One or more base cases (the simplest case) are used to stop
recursion.
– Every recursive call reduces the original problem, bringing it
increasingly closer to a base case until it becomes that case.
29
Problem Solving Using Recursion
Let us consider a simple problem of printing a message for
n times. You can break the problem into two subproblems:
one is to print the message one time and the other is to print
the message for n-1 times. The second problem is the same
as the original problem with a smaller size. The base case
for the problem is n==0. You can solve this problem using
recursion as follows:
nPrintln(“Welcome“, 5);
public static void nPrintln(String message, int times) {
if (times >= 1) {
System.out.println(message);
nPrintln(message, times - 1);
} // The base case is times == 0
}
30
Recursive Selection Sort
1. Find the smallest number in the list and swaps it
with the first number.
2. Ignore the first number and sort the remaining
smaller list recursively.
RecursiveSelectionSort
31
Recursive Binary Search
1. Case 1: If the key is less than the middle element,
recursively search the key in the first half of the array.
2. Case 2: If the key is equal to the middle element, the
search ends with a match.
3. Case 3: If the key is greater than the middle element,
recursively search the key in the second half of the
array.
RecursiveBinarySearch
32
BigInteger
BigInteger class is used for mathematical operation which involves very
big integer calculations that are outside the limit of all available primitive
data types.
●add(BigInteger val)
Returns a BigInteger whose value is (this + val)
●subtract(BigInteger val)
Returns a BigInteger whose value is (this - val)
●multiply(BigInteger val)
Returns a BigInteger whose value is (this * val)
●divide(BigInteger val)
Returns a BigInteger whose value is (this / val).
●compareTo(BigInteger val)
Compares this BigInteger with the specified BigInteger
●valueOf(long val)
Returns a BigInteger whose value is equal to that of the specified long.
33