Glossary#
| Term | Plain meaning | Chapter |
|---|---|---|
| recursion / recursive method | a method solving a problem by calling itself on a smaller input | 1 |
| base case / recursive case / progress | directly answered input / self-call on smaller input / moving toward the base | 1 |
| winding / unwinding | frames piling up / returning in reverse | 2 |
| leap of faith | trust the smaller call's answer (induction) | 3 |
| fast exponentiation | xⁿ by halving n: O(log n) | 3 |
| recursive structure / tree / root / children | self-containing data / its shape / top / contained nodes | 4 |
| depth-first traversal | go as deep as possible before siblings | 4 |
| pre-order / post-order | work before / after recursing into children | 4 |
| linear / halving / tree / divide-and-conquer / structural recursion | the recursion shapes | 5 |
| helper method | private recursive version carrying extra parameters | 5 |
| accumulator | a parameter collecting results as you go | 5 |
| tail call / tail-call optimisation | the call is the last action / turning it into a loop (Java doesn't) | 5 |
| backtracking | choose → explore → unchoose over a decision tree | 6 |
| decision tree | the tree of all choice sequences | 6 |
| overlapping subproblems / memoization | the same subproblem recurring / caching its answer | 7 |
| recursion depth | maximum frames alive at once — the space cost | 8 |
| explicit stack | an ArrayDeque replacing the call stack | 8 |
Cheat Sheet#
The template
static R solve(Input x) {
if (isSmallest(x)) return directAnswer(x); // base case(s) FIRST
R smaller = solve(shrink(x)); // leap of faith
return combine(x, smaller);
}Backtracking template
static void explore(State s, List<Result> out) {
if (complete(s)) {
out.add(copyOf(s));
return;
} // record a COPY
for (Choice c : choices(s)) {
apply(s, c); // choose
explore(s, out); // explore
undo(s, c); // unchoose
}
}Memoization template
static long f(int n, long[] memo) {
if (n <= 1) return base;
if (memo[n] != 0) return memo[n]; // or a HashMap for non-integer / 2-D states
return (memo[n] = combine(f(n - 1, memo), f(n - 2, memo)));
}Costs
calls ≈ branching^depth · time = calls × work per call · space = max depth (+ memo/results)
shrink by 1 → O(n) · halve → O(log n) · two calls shrink by 1 → O(2ⁿ) (memoize!) · subsets 2ⁿ · permutations n!
Java: no tail-call optimisation · ~10⁴+ linear depth is risky → explicit ArrayDeque stackChecklist before running
base case covers ALL smallest inputs? · every call makes progress? · every return value used?
backtracking undoes every change? · results stored as copies? · recursing on indexes, not copies?
repeated subproblems → memoize? · depth could be huge → loop + stack?Try This Yourself#
Trace by hand, then by machine. Trace
sum({3, 1, 2}, 0)on paper, frame by frame. Then add depth-indented prints (Ch 2) and check your trace.Write five small recursions, each using the three questions: count the digits of a number; check if an array is sorted; find the maximum of an array; compute
gcd(a, b)usinggcd(a, b) = gcd(b, a % b)withgcd(a, 0) = a; flatten a nested list of folders into a list of note names.Backtracking warm-up. Modify
subsetsto print only subsets whose letters are in alphabetical order and have exactly 2 letters. Then write all permutations of "aab" without duplicates (hint: sort, and skip a choice equal to the previous one at the same level).From exponential to linear. Write naïve
stairs(n)where the student may take 1, 2 or 3 steps. Count its calls for n = 25, then memoize it and count again. Then rewrite it bottom-up with three variables.Make it iterative. Rewrite
printTreeusing an explicit stack so it prints folders in the same order as the recursive version. (Hint: push subfolders in reverse.)