Recursion: every key term you need (+ practice quiz)
54 flashcard terms for AP Computer Science A Unit 10, written to match the course framework. Read them here, drill them as flashcards, or take the 44-question quiz. Free, no account needed.
Process strings character-by-character. Example: reverse string recursively or check palindrome.
Backtracking
Explore all possibilities; abandon path if constraint violated. Example: Sudoku solver, maze solver.
Unit 10 Summary
Recursion: function calls itself; needs base case and recursive case. Useful for divide-and-conquer, tree traversal, backtracking.
Base Case
The input for which the method returns without recursing. Every recursive method needs at least one; missing or unreachable base cases cause StackOverflowError.
Recursive Case
The branch that calls the method on a smaller or simpler input, moving toward the base case. Progress must be guaranteed for termination.
Call Stack Frames
Each call gets its own copy of parameters and local variables. Work after the recursive call resumes when the deeper call returns, in reverse order of calls.
Tracing Recursion
Write each call with its arguments, resolve the deepest one first, then substitute results upward. For print-then-recurse vs recurse-then-print, output order differs.
Recursion vs Iteration
Any recursion in the AP subset can be rewritten as a loop; the exam asks you to read and trace recursion, not write it from scratch (except in reasoning about equivalence).
Factorial
fact(n) = n * fact(n - 1) with fact(0) = 1. Depth n; fact(5) makes 6 calls including the base case.
fib(n) = fib(n-1) + fib(n-2) with fib(0)=0, fib(1)=1. Two recursive calls per non-base call causes exponential call counts; fib(5) makes 15 calls.
Sum of Digits Recursively
sum(n) = n % 10 + sum(n / 10) with sum(0) = 0. Mirrors the iterative digit loop.
String Recursion
Typical shape: base case on empty or one-char string; recursive case handles the first char and recurses on substring(1). Reversal appends the first char after the recursive result.
Recursion on Arrays with an Index Parameter
Helper methods carry the current index: sum(arr, i) = arr[i] + sum(arr, i + 1) with base i == arr.length returning 0.
Binary Search Preconditions
Requires sorted data. Compare the middle element to the target; discard the half that cannot contain it. Works iteratively or recursively.
Binary Search Efficiency
Each comparison halves the remaining range, so at most about log2(n) + 1 comparisons: 1,000 elements need at most 10; 1,000,000 need at most 20.
Binary Search Middle Index
mid = (low + high) / 2 uses integer division. After comparing, set low = mid + 1 or high = mid - 1; forgetting the +1/-1 can loop forever.
Merge Sort Structure
Split the array in half, recursively sort each half, then merge two sorted halves in linear time. Recursion depth is about log2(n).
Merge Step
Walk two sorted sequences with two indices, always copying the smaller current element into a temporary array, then copy any leftovers.
Merge Sort Efficiency
About n × log2(n) work: log2(n) levels, each doing n element copies. Much faster than selection or insertion sort for large n, at the cost of extra memory.
Sort Comparison Table
Selection: always ~n^2/2 comparisons, ~n swaps. Insertion: n-1 comparisons best case, ~n^2/2 worst. Merge: ~n log n always.
Counting Recursive Calls
Count the initial call plus every call it triggers. For a method that recurses on n - 1 down to 0, calls = n + 1; for halving recursions, roughly log2(n) + 1.
Infinite Recursion Symptoms
StackOverflowError at runtime. Causes: base case never reached (recursing on n + 1 instead of n - 1), or base case placed after the recursive call.
Recursion with Multiple Base Cases
Methods like fib or a palindrome check need two base cases (length 0 and 1). Missing one lets an index go negative or a substring call throw.
Progress Toward the Base Case
Every recursive call must strictly reduce the problem in a way that reaches a base case, or the stack will exhaust and throw StackOverflowError.
Multiple Base Cases
Fibonacci needs two, at n = 0 and n = 1, because a single base case would leave one branch recursing forever.
Stack Frame Contents
Each pending call stores its own parameters, local variables, and return address, which is why deep recursion consumes memory proportional to its depth.
Work After the Recursive Call
Code placed after the call runs on the way back up, which is what makes printing after recursion produce reversed output.