<s>
In	O
number	O
theory	O
,	O
the	O
Stern	B-Data_Structure
–	I-Data_Structure
Brocot	I-Data_Structure
tree	I-Data_Structure
is	O
an	O
infinite	O
complete	O
binary	O
tree	O
in	O
which	O
the	O
vertices	O
correspond	O
one-for-one	B-Algorithm
to	O
the	O
positive	O
rational	O
numbers	O
,	O
whose	O
values	O
are	O
ordered	O
from	O
the	O
left	O
to	O
the	O
right	O
as	O
in	O
a	O
search	B-Data_Structure
tree	I-Data_Structure
.	O
</s>
<s>
The	O
Stern	B-Data_Structure
–	I-Data_Structure
Brocot	I-Data_Structure
tree	I-Data_Structure
was	O
introduced	O
independently	O
by	O
and	O
.	O
</s>
<s>
Stern	O
was	O
a	O
German	O
number	O
theorist	O
;	O
Brocot	O
was	O
a	O
French	O
clockmaker	O
who	O
used	O
the	O
Stern	B-Data_Structure
–	I-Data_Structure
Brocot	I-Data_Structure
tree	I-Data_Structure
to	O
design	O
systems	O
of	O
gears	O
with	O
a	O
gear	O
ratio	O
close	O
to	O
some	O
desired	O
value	O
by	O
finding	O
a	O
ratio	O
of	O
smooth	O
numbers	O
near	O
that	O
value	O
.	O
</s>
<s>
The	O
root	O
of	O
the	O
Stern	B-Data_Structure
–	I-Data_Structure
Brocot	I-Data_Structure
tree	I-Data_Structure
corresponds	O
to	O
the	O
number	O
1	O
.	O
</s>
<s>
The	O
parent-child	O
relation	O
between	O
numbers	O
in	O
the	O
Stern	B-Data_Structure
–	I-Data_Structure
Brocot	I-Data_Structure
tree	I-Data_Structure
may	O
be	O
defined	O
in	O
terms	O
of	O
continued	O
fractions	O
or	O
mediants	O
,	O
and	O
a	O
path	O
in	O
the	O
tree	O
from	O
the	O
root	O
to	O
any	O
other	O
number	O
q	O
provides	O
a	O
sequence	O
of	O
approximations	O
to	O
q	O
with	O
smaller	O
denominators	O
than	O
q	O
.	O
</s>
<s>
Because	O
the	O
tree	O
contains	O
each	O
positive	O
rational	O
number	O
exactly	O
once	O
,	O
a	O
breadth	B-Algorithm
first	I-Algorithm
search	I-Algorithm
of	O
the	O
tree	O
provides	O
a	O
method	O
of	O
listing	O
all	O
positive	O
rationals	O
that	O
is	O
closely	O
related	O
to	O
Farey	O
sequences	O
.	O
</s>
<s>
The	O
left	O
subtree	O
of	O
the	O
Stern	B-Data_Structure
–	I-Data_Structure
Brocot	I-Data_Structure
tree	I-Data_Structure
,	O
containing	O
the	O
rational	O
numbers	O
in	O
the	O
range	O
(	O
0	O
,	O
1	O
)	O
,	O
is	O
called	O
the	O
Farey	B-Data_Structure
tree	I-Data_Structure
.	O
</s>
<s>
Then	O
,	O
unless	O
,	O
the	O
number	O
has	O
a	O
parent	O
in	O
the	O
Stern	B-Data_Structure
–	I-Data_Structure
Brocot	I-Data_Structure
tree	I-Data_Structure
given	O
by	O
the	O
continued	O
fraction	O
expression	O
Equivalently	O
this	O
parent	O
is	O
formed	O
by	O
decreasing	O
the	O
denominator	O
in	O
the	O
innermost	O
term	O
of	O
the	O
continued	O
fraction	O
by1	O
,	O
and	O
contracting	O
with	O
the	O
previous	O
term	O
if	O
the	O
fraction	O
becomes	O
.	O
</s>
<s>
The	O
Stern	B-Data_Structure
–	I-Data_Structure
Brocot	I-Data_Structure
tree	I-Data_Structure
forms	O
an	O
infinite	O
binary	B-Language
search	I-Language
tree	I-Language
with	O
respect	O
to	O
the	O
usual	O
ordering	O
of	O
the	O
rational	O
numbers	O
.	O
</s>
<s>
The	O
path	O
from	O
the	O
root	O
1	O
to	O
a	O
number	O
q	O
in	O
the	O
Stern	B-Data_Structure
–	I-Data_Structure
Brocot	I-Data_Structure
tree	I-Data_Structure
may	O
be	O
found	O
by	O
a	O
binary	O
search	O
algorithm	O
,	O
which	O
may	O
be	O
expressed	O
in	O
a	O
simple	O
way	O
using	O
mediants	O
.	O
</s>
<s>
The	O
sequence	O
of	O
values	O
M	O
computed	O
by	O
this	O
search	O
is	O
exactly	O
the	O
sequence	O
of	O
values	O
on	O
the	O
path	O
from	O
the	O
root	O
to	O
q	O
in	O
the	O
Stern	B-Data_Structure
–	I-Data_Structure
Brocot	I-Data_Structure
tree	I-Data_Structure
.	O
</s>
<s>
Each	O
open	O
interval	O
(	O
L	O
,	O
H	O
)	O
occurring	O
at	O
some	O
step	O
in	O
the	O
search	O
is	O
the	O
interval	O
(	O
LM	O
,	O
HM	O
)	O
representing	O
the	O
descendants	O
of	O
the	O
mediant	O
M	O
.	O
The	O
parent	O
of	O
q	O
in	O
the	O
Stern	B-Data_Structure
–	I-Data_Structure
Brocot	I-Data_Structure
tree	I-Data_Structure
is	O
the	O
last	O
mediant	O
found	O
that	O
is	O
not	O
equal	O
to	O
q	O
.	O
</s>
<s>
This	O
binary	O
search	O
procedure	O
can	O
be	O
used	O
to	O
convert	O
floating-point	B-Algorithm
numbers	I-Algorithm
into	O
rational	O
numbers	O
.	O
</s>
<s>
By	O
stopping	O
once	O
the	O
desired	O
precision	O
is	O
reached	O
,	O
floating-point	B-Algorithm
numbers	I-Algorithm
can	O
be	O
approximated	O
to	O
arbitrary	O
precision	O
.	O
</s>
<s>
The	O
Stern	B-Data_Structure
–	I-Data_Structure
Brocot	I-Data_Structure
tree	I-Data_Structure
may	O
itself	O
be	O
defined	O
directly	O
in	O
terms	O
of	O
mediants	O
:	O
the	O
left	O
child	O
of	O
any	O
number	O
q	O
is	O
the	O
mediant	O
of	O
q	O
with	O
its	O
closest	O
smaller	O
ancestor	O
,	O
and	O
the	O
right	O
child	O
of	O
q	O
is	O
the	O
mediant	O
of	O
q	O
with	O
its	O
closest	O
larger	O
ancestor	O
.	O
</s>
<s>
The	O
Farey	O
sequence	O
of	O
order	O
n	O
is	O
the	O
sorted	O
sequence	O
of	O
fractions	O
in	O
the	O
closed	O
interval	O
 [ 0 , 1 ] 	O
