Proofs: Stern-Brocot tree
These are the human proofs of Stern-Brocot tree, at full length.
The Stern-Brocot tree holds each positive rational once, and its operations are correct full
This proof shows: correctness.
A node is a quadruple of nonnegative integers. The root is , the left child of is and the right child is . The value of the node is , and we read and as its left bound and right bound, with . We write .
Lemma 1 (invariant). Every node satisfies , , , and ; its value is in lowest terms and positive.
Proof. The root has . For the left child, , and for the right child, . The entries and never decrease and start at 1. Then gives , and similarly gives the other inequality (for the right bound is ). The relation shows that any common divisor of and divides 1. The value is positive because .
Lemma 2 (search tree). Every node in the subtree of a node has its value strictly between and ; those in the left subtree are below the value of , and those in the right subtree are above it. In particular, all values are distinct.
Proof. By induction on the depth below , every node of the subtree has bounds in the closed interval , since a child replaces one bound by the value of its parent, which lies between the parent's bounds by Lemma 1. Its value lies strictly between its own bounds, hence strictly between and . The left child of has the right bound equal to the value of , so its whole subtree lies below it; symmetrically for the right child. Two distinct nodes either lie in different subtrees of their lowest common ancestor, or one is an ancestor of the other, and in both cases their values differ.
Lemma 3 (completeness). Let be coprime. The walk from the root that goes left when is smaller than the value and right when it is larger reaches the node with value after at most steps.
Proof. While the value differs from , Lemma 2 keeps , so and as integers. Using ,
so . Each step increases by or , at least 1, and at the root, so after at most steps the walk is at a node whose value is .
Lemmas 1 to 3 together say that the map from nodes to values is a bijection onto the positive rationals, with each value in lowest terms.
Lemma 4 (runs). From a node with bounds , right moves lead to the bounds , and left moves to .
Proof. A right move keeps and replaces by ; after of them, has gained . Left moves are symmetric.
So DECODE (lines L8 to L11) returns the node at the end of the path.
Lemma 5 (ENCODE). For , ENCODE returns the path to the node with value .
Proof. Let be the continued fraction produced by Euclid's algorithm, so , for , and when (the last quotient of Euclid is at least 2 because the last two remainders satisfy and ); if , then . These quotients depend only on the value . Lines L2 to L6 compute exactly , decrease by 1, and emit , dropping runs of length 0.
It remains to show that the search walk for is with the last run of length . At a node with bounds , the map is increasing on (its derivative has the sign of ), runs from to , and takes the value of the node at . So every with is for exactly one real , the coordinate of at that node, and the walk goes right if , left if , and stops if . At the root, the coordinate of is itself.
- After a right move, the bounds are and with .
- After a left move, the bounds are and with , that is, .
So, writing while the walk goes left, the walk is: go right and subtract 1 from while ; go left and subtract 1 from while ; stop when the current coordinate is 1. We prove by induction on that, for with when and when , this process makes moves in the first direction, then in the other, and so on, with in the last run. If , then is an integer, and it makes moves before . If , then , so it makes exactly moves and reaches , and from there it continues in the other direction with , where the induction hypothesis applies. With at the root and the first direction , this is the output of ENCODE. Euclid's algorithm takes steps by Lamé's theorem, so ENCODE is fast even when the depth is large.
Lemma 6 (PARENT). For a node root with bounds , line L13 returns the bounds of its parent.
Proof. If is the right child of , then and , so because , and . If is the left child, then and , so because , and . The two cases are told apart by comparing with , which is what line L13 does. Only the root has bounds , since every other node has ; line L12 handles it.
Lemma 7 (BEST-APPROXIMATION). For with , , and , BEST-APPROXIMATION returns a fraction in lowest terms with that minimizes , and the smaller of two such fractions on a tie.
Proof. Invariant: the current bounds satisfy , , with unless , and is not the value of any node visited so far. It holds at the start since .
In an iteration, the value has . If , line L16 returns , which is in lowest terms by Lemma 1 and is at distance 0. If , every with and is allowed, among them. The fraction is increasing in (because ), so these form an initial segment, and the condition is , where by ; this gives the largest as , capped by if . By Lemma 4 the new bounds are those of a node of the tree, and the invariant holds, the new left bound being below . If , the argument is symmetric with decreasing in and the condition , where , with equality only when and , in which case the condition holds for all . The new right bound stays above , and .
The loop ends. A left move increases by , and a right move increases by unless . A right move with is not capped by , so its is the largest one with , and the next node is at least : the next iteration returns or moves left. So grows at least every second iteration and eventually exceeds . When it ends without returning, and .
Now suppose is a fraction with . Then and , and as in Lemma 3, ... precisely, . So no fraction with denominator at most lies strictly between and . Every candidate is at distance at least from , and every candidate is at distance at least ; both bounds have denominators at most when finite (, and when ). So the nearest candidates are and , and line L19 picks the nearer, comparing with , which are times the two distances. If , then and is the answer. On a tie it returns , the smaller one, and is in lowest terms because .