Recursion and Decomposition
-
Choosing a Base Case for Recursion
A base case produces an answer directly for an input that needs no further recursive work.
-
Making Each Recursive Call Smaller
A recursive decomposition has to make progress toward a base case under a well-founded measure.
-
Tracing a Recursive Call Stack
Each active invocation keeps its own local values and the work to resume when a nested call returns.
-
Tail Calls and Accumulated State
A tail call is made when its result can become the current function's result without further computation.
-
Counting Nodes in a Tree Recursively
A tree can be counted by adding each node to the sizes of all its child subtrees. An empty subtree contributes zero.
-
Measuring Tree Height Recursively
Tree height describes the longest root-to-leaf path. Decide whether the measure counts edges or nodes.
-
Tracing Preorder and Postorder Tree Visits
The position of a node's action relative to its children determines whether the traversal processes parents before or after descendants.
-
Collecting the Leaves of a Tree
To collect leaves in order, emit the current node if it has no children. Otherwise traverse its children in their declared order.
-
Divide, Solve, and Combine
Divide and conquer splits a problem into smaller instances, solves them, and combines the answers using a rule tied to the goal.
-
Splitting a Half-Open Interval Recursively
Two half-open subintervals can cover a segment exactly once and give clear stopping conditions for recursive processing.
-
Binary Search by Hand
Binary search repeatedly uses sorted order to discard positions that cannot contain the desired value or boundary.
-
Merging Two Sorted Sequences
Merging selects the smaller current head from two sorted sources until both sources are exhausted.
-
Counting Recursive Calls
The time used by recursion depends on the number of invocations and the cost inside each one. Stack depth measures only the longest active path.
-
Recognizing Overlapping Recursive Subproblems
Recursive calls overlap when different branches request the same subproblem. Reusing its answer can avoid repeating an entire subtree of work.
-
Memoizing a Recursive Function
Memoization saves a finished answer under the state that produced it and reuses that answer for later requests for the same state.
-
Evaluating a Recurrence from the Bottom Up
A bottom-up evaluation records base values first and computes larger states in an order respecting all dependencies.
-
Replacing Recursion with an Explicit Worklist
An explicit worklist stores pending tasks in ordinary data structures. Its ordering governs which task the traversal performs next.
-
Handling Deep Recursive Inputs
Even a terminating recursion can reach a depth beyond the runtime's available stack or a configured recursion limit.
-
Reading a Small Recursive Grammar
A recursive grammar can describe arbitrarily deep nested structures using a limited set of production rules.
-
Walking Nested Records and Lists
Recursive traversal can visit values in nested records by handling containers and leaf values according to their shape.
-
Backtracking and Choice Points
Backtracking tries a choice, explores its consequences, and restores the state needed to try another choice.
-
Generating Subsets by Inclusion Choices
For each distinct item, a recursive generator can branch into including it and leaving it out. The base case records the completed choices.
-
Generating Permutations by Choosing the Next Item
To enumerate input permutations, choose each possible first item and recursively enumerate arrangements of what remains.
-
Pruning an Impossible Branch
Pruning skips descendants of a search state when a justified bound establishes that none can satisfy the goal.
-
Processing a List as First Item and Remainder
A list computation can separate the first item from the rest and combine the first contribution with the result for the remainder.
-
Processing Digits with Quotient and Remainder
Repeated division by the base can separate an integer into its least significant digit and the digits that precede it.
-
Structural Induction on Lists
Structural induction proves a property for empty input and shows how extending an arbitrary shorter list preserves that property.
-
Induction on Tree Shapes
A structural proof for finite trees shows the claim for the smallest tree and assumes it for each child when proving it for a larger node.
-
Reasoning About Mutual Recursion
Mutually recursive functions call one another. Their combined transitions must reach a base case for every allowed input.
-
Balancing Recursive Subproblem Sizes
The shape of recursive splits affects maximum depth and the amount of work. A split that removes one item can be much deeper than halving the size.