12.1.1.1. mystery(5)
?
Solution.
The output would be: 0 1 2 3 4 5 6. The 6 is printed before the recursion stops.
base case | recursion parameter |
computational overhead | recursive case |
head-and-tail algorithm | recursive definition |
iterative method | recursive method |
last-in-first-out (LIFO) | self-similarity |
method call stack | tail recursive |
JComboBox
component is used to represent a GUI drop-down menu.mystery(5)
?
mystery(100)
?
mystery(100)
would be 100.mystery2(5)
?
mystery(5)
would be: 5 4 3 2 1 0 -1 -2 ... In other words, this is an infinite recursion./** countDown(N) recursively prints a countdown
* beginning at N and ending at 1
* @param N >= 1
* Base case: N == 0
*/
void countDown(int N) {
if (N == 0) // Base case
System.out.println("blastoff");
else {
System.out.print(N + ", "); // Recursive case
countDown(N - 1);
}
} // countDown()
/** countDown(N) recursively prints a countdown
* beginning at N, counting every other number, 10 8 6 ...
* and ending at "blastoff"
* @param N >= 1
* Base case: N <= 0
*/
void countDown(int N) {
if (N <= 0) // Base case
System.out.println("blastoff");
else {
System.out.print(N + ", "); // Recursive case
countDown(N - 2 );
}
} // countDown()
int sum1toN(int N) {
if (N == 0)
return 0;
else
return N + sum1toN(N-1);
}
String addBlanks(String s) {
if (s.length() == 0)
return "";
else if (s.charAt(0) == ' ')
return ' ' + s.charAt(0) + addBlanks(s.substring(1));
else
return s.charAt(0) + addBlanks(s.substring(1));
}
N
games. printOutcomes(str, N)
will print all outcomes for the next N
games given that results for previous games are stored in the string named str
.public static void printOutcomes(String str, int N){
if (N = 1) { // Base case: win, lose, or draw one game
System.out.println(str + "W");
System.out.println(str + "L");
System.out.println(str + "D");
} else { // Recursive case
printOutcomes(str + "W", N - 1);
printOutcomes(str + "L", N - 1);
printOutcomes(str + "D", N - 1);
} //else
} // printOutcomes()
public static void main(String args[]) {
int numbers[] = {0, 2, 4, 6, 8, 10, 12, 14, 16, 18};
Searcher searcher = new Searcher();
for (int k = 0; k <= 20; k++) {
int result = searcher.search(numbers, k);
if (result != -1)
System.out.println(k + " found at " + result);
else
System.out.println(k + " is not in the array ");
} // for
} // main()
/** findMax (arr,N) returns the index of the largest
* value between arr[0] and arr[N], N >= 0.
* Pre: 0 <= N <= arr.length -1
* Post: arr[findMax()]>=arr[k] for k between 0 and N.
*/
private int findMax(int arr[], int N) {
int maxSoFar = 0;
for (int k = 0; k <= N; k++)
if (arr[k] > arr[maxSoFar])
maxSoFar = k;
return maxSoFar;
} // findMax()
/** sort(arr) sorts the int array, arr
* Pre: arr is not null
* Post: arr will be arranged so that arr[j] <= arr[k]
* for any j < k
*/
public void sort(int arr[]) {
selectionSort(arr, arr.length - 1); // Call the recursive method
}
private void drawBoxesIterative(Graphics g, int level, int locX, int locY, int side, int delta) {
for (int k = level; k >= 0; k--) {
g.drawRect(locX, locY, side, side); // Draw a square
locX += delta; // Calculate new location
locY += delta;
side -= 2 * delta; // Calculate new side length
}
} // drawBoxesIterative()
printReverse()
tail recursive?
printReverse()
method is not tail recursive because in that method the recursive call is not the last statement executed.countChar()
tail recursive?
countChar()
method is tail recursive. The recursive calls are not the last statements in the method definition. However, each of the recursive calls would be the last statement executed by the method.JComboBox
Example
private void drawBoxesRev(Graphics g, int level, int locX,
int locY, int side, int delta) {
g.drawRect(locX, locY, side, side );
if (level > 0) {
int dside = side * delta / 100; // Percent delta
int newLocX = locX + dside;
int newLocY = locY + dside;
drawBoxesRev(g, level - 1, newLocX, newLocY,
side - 2 * dside, delta);
}
} // drawBoxesRev()