# Stern-Brocot Tree

### Binary Encoding, a Second Look

As we have seen, Algorithm 1 finds L-R encoding for any (positive) rational number m/n. However, intermediate fractions generated by the algorithm are not quite those one would expect from the manner in which the encoding has been defined. With

tLRLL = [0/1, 1/0]LRLL = [0/1, 1/1]RLL = [1/2, 1/1]LL = [1/2, 2/3] = [1/2, 3/5].

In terms of single numbers, the sequence is 1/1, 1/2, 2/3, 3/5, 4/7, and we as well may wonder why does the whole thing really work. Of course a proof is a proof and, since we have one, we know that the algorithm works. In what follows, I establish a geometric interpretation for the algorithm which puts the L-R encoding and the algorithm back into the scope of our intuition.

Two identities f(RS) = f(S) + 1 and 1/f(LS) = 1/f(S) + 1 form a basis for our derivation. Given a fraction m/n and its (unique) encoding S, we obtain another fraction f(RS) through the first formula. f(RS) is uniquely determined by m/n which enables us to define a function f_{R} as f_{R}(m/n) = m/n+1 = (m+n)/n. It all may appear as a vacuous manipulation of mathematical symbols; for, obviously, m/n+1 is a function of m/n. The point was of course not in affirming a trivial mathematical fact but in pointing out that this trivial mathematical fact underlies the algorithm at hand. From the second formula we derive another function f_{L} as f_{L}(m/n) = m/(n+m). An easily proven formula f_{R}(1 - f_{L}) = 1 may also be observed on the tree.

In the diagram below, red lines trace the action of f_{R}, the blue lines that of f_{L}. Starting with row #1, there are three lines emanating from every fraction on the tree: two (one red, one blue) point to the next row, one line points to the row above. Because of the latter property, for every fraction m/n, there is a unique way upwards that connects it to the fraction 1/1. For example, for m/n = 4/7 we get successively, 4/7, 4/3, 1/3, 1/2, 1/1 which, of course, is the sequence that has been generated by the algorithm.

This also shows that 4/7 = f_{L}f_{R}f_{L}f_{L}(1/1).

One more observation will prove helpful. For a given row, all red lines from its terms downwards end on the right half of the row below. Likewise, all the blue lines connect fractions in one row to the left-hand side fractions of the next row. We have already observed how the two halves of a row (of numerators) are formed from the previous row. Bearing in mind that in the Stern-Brocot tree, sequences of denominators are obtained by reversing the order of the sequences of numerators, we see that, extended to the tree of fractions our old observation confirms the new one: to obtain the right half of a row, apply function f_{R} to the fractions of the previous row. To obtain the left half, apply function f_{L}.

Let's compare 4/7 = f_{L}f_{R}f_{L}f_{L}(1/1) with f(LRLL) = 4/7 which is obtained from _{L}f_{R}f_{L}f_{L}(1/1)_{L}f_{R}f_{L}f_{L}(1/1) f_{L} and f_{R} appear on the left. What's the mystery?

Extend definition of f_{R} and f_{L} columnwise to the tree (or the matrix) case: _{R}[x1/y2, y1/x2] = [f_{R}(x1/y2), f_{R}(y1/x2)]._{L}[x1/y2, y1/x2] = [f_{L}(x1/y2), f_{L}(y1/x2)]._{L}[0/1,1/0] = [0/1,1/1]._{L}f_{R}f_{L}f_{L}[0/1, 1/0] = f_{L}f_{R}f_{L}[0/1, 1/1] = f_{L}f_{R}[0/1, 1/2] = f_{L}[1/1, 3/2] = [1/2, 3/5]_{L}f_{R}f_{L}f_{L}t^{2} = I,_{L} and tRt = f_{R}? This is indeed the case. From the definition, f_{R}[x1/y2, y1/x2] = [f_{R}(x1/y2), f_{R}(y1/x2)], _{R}[x1/y2, y1/x2] = [f_{R}(x1/y2), f_{R}(y1/x2)],_{R} is _{L} is _{R} = L_{L} = R.

As a result, we got two binary encodings complementary to each other. Consider the "left-side" one (namely RLRRt, as opposed to the "right-side" encoding tLRLL.) Replacing R with 0 and L with 1 gives a binary string 0100 which converts to the decimal 4. A glance at the last row of the tree above reveals that 4/7 is located in the last row of the tree in the fifth position. Should we decide to start counting from 0, the position of 4/7 would become 4th. So let's go for it. Start counting from 0. Every time we prepend a 0 to a given binary string the string goes to the left in the next row. When we prepend 1, the string goes to the right. So we see, that in any row, binary encoding of fractions in the left half of the row starts with 0. The encodings in the right half start with 1. The net result of this observation is that encodings of the fractions in any given row are given by consecutive binary numbers: (for row #3)

How do we get 4/3? First, 4/3 is located in the third row. Therefore we shall need 3 binary digits to represent this fraction. Second, it's the fourth fraction in the row. (The first is the 0th!) Thus its binary encoding is 100 which converts to LRR for the "left-side" encoding or into its complementary RLL "right-side" encoding.

**Problem**: Find the fraction in the 10-th position of the 4-th row of the Stern-Brocot tree. (Remember to start counting from 0.)

**Solution**: Using for binary digits, 10th position is represented by 1010 which
converts to LRLR for the "left-hand" encoding and to RLRL for the "right-hand" encoding.

**Problem**: Find location on the tree of a fraction whose "right-side" encoding is
LRLLRLRR.

**Solution**: Its "left-side" encoding is RLRRLRLL which converts to 01001011_{2} = 75_{10}. Therefore, the corresponding fraction is located in the row #8, position 75 where the count starts from 0.

- Modular Arithmetic
- Chinese Remainder Theorem
- Euclid's Algorithm
- Euclid's Game
- Binary Euclid's Algorithm
- gcd and the Fundamental Theorem of Arithmetic
- Extension of Euclid's Algorithm
- Stern-Brocot Tree
- Fine features
- Binary Encoding
- Binary Encoding, a Second Interpretation
- Continued Fractions on the Stern-Brocot Tree
- Fractions on a Binary Tree
- Fractions on a Binary Tree II

- Farey series

- Pick's Theorem
- Fermat's Little Theorem
- Wilson's Theorem
- Euler's Function
- Divisibility Criteria
- Examples
- Equivalence relations
- A real life story

Copyright © 1966-2016 Alexander Bogomolny

70372563