that	O
have	O
denominator	O
less	O
than	O
or	O
equal	O
to	O
n	O
.	O
As	O
in	O
the	O
binary	O
search	O
technique	O
for	O
generating	O
the	O
Stern	B-Data_Structure
–	I-Data_Structure
Brocot	I-Data_Structure
tree	I-Data_Structure
,	O
the	O
Farey	O
sequences	O
can	O
be	O
constructed	O
using	O
mediants	O
:	O
the	O
Farey	O
sequence	O
of	O
order	O
n+1	O
is	O
formed	O
from	O
the	O
Farey	O
sequence	O
of	O
order	O
n	O
by	O
computing	O
the	O
mediant	O
of	O
each	O
two	O
consecutive	O
values	O
in	O
the	O
Farey	O
sequence	O
of	O
order	O
n	O
,	O
keeping	O
the	O
subset	O
of	O
mediants	O
that	O
have	O
denominator	O
exactly	O
equal	O
to	O
n+1	O
,	O
and	O
placing	O
these	O
mediants	O
between	O
the	O
two	O
values	O
from	O
which	O
they	O
were	O
computed	O
.	O
</s>
<s>
A	O
similar	O
process	O
of	O
mediant	O
insertion	O
,	O
starting	O
with	O
a	O
different	O
pair	O
of	O
interval	O
endpoints	O
[0/1,1/0],	O
may	O
also	O
be	O
seen	O
to	O
describe	O
the	O
construction	O
of	O
the	O
vertices	O
at	O
each	O
level	O
of	O
the	O
Stern	B-Data_Structure
–	I-Data_Structure
Brocot	I-Data_Structure
tree	I-Data_Structure
.	O
</s>
<s>
The	O
Stern	O
–	O
Brocot	O
sequence	O
of	O
order	O
i	O
consists	O
of	O
all	O
values	O
at	O
the	O
first	O
i	O
levels	O
of	O
the	O
Stern	B-Data_Structure
–	I-Data_Structure
Brocot	I-Data_Structure
tree	I-Data_Structure
,	O
together	O
with	O
the	O
boundary	O
values	O
0/1	O
and	O
1/0	O
,	O
in	O
numerical	O
order	O
.	O
</s>
<s>
Thus	O
the	O
Stern	O
–	O
Brocot	O
sequences	O
differ	O
from	O
the	O
Farey	O
sequences	O
in	O
two	O
ways	O
:	O
they	O
eventually	O
include	O
all	O
positive	O
rationals	O
,	O
not	O
just	O
the	O
rationals	O
within	O
the	O
interval	O
[0,1],	O
and	O
at	O
the	O
nth	O
step	O
all	O
mediants	O
are	O
included	O
,	O
not	O
only	O
the	O
ones	O
with	O
denominator	O
equal	O
to	O
n	O
.	O
The	O
Farey	O
sequence	O
of	O
order	O
n	O
may	O
be	O
found	O
by	O
an	O
inorder	O
traversal	O
of	O
the	O
left	O
subtree	O
of	O
the	O
Stern	B-Data_Structure
–	I-Data_Structure
Brocot	I-Data_Structure
tree	I-Data_Structure
,	O
backtracking	O
whenever	O
a	O
number	O
with	O
denominator	O
greater	O
than	O
n	O
is	O
reached	O
.	O
</s>
<s>
Along	O
with	O
the	O
definitions	O
in	O
terms	O
of	O
continued	O
fractions	O
and	O
mediants	O
described	O
above	O
,	O
the	O
Stern	B-Data_Structure
–	I-Data_Structure
Brocot	I-Data_Structure
tree	I-Data_Structure
may	O
also	O
be	O
defined	O
as	O
a	O
Cartesian	B-Algorithm
tree	I-Algorithm
for	O
the	O
rational	O
numbers	O
,	O
prioritized	O
by	O
their	O
denominators	O
.	O
</s>
<s>
In	O
other	O
words	O
,	O
it	O
is	O
the	O
unique	O
binary	B-Language
search	I-Language
tree	I-Language
of	O
the	O
rational	O
numbers	O
in	O
which	O
the	O
parent	O
of	O
any	O
vertex	O
q	O
has	O
a	O
smaller	O
denominator	O
than	O
q	O
(	O
or	O
if	O
q	O
and	O
its	O
parent	O
are	O
both	O
integers	O
,	O
in	O
which	O
the	O
parent	O
is	O
smaller	O
than	O
q	O
)	O
.	O
</s>
<s>
It	O
follows	O
from	O
the	O
theory	O
of	O
Cartesian	B-Algorithm
trees	I-Algorithm
that	O
the	O
lowest	O
common	O
ancestor	O
of	O
any	O
two	O
numbers	O
q	O
and	O
r	O
in	O
the	O
Stern	B-Data_Structure
–	I-Data_Structure
Brocot	I-Data_Structure
tree	I-Data_Structure
is	O
the	O
rational	O
number	O
in	O
the	O
closed	O
interval	O
 [ q , r ] 	O
