# CalkinŌĆōWilf tree

https://en.wikipedia.org/wiki/CalkinŌĆōWilf_tree

In number theory, the CalkinŌĆōWilf tree is a tree in which the vertices correspond one-to-one to the positive rational numbers. The tree is rooted at the number 1, and any rational number expressed in simplest terms as the fraction a/b has as its two children the numbers a/a + b and a + b/b. Every positive rational number appears exactly once in the tree.

The sequence of rational numbers in a breadth-first traversal of the CalkinŌĆōWilf tree is known as the CalkinŌĆōWilf sequence. Its sequence of numerators (or, offset by one, denominators) is Stern's diatomic series, and can be computed by the fusc function.

The CalkinŌĆōWilf tree is named after Neil Calkin and Herbert Wilf, who considered it in their 2000 paper. The tree was introduced earlier by Jean Berstel and Aldo de Luca  as Raney tree, since they drew some ideas from a paper by George N. Raney.  Stern's diatomic series was formulated much earlier by Moritz Abraham Stern, a 19th-century German mathematician who also invented the closely related SternŌĆōBrocot tree. Even earlier, a similar tree appears in Kepler's Harmonices Mundi (1619). 

## Definition and structure

The CalkinŌĆōWilf tree may be defined as a directed graph in which each positive rational number a/b occurs as a vertex and has one outgoing edge to another vertex, its parent. We assume that a/b is in simplest terms; that is, the greatest common divisor of a and b is 1. If a/b < 1, the parent of a/b is a/b ŌłÆ a; if a/b > 1, the parent of a/b is a ŌłÆ b/b. Thus, in either case, the parent is a fraction with a smaller sum of numerator and denominator, so repeated reduction of this type must eventually reach the number 1. As a graph with one outgoing edge per vertex and one root reachable by all other vertices, the CalkinŌĆōWilf tree must indeed be a tree.

The children of any vertex in the CalkinŌĆōWilf tree may be computed by inverting the formula for the parents of a vertex. Each vertex a/b has one child whose value is less than 1, a/a + b, because this is the only value less than 1 whose parent formula leads back to a/b. Similarly, each vertex a/b has one child whose value is greater than 1, a + b/b. 

Although it is a binary tree (each vertex has two children), the CalkinŌĆōWilf tree is not a binary search tree: its inorder does not coincide with the sorted order of its vertices. However, it is closely related to a different binary search tree on the same set of vertices, the SternŌĆōBrocot tree: the vertices at each level of the two trees coincide, and are related to each other by a bit-reversal permutation. The CalkinŌĆōWilf sequence, depicted as the red spiral tracing through the CalkinŌĆōWilf tree

The CalkinŌĆōWilf sequence is the sequence of rational numbers generated by a breadth-first traversal of the CalkinŌĆōWilf tree,

1/1, 1/2, 2/1, 1/3, 3/2, 2/3, 3/1, 1/4, 4/3, 3/5, 5/2, 2/5, 5/3, 3/4, ŌĆ”.

Because the CalkinŌĆōWilf tree contains every positive rational number exactly once, so does this sequence.  The denominator of each fraction equals the numerator of the next fraction in the sequence. The CalkinŌĆōWilf sequence can also be generated directly by the formula

$q_{i+1}={\frac {1}{2\lfloor q_{i}\rfloor -q_{i}+1}}$ where qi denotes the ith number in the sequence, starting from q1 = 1, and ŌīŖ qi Ōīŗ represents the integral part. 

It's also possible to calculate qi directly from the run-length encoding of the binary representation of i: the number of consecutive 1s starting from the least significant bit, then the number of consecutive 0s starting from the first block of 1s, and so on. The sequence of numbers generated in this way gives the continued fraction representation of qi.Example:

i = 1081 = 100001110012: The continued fraction is [1;2,3,4,1] hence q1081 = 53/37.
i = 1990 = 111110001102: The continued fraction is [0;1,2,3,5] hence q1990 = 37/53.

In the other direction, using the continued fraction of any qi as the run-length encoding of a binary number gives back i itself. Example:

qi = 3/4: The continued fraction is [0;1,3] hence i = 11102 = 14.
qi = 4/3: The continued fraction is [1;3]. But to use this method, the length of the continued fraction must be an odd number. So [1;3] should be replaced by the equivalent continued fraction [1;2,1]. Hence i = 10012 = 9.

A similar conversion between run-length-encoded binary numbers and continued fractions can also be used to evaluate Minkowski's question mark function; however, in the CalkinŌĆōWilf tree the binary numbers are integers (positions in the breadth-first traversal) while in the question mark function they are real numbers between 0 and 1.

## Stern's diatomic sequence

Stern's diatomic sequence is the integer sequence

0, 1, 1, 2, 1, 3, 2, 3, 1, 4, 3, 5, 2, 5, 3, 4, ŌĆ” (sequence in the OEIS).

Using zero-based numbering, the nth value in the sequence is the value fusc(n) of the fusc function, named  according to the obfuscating appearance of the sequence of values and defined by the recurrence relations

{\begin{aligned}\operatorname {fusc} (2n)&=\operatorname {fusc} (n)\\\operatorname {fusc} (2n+1)&=\operatorname {fusc} (n)+\operatorname {fusc} (n+1),\end{aligned}} with the base cases fusc(0) = 0 and fusc(1) = 1.

The nth rational number in a breadth-first traversal of the CalkinŌĆōWilf tree is the number fusc(n)/fusc(n + 1).  Thus, the diatomic sequence forms both the sequence of numerators and the sequence of denominators of the numbers in the CalkinŌĆōWilf sequence.

The function fusc(n + 1) is the number of odd binomial coefficients of the form (n ŌłÆ r
r
)
, 0 Ōēż 2r < n,  and also counts the number of ways of writing n as a sum of powers of two in which each power occurs at most twice. This can be seen from the recurrence defining fusc: the expressions as a sum of powers of two for an even number 2n either have no 1s in them (in which case they are formed by doubling each term in an expression for n) or two 1s (in which case the rest of the expression is formed by doubling each term in an expression for n ŌłÆ 1), so the number of representations is the sum of the number of representations for n and for n ŌłÆ 1, matching the recurrence. Similarly, each representation for an odd number 2n + 1 is formed by doubling a representation for n and adding 1, again matching the recurrence.  For instance,

6 = 4 + 2 = 4 + 1 + 1 = 2 + 2 + 1 + 1

has three representations as a sum of powers of two with at most two copies of each power, so fusc(6 + 1) = 3.

## Relation to SternŌĆōBrocot tree

The CalkinŌĆōWilf tree resembles the SternŌĆōBrocot tree in that both are binary trees with each positive rational number appearing exactly once. Additionally, the top levels of the two trees appear very similar, and in both trees, the same numbers appear at the same levels. One tree can be obtained from the other by performing a bit-reversal permutation on the numbers at each level of the trees.  Alternatively, the number at a given node of the CalkinŌĆōWilf tree can be converted into the number at the same position in the SternŌĆōBrocot tree, and vice versa, by a process involving the reversal of the continued fraction representations of these numbers.  However, in other ways, they have different properties: for instance, the SternŌĆōBrocot tree is a binary search tree: the left-to-right traversal order of the tree is the same as the numerical order of the numbers in it. This property is not true of the CalkinŌĆōWilf tree.