Stern-Brocot Tree & Farey Sequence
Stern-Brocot tree¶
The Stern-Brocot tree is an elegant data structure for maintaining positive fractions. It was discovered independently by Moritz Stern in 1858 and Achille Brocot in 1861.
Overview¶
The Stern-Borcot tree starts with two simple fractions:
This
Every time we insert a fraction
Since we call this data structure Stern-Brocot tree, it must look like a tree, right? Look at this figure:
You can think of the sequence at the
Property¶
Next, let's discuss the property of the Stern-Brocot tree.
Monotonicity¶
In the sequence of each layer, the true fraction is monotonically increasing.
Brief proof: only need to prove in the case of
That's it. This is very easy, we can just do algebraic transformation directly
The same is true on the other side.
Irreducibility¶
The fractions in the sequence (except for
Brief proof: To prove the irreducibility, we first prove that for two consecutive fractions in the sequence
Obviously, we only need to prove that
The latter part is the same. we can prove using the extended Euclidean theorem. If the above equation has a solution, obviously
With the above proof, we can prove
With these two properties, you can treat it as a balanced tree. So we can construct and query the same way we do to the balanced trees.
Implementation¶
Construct:
void build(int a = 0, int b = 1, int c = 1, int d = 0, int level = 1) {
int x = a + c, y = b + d;
// ... output the current fraction x/y
// at the current level in the tree
build(a, b, x, y, level + 1);
build(x, y, c, d, level + 1);
}
Query:
string find(int x, int y, int a = 0, int b = 1, int c = 1, int d = 0) {
int m = a + c, n = b + d;
if (x == m && y == n) return "";
if (x * n < y * m)
return 'L' + find(x, y, a, b, m, n);
else
return 'R' + find(x, y, m, n, c, d);
}
Farey sequence¶
Stern-Brocot tree and Farey sequence have very similar characteristics. The
Obviously, the above algorithm for constructing Stern-Brocot tree is also suitable for constructing the Farey sequence. Because the numbers in the Stern-Brocot tree are the irreducible fraction, a slight modification of the boundary conditions (denominator) can form the code for constructing the Farey sequence. You can think the Farey sequence
The Farey sequence also satisfies the irreducibility and monotonicity, and satisfies a property similar to the Stern-Brocot tree: for the three consecutive numbers
From the definition of Farey sequence, we can get the length of
This page is mainly translated from the blog post Дерево Штерна-Броко. Ряд Фарея and its English version The Stern-Brocot Tree and Farey Sequences. The license for the Russian version is Public Domain + Leave a Link; the license for the English version is CC-BY-SA 4.0.
buildLast update and/or translate time of this article,Check the history
editFound smelly bugs? Translation outdated? Wanna contribute with us? Edit this Page on Github
peopleContributor of this article OI-wiki
translateTranslator of this article Visit the original article!
copyrightThe article is available under CC BY-SA 4.0 & SATA ; additional terms may apply.