All chapters[•]Data StructuresChapter 04
Data Structures · Chapter 4

RecursionData Structures · Chapter 4

A recursive function solves a problem by solving a smaller copy of the same problem. Four lines of Fibonacci make almost three million calls for fib(30); binary search finds one name among a million in at most twenty looks; merge sort and quicksort turn "sort everything" into "sort two halves". This chapter gives you the picture that explains all of it, the recursion tree, and the habit of reading the cost of a recursive function off the tree: the number of nodes is the time, the depth is the stack space.

Instructors: Boxi Yu · Yintong Huo · Python and Java versions (switch at the top) · Every program here has been run, and every output shown is the real output. · All chapters

How big is the call tree of fib(n)?

The recursion tree of fib(n) for n = 3…7 grows node by node; a counter shows the calls so far and the final count, and the depth is marked on the side.

Why this matters

about 100–130 minutes (4 videos of 7–9 minutes plus the exercises).

Cost lives in the tree, not in the code

A recursive function can be four lines long and still make billions of calls; counting the nodes of its recursion tree (and its depth) tells you the time and the stack space.

Recurrences turn a tree into a formula

T(n) = T(n/2) + 1 is binary search, T(n) = 2T(n/2) + n is merge sort; unrolling a recurrence is the same as summing the levels of the tree.

The same idea sorts faster, if the split is balanced

Merge sort splits evenly by construction, quicksort only by luck; the tree shows why a random pivot protects it.

5 parts

Four videos with their notes, then the summary and practice. Read them in order.

01

The call stack and recursion trees

What does the computer remember while a recursive function waits for its own call to return? A stack of frames. We draw every call as a node of a tree, count the nodes of the factorial chain and of the Fibonacci tree, see why the naive Fibonacci is exponential, and fix it by remembering answers (memoization).

call stackframerecursion treeexponential growthmemoizationbase case
Open →
02

Binary search and recurrences

How many looks does it take to find one name in a sorted phone book of a million names? At most twenty. We write the cost of binary search as a recurrence, solve it by unrolling, check it against real counts, and read recurrences as trees: levels times work per level.

binary searchrecurrenceunrollinglevels × work per levellog npass indexes, not slices
Open →
03

Merge sort

Sorting a million items by comparing every pair takes about 500 billion comparisons; merge sort needs about 20 million. We merge two sorted lists with two fingers, split a list in half and sort each half recursively, and read the cost off the tree: n per level, log₂ n levels.

divide and conquermergen log nstableextra memoryrecursion depth log n
Open →
04

Quicksort

Quicksort sorts inside the array it is given, with no extra arrays. It picks a pivot, partitions the items around it, and recurses on both sides. A balanced split gives n log n, a lopsided split gives n², and an already sorted list is the worst input when the last item is the pivot. A random pivot protects us.

pivotpartitionin placebest and worst caserandom pivotnot stable
Open →
05

Summary and practice

A one-page map of the chapter, five self-check questions, ten exercises with solutions, and a way to test claims an AI makes about complexity.

self-checkexercises
Open →

How to use these notes

Watch · Predict · Step through · Practise

STEP 1

Watch

Each part starts with its video, 8–10 minutes, in Python or Java.

STEP 2

Predict

Answer the amber Predict box before you open the answer.

STEP 3

Step through

The interactive demos are being added; each one is described where it will appear.

STEP 4

Practise

Finish with the self-check and the exercises, with templates in your language.