Consider two fractions n1m1 and n2m2 with positive numerators and denominators. The fraction n1+n2m1+m2 is called the mediant of n1m1 and n2m2. It is straightforward to show that the mediant is placed numerically between the original fractions,
(1)
n1m1<n2m2⇒n1m1<n1+n2m1+m2<n2m2.
Consider now the following simple procedure. Start with the two fractions,
(2)
10,01.
(If you don't like calling 01 a fraction just consider tuples of integers instead, where inequality (m1,n1)<(m2,n2), for instance, means m1n2<m2n1. However, 01 is just an auxiliary fraction that makes everything simpler.) Insert now the mediant in between the two,
(3)
10,11,01.
and repeat the process, inserting mediants between any two consequtive fractions, obtaining
(4)
10,21,11,12,01,
(5)
10,31,21,32,11,23,12,13,01,
and so on. At any stage of the process we see that the values of the fractions strictly increase going from left to right, because of (1).
The Stern-Brocot Tree
A lot more can be said about these sequences, but first we arrange the fractions in the so-called Stern-Brocot tree. To simplify the definition and the properties of this structure, we use 2×2 matrices with non-negative integers as elements. The initial two fractions from (2) will be represented by
(6)
I=[1001],
where the numerators and denominators are in the first and second row, respectively. For technical reasons we will always have the larger fraction in the first column and the smaller fraction in the second column. We can now associate a fraction to any 2×2 matrix,
f([m2n2m1n1])=n2+n1m2+m1,
which is seen to be the mediant of the two fractions represented by the columns of the matrix. So the first mediant we inserted was f(I)=11, leading to (3).
Notice how (right-)multiplication of both L and R preserves the fact that the fraction represented by the first column is greater than the fraction represented by the second column. This follows from (1). Notice also how f(L)=21 and f(R)=12 are the left- and rightmost of the newly inserted fractions in (4), respectively. Similarly, f(L2)=31, f(LR)=32, f(RL)=23, f(R2)=13 are the inserted fractions in (5).
This process is easily generalized:
Definition 1. The Stern-Brocot tree is equal to T(I), where T(M) is a binary tree with root f(M), left subtree T(ML), and right subtree T(MR).
So the root of the Stern-Brocot tree is f(I)=11 with T(L) and T(R) as left and right subtree, respectively. T(L) has root f(L)=21, left subtree T(L2) and right subtree T(LR). T(R) has root f(R)=12, left subtree T(RL) and right subtree T(R2). And so on. The top of the tree can be seen in Figure 1.
Consider any subtree T(M) of the Stern-Brocot tree with root f(M). Note that it follows from the definitions that every node in the left subtree T(ML) is strictly less than f(M) and that every node in the right subtree T(MR) is strictly greater than f(M). Thus, the Stern-Brocot tree is a binary search tree, although an infinite one.
From the definition we see that for any node f(M) of the Stern-Brocot tree we have
(8)
M=Ra0La1Ra2La3⋯Lan−1,
for even n and non-negative integers ak. Insisting that n must be even is just a technicality which will be clarified later. Any ak can be zero so its not a restriction. Computing the determinants of the simple matrices in (6) and (7) we get detI=detL=detR=1. Since det(AB)=detAdetB it follows that
(9)
detM=1 for any subtree T(M) of the Stern-Brocot tree.
This is an important property. Consider any node nm of the tree. Then the left subtree is T(M′) with
M′=[m′n′mn]
for some integers m′ and n′. But then m′n−mn′=1 from (9), which shows that m and n are relatively prime, m⊥n. Or, to put it another way, every fraction in the Stern-Brocot tree is in its lowest terms.
A natural question is now: Can any reduced positive fraction be found in the Stern-Brocot tree? Assume there is a fraction qp with p⊥q which is not present. Consider now the process of searching for this fraction. This will produce an infinite sequence of matrices of the form (8) for n=0,1,…, where the ak's are determined from the number of left and right branches chosen during the search. Let one of the matrices be
M=[m2n2m1n1] where n1m1<qp<n2m2.
The inequalities follow from the properties of the Stern-Brocot tree and the search process being performed. They imply that
pn1−qm1≥1 and qm2−pn2≥1
and we know from (9) that n1m2−m1n2=1. We now get
This inequality must hold for the infinite sequence of matrices from the search process. This is not possible since the sum of the elements of any matrix of the form (8) strictly increases when multiplied by L or R. So the Stern-Brocot tree contains all reduced positive fractions.
Relation to Continued Fractions
The Stern-Brocot tree is intimately tied to continued fractions. See the post Continued Fractions and Continuants for some basic properties that we will use below. Especially equations (8) and (10) from that post are essential to the following. Observe that
where P is a permutation matrix for which P2=I. Recall that any node in the Stern-Brocot is of the form f(M) where M is as in (8). Using the identities above we get
This is a truly remarkable result. There is a one-to-one correspondance between continued fractions and nodes in the Stern-Brocot tree. Indeed, the continued fraction a0+//a1,…,an−1,1// can be found in the tree by starting at the root 11 and then taking a0 right branches, a1 left branches, a2 right branches, and so on, until taking an−1 left branches. What if an−1=0 for even n≥2? We have
a0+//a1,…,an−2,0,1//=a0+//a1,…,an−2,1//,
so a trailing //…,0,1// can be simplified to //…,1//. So all cases are captured in
for even n≥2, linking any node from the Stern-Brocot tree to a continued fraction and vice versa.
For instance, 57=1+//2,2//=1+//2,1,1//, so 57 can be found in the Stern-Brocot tree by going right one time from the root, then 2 times to the left, and finally 1 time to the right. Check Figure 1 to verify. Similarly, we see that 41 is located by going 3 times to the left from the root. What is its continued fraction representation? Since we must start by counting right we rephrase its location to: Go 0 times to the right and then 3 times to the left. This gives us 41=0+//3,1//=//4//.