that	O
has	O
the	O
smallest	O
denominator	O
among	O
all	O
numbers	O
in	O
this	O
interval	O
.	O
</s>
<s>
Permuting	O
the	O
vertices	O
on	O
each	O
level	O
of	O
the	O
Stern	B-Data_Structure
–	I-Data_Structure
Brocot	I-Data_Structure
tree	I-Data_Structure
by	O
a	O
bit-reversal	B-Algorithm
permutation	I-Algorithm
produces	O
a	O
different	O
tree	O
,	O
the	O
Calkin	B-Data_Structure
–	I-Data_Structure
Wilf	I-Data_Structure
tree	I-Data_Structure
,	O
in	O
which	O
the	O
children	O
of	O
each	O
number	O
a/b	O
are	O
the	O
two	O
numbers	O
a/	O
( a+b	O
)	O
and	O
(	O
a+b	O
)	O
/b	O
.	O
</s>
<s>
Like	O
the	O
Stern	B-Data_Structure
–	I-Data_Structure
Brocot	I-Data_Structure
tree	I-Data_Structure
,	O
the	O
Calkin	B-Data_Structure
–	I-Data_Structure
Wilf	I-Data_Structure
tree	I-Data_Structure
contains	O
each	O
positive	O
rational	O
number	O
exactly	O
once	O
,	O
but	O
it	O
is	O
not	O
a	O
binary	B-Language
search	I-Language
tree	I-Language
.	O
</s>
