Start with two "boundary" fractions: 0/1 and 1/0. Insert the mediant (a+c)/(b+d) between any two adjacent fractions. Repeat forever. Every positive rational appears exactly once, in lowest terms — and the tree is the binary tree of best rational approximations to every irrational.
why every rational appears once, in lowest terms
The mediant of fractions p/q and r/s is (p+r)/(q+s). For two fractions in lowest terms with p/q < r/s, the mediant always lies strictly between them. Repeatedly inserting mediants between adjacent pairs generates the Stern-Brocot tree:
0/1 — 1/0
0/1 — 1/1 — 1/0
0/1 — 1/2 — 1/1 — 2/1 — 1/0
0/1 — 1/3 — 1/2 — 2/3 — 1/1 — 3/2 — 2/1 — 3/1 — 1/0
Two theorems make this magical:
1. Every mediant is in lowest terms. If p/q and r/s are adjacent (in some level), then ps − qr = ±1 (a Farey-neighbor invariant). The mediant (p+r)/(q+s) inherits this, so it's automatically reduced.
2. Every positive rational appears exactly once. To find p/q, navigate the tree: at each node, go left if your target < current, right if your target > current. The path is the rational's "Stern-Brocot encoding" — equivalent to its continued fraction.
For irrationals: there's no node that equals them, but the tree path goes on forever. The denominators along the path are exactly the convergents of the continued fraction. For φ, the path alternates LRLRLR… (every CF coefficient = 1), giving Fibonacci ratios. For π, the path takes many lefts then a big jump right (because π's CF has a big coefficient: [3; 7, 15, 1, 292, ...]).
Enter any target and click "trace path" — watch the binary path through the tree converge on it. The fractions along the path are the best rational approximations.