# Bird tree

I came across a very nice method of enumerating the positive rational numbers based on a particular tree. This post explains how this tree is constructed and shows that actually all positive rational numbers are contained in this tree.

## Bird tree definition

The tree itself is defined in a very simple manner:

- The root element contains 1 (i.e. the fraction \(\frac{1}{1}\)).
- The left subtree of the root is the whole bird tree mapped using the function \(x \mapsto \frac{1}{b+1}\).
- The right subtree of the root is the whole bird tree mapped using the function \(x \mapsto \frac{1}{b}+1\).

A very simplistic implementation in Haskell could look as follows:

```
data BirdTree a = Node a (BirdTree a) (BirdTree a)
instance Functor BirdTree where
fmap f (Node e l r) = Node (f e) (fmap f l) (fmap f r)
bird = Node 1
(fmap (\b -> 1/(b+1)) bird)
(fmap (\b -> (1/b) + 1) bird)
```

Let's consider the first levels of the Bird tree:

```
1/1
/ \
/ \
/ \
1/2 2/1
/ \ / \
2/3 1/3 3/1 3/2
```

## All positive rationals are contained in the Bird tree

It can be shown quite easily that all positive rationals are contained in this tree. We do so by first defining two functions:

- \(r(x) = \frac{1}{x} + 1\)
- \(l(x) = \frac{1}{x+1}\)

We can use this functions to "traverse" the tree in a very straightforward manner:

- \(l(r(1))\) denotes the value that is reached if we first go
*r*ight and then*l*eft, which is \(l(r(1)) = l(\frac{1}{x+1}) = l(\frac{1}{2}) = \frac{1}{\frac{1}{2}} + 1 = \frac{3}{1}\). We can confirm that this corresponds to the value in the Bird tree. - Similarily, the value \(l(r(r(l(1))))\) denotes the value reached by going left, right, right, left.

Let us also construct the respective inverse functions of \(l\) and \(r\):

- \(r^{-1}(x) = \frac{1}{x-1}\)
- \(l^{-1}(x) = \frac{1}{x}-1\)

If we now want to prove that any \(q\in\mathbb{Q}\) is contained in the tree, we want to prove that - given any \(q\) - we can find functions \(f_1,\dots,f_n \in \left\{ l, r\right\}\) such that \(f_1(f_2(\dots f_n(1))) = q\).

We transform this claim (by using the inverse functions) into the following: We can find - given any \(q\in\mathbb{Q}\) functions \(f_1^{-1},\dots,f_n^{-1}\in\left\{ l^{-1}, r^{-1}\right\}\) such that \(f_n^{-1}(f_{n-1}^{-1}(\dots f_2^{-1}(f_1^{-1}(q))))=1\).

## Euclid to the rescue

This might not look like a simplification at first sight, but we can compute the following:

- \(r^{-1}(\frac{a}{b}) = \frac{1}{\frac{a}{b}-1} = \frac{b}{a-b}\)
- \(l^{-1}(\frac{a}{b}) = \frac{1}{\frac{a}{b}} - 1 = \frac{b-a}{a}\)

Let us use - for a short intermezzo - use another notation for fractions: Instead of the fraction \(\frac{a}{b}\) we write the pair \((a, b)\). Extending this notation, the functions \(r^{-1}\) resp. \(l^{-1}\) map the pair \((a,b)\) to \((b, a-b)\) resp. \((b-a, a)\). Now, our only goal is to prove that we can "concatenate" these functions in a way such that we eventually reach a pair \((c,c)\) for some \(c\in\mathbb{N}\).

This, however, is a well known fact: If we have a pair \((a, b)\), we can have three cases:

- \(a=b\): This means that we are already done and nothing is left to be proven.
- \(a > b\): We continue with the pair \((b, a-b)\) (i.e. we choose the function \(r^{-1}\)).
- \(a < b\): We continue with the pair \((b-a, a)\) (i.e. we choose the function \(l^{-1}\)).

It is clear that this routine finally produces a pair with two equal numbers - it corresponds to Euclids algorithm for finding the greatest common divisor.

## Testing stuff

We can transform our knowledge into a small piece of software:

```
-- The functions l and r...
r x = (1 / x) + 1
l x = 1 / (x + 1)
-- ... and their inverse counterparts
_l x = 1/x - 1
_r x = 1/(x - 1)
-- find a certain element
find 1 = []
find q = if q > 1 then 1:(find (_r q)) else 0:(find (_l q))
```

The result of `find a`

is a list of zeroes and ones, where 1 indicates that you should go right and 0 indicates that you should go left.

We write a quick test routine to ensure that our finding routine actually works.

```
-- traverse tree according to a specific route
traverse :: BirdTree a -> [Integer] -> Maybe a
traverse (Node e l r) seq = case seq of
[] -> Just e
(x:xs) -> traverse (if x==0 then l else r) xs
-- check results with the following:
-- traverse bird $ find (123/345)
```