Lawfare Group → Knowledge Institute → Lawfare Academy
← CS 201 — Data Structures, Algorithms and Systems
Weeks 9–11
Recursion, trees and computational state
Learning objectives
- Explain and trace the mechanisms in recursion, trees and computational state.
- Apply the method to the worked example and changed-condition exercise.
- Identify assumptions, failure cases and the evidence needed to justify a result.
Original on-site study sequence
Study this preserved lesson with the expanded original reader linked in Further reading. It supplies mathematical preparation, advanced representations, complete original and changed models, the Cedar cumulative investigation and a 24-point rubric. Use the resource-safe laboratory edition for optional local execution. All older publisher and textbook links are optional references. Current whole-course internal review remains pending.
Recursion solves a problem by invoking the same procedure on smaller cases. It needs a base case, a reduction toward that case and a way to combine results. A recursive call has its own local state; returning from it resumes the caller. Confusing caller and callee state is a common source of incorrect traces.
A binary tree can represent hierarchical structure, but not every binary tree is balanced and not every tree is a search tree. Search-tree ordering places smaller keys on one side and larger keys on the other under a defined duplicate policy. A chain-shaped tree can need linear lookup time. Balanced trees maintain additional invariants to limit height, paying maintenance costs on changes.
Memoization stores answers for repeated subproblems. It can turn exponential repeated work into linear or polynomial work when subproblems overlap, but consumes memory and requires an appropriate cache key. Caching a result that depends on hidden mutable state can return a stale answer. Dynamic programming is a problem-structure technique rather than a command to make every function recursive. For untrusted or very deep structures, an explicit stack can avoid runtime recursion limits.
Worked reasoning The function counts ordered sequences of one-step and two-step moves that cover n steps. There is one way to cover zero steps: take no moves. Negative subproblems contribute zero. For four steps the counts build as 1, 1, 2, 3, 5. Memoization avoids recomputing each smaller n repeatedly. This code is for small instructional inputs; very large n needs an iterative design and consideration of integer size.
Extended teaching and laboratory
A recurrence defines both its base cases and the relation among subproblems. For stair counts, ways(0)=1 represents the empty sequence; negative remaining distance contributes zero. This is a counting convention tied to the problem, not a universal rule that every recursive function returns one at zero.
Memoization removes repeated subproblem evaluation but not every resource cost. A recursive memoized function can retain a deep call stack. Bottom-up iteration can keep only the preceding two values for this recurrence. As values grow, arithmetic cost and integer storage can matter even when the number of recurrence steps is linear.
Worked problem Count ordered sequences of one- and two-step moves covering five stairs. Compare naive recursion with a bottom-up state pair.
Step-by-step reasoning 1. Starting with ways(0)=1 and ways(1)=1 gives ways(2)=2, ways(3)=3, ways(4)=5 and ways(5)=8.
- Naive recursion recomputes smaller states through multiple branches. Memoization evaluates each remaining-distance state once, while retaining cached results.
- An iterative pair holds the preceding two counts and updates them through five. Its invariant identifies those counts at the start of each iteration.
- The result counts orders separately: 1+2 and 2+1 are different sequences. An unordered partition problem would require a different recurrence.
Apply the idea
Count ordered sequences of one- and two-step moves covering five stairs. Compare naive recursion with a bottom-up state pair.
Moves may now be one or three stairs. State the recurrence and compute the count for five. Identify the required base convention.
Read the worked reasoning
Use f(n)=f(n−1)+f(n−3), f(0)=1 and f(n)=0 for n<0. The sequence for n=0 through 5 is 1,1,1,2,3,4. The four sequences for five are all ones and the three placements of one 3 among two 1s. Reusing the one-or-two recurrence would solve the wrong specification even if the memoization were perfect.
Check understanding
Why must a memoization key capture every relevant input?
- Because recursion always requires a cache
Feedback
Many recursive algorithms have no repeated subproblems.
- Otherwise different subproblems can incorrectly share one cached result
Feedback
A cache can only reuse results for equivalent subproblems.
- Because caches prove every result correct
Feedback
Incorrect computations remain incorrect when cached.
Further reading
Study the original native lesson and expanded on-site reader. Work through the relevant mathematical and representation mechanisms, then the complete Cedar original/changed investigation and rubric. The retained workbook and weighted-path model provide further original practice. All practice and source references are optional; no external chapter or exercise is required.
- Original on-site mathematical preparation, representation mechanisms and Cedar cumulative investigation
- Resource-safe complete implementation laboratories
- Oscar Levin — Discrete Mathematics: 2.5 and 3.2: induction, strong induction and proof methods
- 6 . Binary Trees
- 6 . 1 BinaryTree: A Basic Binary Tree
- 6 . 2 BinarySearchTree: An Unbalanced Binary Search Tree
- 6 . 3 Discussion and Exercises
- 7 . Random Binary Search Trees
- 7 . 1 Random Binary Search Trees
- 7 . 2 Treap: A Randomized Binary Search Tree
- 7 . 3 Discussion and Exercises
- 8 . Scapegoat Trees
- 8 . 1 ScapegoatTree: A Binary Search Tree with Partial Rebuilding
- 8 . 2 Discussion and Exercises
- 9 . Red-Black Trees
- 9 . 1 2-4 Trees
- 9 . 2 RedBlackTree: A Simulated 2-4 Tree
- 9 . 3 Summary
- 9 . 4 Discussion and Exercises
- 10 . Heaps
- 10 . 1 BinaryHeap: An Implicit Binary Tree
- 10 . 2 MeldableHeap: A Randomized Meldable Heap
- 10 . 3 Discussion and Exercises