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 has as its two children the numbers and. 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 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.

Definition and structure

The Calkin–Wilf tree may be defined as a directed graph in which each positive rational number occurs as a vertex and has one outgoing edge to another vertex, its parent. We assume that is in simplest terms; that is, the greatest common divisor of and is 1. If, the parent of is ; if, the parent of is. 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 has one child whose value is less than 1,, because this is the only value less than 1 whose parent formula leads back to. Similarly, each vertex has one child whose value is greater than 1,.
Although it is a binary tree, 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.

Breadth first traversal

The Calkin–Wilf sequence is the sequence of rational numbers generated by a breadth-first traversal of the Calkin–Wilf tree,
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
where denotes the th number in the sequence, starting from, and represents the integral part.
It's also possible to calculate directly from the run-length encoding of the binary representation of :
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.Example:
In the other direction, using the continued fraction of any as the run-length encoding of a binary number gives back itself. Example:
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 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
Using zero-based numbering, the th value in the sequence is the value of the fusc function, named according to the obfuscating appearance of the sequence of values and defined by the recurrence relations
with the base cases and.
The th rational number in a breadth-first traversal of the Calkin–Wilf tree is the number. 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 is the number of odd binomial coefficients of the form,, and also counts the number of ways of writing 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 either have no 1s in them or two 1s, so the number of representations is the sum of the number of representations for and for, matching the recurrence. Similarly, each representation for an odd number is formed by doubling a representation for and adding 1, again matching the recurrence. For instance,
has three representations as a sum of powers of two with at most two copies of each power, so.

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.