NAME
Math::PlanePath::RationalsTree  rationals by tree
SYNOPSIS
use Math::PlanePath::RationalsTree;
my $path = Math::PlanePath::RationalsTree>new (tree_type => 'SB');
my ($x, $y) = $path>n_to_xy (123);
DESCRIPTION
This path enumerates reduced rational fractions X/Y > 0, ie. X and Y having no common factor.
The rationals are traversed by rows of a binary tree which represents a coprime pair X,Y by steps of a subtractiononly greatest common divisor algorithm which proves them coprime. Or equivalently by bit runs with lengths which are the quotients in the divisionbased Euclidean GCD algorithm and which are also the terms in the continued fraction representation of X/Y.
The SB, CW, AYT, HCS, Bird and Drib trees all have the same set of X/Y rationals in a row, but in a different order due to different encodings of the N value. See the author's mathematical writeup for a proof that these are the only trees with a fixed set of matrices.
The bit runs mean that N values are quite large for relatively modest sized rationals. For example in the SB tree 167/3 is N=288230376151711741, a 58bit number. The tendency is for the tree to make excursions out to large rationals while only slowly filling in small ones. The worst is the integer X/1 for which N has X many bits, and similarly 1/Y is Y bits.
See examples/rationalstree.pl for a printout of all the trees.
SternBrocot Tree
The default tree_type=>"SB"
is the tree of Moritz Stern and Achille Brocot.
depth N
 
0 1 1/1
 
1 2 to 3 1/2 2/1
/ \ / \
2 4 to 7 1/3 2/3 3/2 3/1
       
3 8 to 15 1/4 2/5 3/5 3/4 4/3 5/3 5/2 4/1
Within a row the fractions increase in value. Each row of the tree is a repeat of the previous row as first X/(X+Y) and then (X+Y)/Y. For example
depth=1 1/2, 2/1
depth=2 1/3, 2/3 X/(X+Y) of previous row
3/2, 3/1 (X+Y)/Y of previous row
Plotting the N values by X,Y is as follows. The unused X,Y positions are where X and Y have a common factor. For example X=6,Y=2 has common factor 2 so is never reached.
tree_type => "SB"
10  512 35 44 767
9  256 33 39 40 46 383 768
8  128 18 21 191 384
7  64 17 19 20 22 95 192 49 51
6  32 47 96
5  16 9 10 23 48 25 26 55
4  8 11 24 27 56
3  4 5 12 13 28 29 60
2  2 6 14 30 62
1  1 3 7 15 31 63 127 255 511 1023
Y=0 

X=0 1 2 3 4 5 6 7 8 9 10
The X=1 vertical is the fractions 1/Y which is at the left of each tree row, at N value
Nstart = 2^depth
The Y=1 horizontal is the X/1 integers at the end each row which is
Nend = 2^(depth+1)1
Numbering nodes of the tree by rows starting from 1 means N without the high 1 bit is the offset into the row. For example binary N="1011" is "011"=3 into the row. Those bits after the high 1 are also the directions to follow down the tree to a node, with 0=left and 1=right. So N="1011" binary goes from the root 0=left then twice 1=right to reach X/Y=3/4 at N=11 decimal.
SternBrocot Mediant
Writing the parents between the children as an "inorder" tree traversal to a given depth has all values in increasing order (the same as each row individually is in increasing order).
1/1
1/2  2/1
1/3  2/3  3/2  3/1
      
1/3 1/2 2/3 1/1 3/2 2/1 3/1
^

next level (1+3)/(1+2) = 4/3 mediant
New values at the next level of this flattening are a "mediant" (x1+x2)/(y1+y2) formed from the left and right parent. So the next level 4/3 shown is left parent 1/1 and right parent 3/2 giving mediant (1+3)/(1+2)=4/3. At the left end a preceding 0/1 is imagined. At the right end a following 1/0 is imagined, so as to have 1/(depth+1) and (depth+1)/1 at the ends for a total 2^depth many new values.
The turn sequence left or right along the row depth >= 2 is by a repeating LRRL pattern, except the first and last are always R. (See the author's mathematical writeup for details.)
RRRL,LRRL,LRRL,LRRL,LRRL,LRRL,LRRL,LRRR # row N=32 to N=63
CalkinWilf Tree
tree_type=>"CW"
selects the tree of Calkin and Wilf,
Neil Calkin and Herbert Wilf, "Recounting the Rationals", American Mathematical Monthly, volume 107, number 4, April 2000, pages 360363.
http://www.math.upenn.edu/~wilf/reprints.html, http://www.math.upenn.edu/~wilf/website/recounting.pdf, http://www.jstor.org/stable/2589182
As noted above, the values within each row are the same as the SternBrocot, but in a different order.
N=1 1/1
 
N=2 to N=3 1/2 2/1
/ \ / \
N=4 to N=7 1/3 3/2 2/3 3/1
       
N=8 to N=15 1/4 4/3 3/5 5/2 2/5 5/3 3/4 4/1
Going by rows the denominator of one value becomes the numerator of the next. So at 4/3 the denominator 3 becomes the numerator of 3/5 to the right. These values are Stern's diatomic sequence.
Each row is symmetric in reciprocals, ie. reading from right to left is the reciprocals of reading left to right. The numerators read left to right are the denominators read right to left.
A node descends as
X/Y
/ \
X/(X+Y) (X+Y)/Y
Taking these formulas in reverse up the tree shows how it relates to a subtractiononly greatest common divisor. At a given node the smaller of P or Q is subtracted from the bigger,
P/(QP) (PQ)/P
/ or \
P/Q P/Q
Plotting the N values by X,Y is as follows. The X=1 vertical and Y=1 horizontal are the same as the SB above, but the values in between are reordered.
tree_type => "CW"
10  512 56 38 1022
9  256 48 60 34 46 510 513
8  128 20 26 254 257
7  64 24 28 18 22 126 129 49 57
6  32 62 65
5  16 12 10 30 33 25 21 61
4  8 14 17 29 35
3  4 6 9 13 19 27 39
2  2 5 11 23 47
1  1 3 7 15 31 63 127 255 511 1023
Y=0 

X=0 1 2 3 4 5 6 7 8 9 10
At each node the left leg is X/(X+Y) < 1 and the right leg is (X+Y)/Y > 1, which means N is even above the X=Y diagonal and odd below. In general each right leg increments the integer part of the fraction,
X/Y right leg each time
(X+Y)/Y = 1 + X/Y
(X+2Y)/Y = 2 + X/Y
(X+3Y)/Y = 3 + X/Y
etc
This means the integer part is the trailing 1bits of N,
floor(X/Y) = count trailing 1bits of N
eg. 7/2 is at N=23 binary "10111"
which has 3 trailing 1bits for floor(7/2)=3
N values for the SB and CW trees are converted by reversing bits except the highest. So at a given X,Y position
SB N = 1abcde SB <> CW by reversing bits
CW N = 1edcba except the high 1bit
For example at X=3,Y=4 the SB tree has N=11 = "1011" binary and the CW has N=14 binary "1110", a reversal of the bits below the high 1.
N to X/Y in the CW tree can be calculated keeping track of just an X,Y pair and descending to X/(X+Y) or (X+Y)/Y using the bits of N from high to low. The relationship between the SB and CW N's means the same can be used to calculate the SB tree by taking the bits of N from low to high instead.
See also Math::PlanePath::ChanTree for a generalization of CW to ternary or higher trees, ie. descending to 3 or more children at each node.
YuTing and Andreev Tree
tree_type=>"AYT"
selects the tree described independently by YuTing and Andreev.
Shen YuTing, "A Natural Enumeration of NonNegative Rational Numbers  An Informal Discussion", American Mathematical Monthly, 87, 1980, pages 2529. http://www.jstor.org/stable/2320374
D. N. Andreev, "On a Wonderful Numbering of Positive Rational Numbers", Matematicheskoe Prosveshchenie, Series 3, volume 1, 1997, pages 126134 http://mi.mathnet.ru/mp12
Their constructions are a onetoone mapping between integer N and rational X/Y as a way of enumerating the rationals. This is not designed to be a tree as such, but the result is the same 2^level rows as the above trees. The X/Y values within each row are the same, but in a different order.
N=1 1/1
 
N=2 to N=3 2/1 1/2
/ \ / \
N=4 to N=7 3/1 1/3 3/2 2/3
       
N=8 to N=15 4/1 1/4 4/3 3/4 5/2 2/5 5/3 3/5
Each fraction descends as follows. The left is an increment and the right is reciprocal of the increment.
X/Y
/ \
X/Y + 1 1/(X/Y + 1)
which means
X/Y
/ \
(X+Y)/Y Y/(X+Y)
The left leg (X+Y)/Y is the same the CW has on its right leg. But Y/(X+Y) is not the same as the CW (the other there being X/(X+Y)).
The left leg increments the integer part, so the integer part is given by (in a fashion similar to CW 1bits above)
floor(X/Y) = count trailing 0bits of N
plus one extra if N=2^k
N=2^k is one extra because its trailing 0bits started from N=1 where floor(1/1)=1 whereas any other odd N starts from some floor(X/Y)=0.
The Y/(X+Y) right leg forms the Fibonacci numbers F(k)/F(k+1) at the end of each row, ie. at Nend=2^(level+1)1. And as noted by Andreev, successive right leg fractions N=4k+1 and N=4k+3 add up to 1,
X/Y at N=4k+1 + X/Y at N=4k+3 = 1
Eg. 2/5 at N=13 and 3/5 at N=15 add up to 1
Plotting the N values by X,Y gives
tree_type => "AYT"
10  513 41 43 515
9  257 49 37 39 51 259 514
8  129 29 31 131 258
7  65 25 21 23 27 67 130 50 42
6  33 35 66
5  17 13 15 19 34 26 30 38
4  9 11 18 22 36
3  5 7 10 14 20 28 40
2  3 6 12 24 48
1  1 2 4 8 16 32 64 128 256 512
Y=0 

X=0 1 2 3 4 5 6 7 8 9 10
N=1,2,4,8,etc on the Y=1 horizontal is the X/1 integers at Nstart=2^level=2^X. N=1,3,5,9,etc in the X=1 vertical is the 1/Y fractions. Those fractions always immediately follow the corresponding integer, so N=Nstart+1=2^(Y1)+1 in that column.
In each node the left leg (X+Y)/Y > 1 and the right leg Y/(X+Y) < 1, which means odd N is above the X=Y diagonal and even N is below.
The tree structure corresponds to Johannes Kepler's tree of fractions (see Math::PlanePath::FractionsTree). That tree starts from 1/2 and makes fractions A/B with A<B by descending to A/(A+B) and B/(A+B). Those descents are the same as the AYT tree and the two are related simply by
A = Y AYT denominator is Kepler numerator
B = X+Y AYT sum num+den is the Kepler denominator
X = BA inverse
Y = A
HCS Continued Fraction
tree_type=>"HCS"
selects continued fraction terms coded as bit runs 1000...00 from high to low, as per Paul D. Hanna and independently Czyz and Self.
Jerzy Czyz and William Self, "The Rationals Are Countable: Euclid's Proof", The College Mathematics Journal, volume 34, number 5, November 2003, page 367. http://www.jstor.org/stable/3595818
http://www.cuttheknot.org/do_you_know/countRatsCF.shtml, http://www.dm.unito.it/~cerruti/dochtml/tremattine/tre_mattine.pdf
This arises also in a radix=1 variation of Jeffrey Shallit's digitbased continued fraction encoding. See "Radix 1" in Math::PlanePath::CfracDigits.
If the continued fraction of X/Y is
1
X/Y = a +  a >= 0
1
b +  b,c,etc >= 1
1
c + 
... + 1
 z >= 2
z
then the N value is bit runs of lengths a,b,c etc.
N = 1000 1000 1000 ... 1000
\/ \/ \/ \/
a+1 b c z1
Each group is 1 or more bits. The +1 in "a+1" makes the first group 1 or more bits, since a=0 occurs for any X/Y<=1. The 1 in "z1" makes the last group 1 or more since z>=2.
N=1 1/1
 
N=2 to N=3 2/1 1/2
/ \ / \
N=4 to N=7 3/1 3/2 1/3 2/3
       
N=8 to N=15 4/1 5/2 4/3 5/3 1/4 2/5 3/4 3/5
The result is a bit reversal of the N values in the AYT tree.
AYT N = binary "1abcde" AYT <> HCS bit reversal
HCS N = binary "1edcba"
For example at X=4,Y=7 the AYT tree is N=11 binary "10111" whereas HCS there has N=30 binary "11110", a reversal of the bits below the high 1.
Plotting by X,Y gives
tree_type => "HCS"
10  768 50 58 896
9  384 49 52 60 57 448 640
8  192 27 31 224 320
7  96 25 26 30 29 112 160 41 42
6  48 56 80
5  24 13 15 28 40 21 23 44
4  12 14 20 22 36
3  6 7 10 11 18 19 34
2  3 5 9 17 33
1  1 2 4 8 16 32 64 128 256 512
Y=0 
+
X=0 1 2 3 4 5 6 7 8 9 10
N=1,2,4,etc in the row Y=1 are powersof2, being integers X/1 having just a single group of bits N=1000..000.
N=1,3,6,12,etc in the column X=1 are 3*2^(Y1) corresponding to continued fraction 0 + 1/Y so terms 0,Y making runs 1,Y1 and so bits N=11000...00.
The turn sequence left or right following successive X,Y points is the ThueMorse sequence. A proof of this can be found in the author's mathematical writeup (above).
count 1bits in N+1 turn at N
 
odd right
even left
Bird Tree
tree_type=>"Bird"
selects the Bird tree,
Ralf Hinze, "Functional Pearls: The Bird tree", Journal of Functional Programming, volume 19, issue 5, September 2009, pages 491508. DOI 10.1017/S0956796809990116 http://www.cs.ox.ac.uk/ralf.hinze/publications/Bird.pdf
It's expressed recursively, illustrating Haskell programming features. The left subtree is the tree plus one and take the reciprocal. The right subtree is conversely the reciprocal first then add one,
1 1
 and  + 1
tree + 1 tree
which means Y/(X+Y) and (X+Y)/X taking N bits low to high.
N=1 1/1
 
N=2 to N=3 1/2 2/1
/ \ / \
N=4 to N=7 2/3 1/3 3/1 3/2
       
N=8 to N=15 3/5 3/4 1/4 2/5 5/2 4/1 4/3 5/3
Plotting by X,Y gives
tree_type => "Bird"
10  682 41 38 597
9  341 43 45 34 36 298 938
8  170 23 16 149 469
7  85 20 22 17 19 74 234 59 57
6  42 37 117
5  21 11 8 18 58 28 31 61
4  10 9 29 30 50
3  5 4 14 15 25 24 54
2  2 7 12 27 52
1  1 3 6 13 26 53 106 213 426 853
Y=0 

X=0 1 2 3 4 5 6 7 8 9 10
Notice that unlike the other trees N=1,2,5,10,etc in the X=1 vertical for fractions 1/Y is not the row start or end, but instead are on a zigzag through the middle of the tree binary N=1010...etc alternate 1 and 0 bits. The integers X/1 in the Y=1 vertical are similar, but N=11010...etc starting the alternation from a 1 in the second highest bit, since those integers are in the right hand half of the tree.
The Bird tree N values are related to the SB tree by inverting every second bit starting from the second after the high 1bit,
Bird N=1abcdefg.. binary
101010.. xor, so b,d,f etc flip 0<>1
SB N=1aBcDeFg.. to make B,D,F
For example 3/4 in the SB tree is at N=11 = binary 1011. Xor with 0010 for binary 1001 N=9 which is 3/4 in the Bird tree. The same xor goes back the other way Bird tree to SB tree.
This xoring is a mirroring in the tree, swapping left and right at each level. Only every second bit is inverted because mirroring twice puts it back to the ordinary way on even rows.
Drib Tree
tree_type=>"Drib"
selects the Drib tree by Ralf Hinze.
It reverses the bits of N in the Bird tree (in a similar way that the SB and CW are bit reversals of each other).
N=1 1/1
 
N=2 to N=3 1/2 2/1
/ \ / \
N=4 to N=7 2/3 3/1 1/3 3/2
       
N=8 to N=15 3/5 5/2 1/4 4/3 3/4 4/1 2/5 5/3
The descendants of each node are
X/Y
/ \
Y/(X+Y) (X+Y)/X
The endmost fractions of each row are Fibonacci numbers, F(k)/F(k+1) on the left and F(k+1)/F(k) on the right.
tree_type => "Drib"
10  682 50 44 852
9  426 58 54 40 36 340 683
8  170 30 16 212 427
7  106 18 22 24 28 84 171 59 51
6  42 52 107
5  26 14 8 20 43 19 31 55
4  10 12 27 23 41
3  6 4 11 15 25 17 45
2  2 7 9 29 37
1  1 3 5 13 21 53 85 213 341 853
Y=0 

X=0 1 2 3 4 5 6 7 8 9 10
In each node descent the left Y/(X+Y) < 1 and the right (X+Y)/X > 1, which means even N is above the X=Y diagonal and odd N is below.
Because Drib/Bird are bit reversals like CW/SB are bit reversals, the xor procedure described above which relates Bird<>SB applies to Drib<>CW, but working from the second lowest bit upwards, ie. xor binary "0..01010". For example 4/1 is at N=15 binary 1111 in the CW tree. Xor with 0010 for 1101 N=13 which is 4/1 in the Drib tree.
L Tree
tree_type=>"L"
selects the Ltree by Peter Luschny.
It's a rowreversal of the CW tree with a shift to include zero as 0/1.
N=0 0/1
 
N=1 to N=2 1/2 1/1
/ \ / \
N=3 to N=8 2/3 3/2 1/3 2/1
       
N=9 to N=16 3/4 5/3 2/5 5/2 3/5 4/3 1/4 3/1
Notice in the N=9 to N=16 row rationals 3/4 to 1/4 are the same as in the CW tree but read righttoleft.
tree_type => "L"
10  1021 37 55 511
9  509 45 33 59 47 255 1020
8  253 25 19 127 508
7  125 21 17 27 23 63 252 44 36
6  61 31 124
5  29 9 11 15 60 20 24 32
4  13 7 28 16 58
3  5 3 12 8 26 18 54
2  1 4 10 22 46
1  0 2 6 14 30 62 126 254 510 1022 2046
Y=0 

X=0 1 2 3 4 5 6 7 8 9 10
N=0,2,6,14,30,etc along the row at Y=1 are powers 2^(X+1)2. N=1,5,13,29,etc in the column at X=1 are similar powers 2^Y3.
Common Characteristics
The SB, CW, Bird, Drib, AYT and HCS trees have the same set of rationals in each row, just in different orders. The properties of Stern's diatomic sequence mean that within a row the totals are
row N=2^depth to N=2^(depth+1)1 inclusive
sum X/Y = (3 * 2^depth  1) / 2
sum X = 3^depth
sum 1/(X*Y) = 1
For example the SB tree depth=2, N=4 to N=7,
sum X/Y = 1/3 + 2/3 + 3/2 + 3/1 = 11/2 = (3*2^21)/2
sum X = 1+2+3+3 = 9 = 3^2
sum 1/(X*Y) = 1/(1*3) + 1/(2*3) + 1/(3*2) + 1/(3*1) = 1
Many permutations are conceivable within a row, but the ones here have some relationship to X/Y descendants, tree subforms or continued fractions. As an encoding of continued fraction terms by bit runs the combinations are
bit encoding high to low low to high
  
0000 1111 runs SB CW
0101 1010 alternating Bird Drib
1000 1000 runs HCS AYT
A run of alternating 101010 ends where the next bit is the oppose of the expected alternating 0,1. This is a doubled bit 00 or 11. An electrical engineer would think of it as a phase shift.
Minkowski Question Mark
The Minkowski question mark function is a sum of the terms in the continued fraction representation of a real number. If q0,q1,q2,etc are those terms then the question mark function "?(r)" is
1 1 1
?(r) = 2 * (1   * (1   * (1   * (1  ...
2^q0 2^q1 2^q2
1 1 1
= 2 * (1   +    + ... )
2^q0 2^(q0+q1) 2^(q0+q1+q2)
For rational r the continued fraction q0,q1,q2,etc is finite and so the ?(r) sum is finite and rational. The pattern of + and  in the terms gives runs of bits the same as the N values in the SternBrocot tree. The RationalsTree code can calculate the ?(r) function by
rational r=X/Y
N = xy_to_n(X,Y) tree_type=>"SB"
depth = floor(log2(N)) # row containing N (depth=0 at top)
Ndepth = 2^depth # start of row containing N
2*(NNdepth) + 1
?(r) = 
Ndepth
The effect of NNdepth is to remove the high 1bit, leaving an offset into the row. 2*(..)+1 appends an extra 1bit at the end. The division by Ndepth scales down from integer N to a fraction.
N = 1abcdef integer, in binary
?(r) = a.bcdef1 binary fraction
For example ?(2/3) is X=2,Y=3 which is N=5 in the SB tree. It is at depth=2, Ndepth=2^2=4, and so ?(2/3)=(2*(54)+1)/4=3/4. Or written in binary N=101 gives Ndepth=100 and NNdepth=01 so 2*(NNdepth)+1=011 and divide by Ndepth=100 for ?=0.11.
In practice this is not a very efficient way to handle the question function, since the bit runs in the N values may become quite large for relatively modest fractions. (Math::ContinuedFraction may be better, and also allows repeating terms from quadratic irrationals to be represented exactly.)
Pythagorean Triples
Pythagorean triples A^2+B^2=C^2 can be generated by A=P^2Q^2, B=2*P*Q. If P>Q>1 with P,Q no common factor and one odd the other even then this gives all primitive triples, being primitive in the sense of A,B,C no common factor ("PQ Coordinates" in Math::PlanePath::PythagoreanTree).
In the CalkinWilf tree the parity of X,Y pairs are as follows. Pairs X,Y with one odd the other even are N=0 or 2 mod 3.
CW tree X/Y

N=0 mod 3 even/odd
N=1 mod 3 odd/odd
N=2 mod 3 odd/even
This occurs because the numerators are the Stern diatomic sequence and the denominators likewise but offset by 1. The Stern diatomic sequence is a repeating pattern even,odd,odd (eg. per "Odd and Even" in Math::NumSeq::SternDiatomic).
The X>Y pairs in the CW tree are the right leg of each node, which is N odd. so
CW tree N=3 or 5 mod 6 gives X>Y one odd the other even
index t=1,2,3,etc to enumerate such pairs
N = 3*t if t odd
3*t1 if t even
2 of each 6 points are used. In a given row it's width/3 but rounded up or down according to where the 3,5mod6 falls on the N=2^depth start, which is either floor or ceil according to depth odd or even,
NumPQ(depth) = floor(2^depth / 3) for depth=even
ceil (2^depth / 3) for depth=odd
= 0, 1, 1, 3, 5, 11, 21, 43, 85, 171, 341, ...
These are the Jacobsthal numbers, which in binary are 101010...101 and 1010...1011.
For the other tree types the various bit transformations which map N positions between the trees can be applied to the above N=3or5 mod 6. The simplest is the L tree where the N offset and row reversal gives N=0or4 mod 6.
The SB tree is a bit reversal of the CW, as described above, and for the Pythagorean N this gives
SB tree N=0 or 2 mod 2 and N="11...." in binary
gives X>Y one odd the other even
N="11..." binary is the bit reversal of the CW N=odd "1...1" condition. This bit pattern is those N in the second half of each row, which is where the X/Y > 1 rationals occur. The N=0or2 mod 3 condition is unchanged by the bit reversal. N=0or2 mod 3 precisely when bitreverse(N)=0or2 mod 3.
For SB whether it's odd/even or even/odd at N=0or2 mod 3 alternates between rows. The two are both wanted, they just happen to switch in each row.
SB tree X/Y depth=even depth=odd
 
N=0 mod 3 odd/even even/odd
N=1 mod 3 odd/odd odd/odd < exclude for Pythagorean
N=2 mod 3 even/odd odd/even
FUNCTIONS
See "FUNCTIONS" in Math::PlanePath for behaviour common to all path classes.
$path = Math::PlanePath::RationalsTree>new ()
$path = Math::PlanePath::RationalsTree>new (tree_type => $str)

Create and return a new path object.
tree_type
(a string) can be"SB" SternBrocot "CW" CalkinWilf "AYT" YuTing, Andreev "HCS" "Bird" "Drib" "L"
$n = $path>n_start()

Return the first N in the path. This is 1 for SB, CW, AYT, HCS, Bird and Drib, but 0 for L.
($n_lo, $n_hi) = $path>rect_to_n_range ($x1,$y1, $x2,$y2)

Return a range of N values which occur in a rectangle with corners at
$x1
,$y1
and$x2
,$y2
. The range is inclusive.For reference,
$n_hi
can be quite large because within each row there's only one new X/1 integer and 1/Y fraction. So if X=1 or Y=1 is included then roughly$n_hi = 2**max(x,y)
. If min(x,y) is bigger than 1 then it reduces a little to roughly 2**(max/min + min).
Tree Methods
Each point has 2 children, so the path is a complete binary tree.
@n_children = $path>tree_n_children($n)

Return the two children of
$n
, or an empty list if$n < 1
(ie. before the start of the path).This is simply
2*$n, 2*$n+1
. Written in binary the children are$n
with an extra bit appended, a 0bit or a 1bit. $num = $path>tree_n_num_children($n)

Return 2, since every node has two children. If
$n<1
, ie. before the start of the path, then returnundef
. $n_parent = $path>tree_n_parent($n)

Return the parent node of
$n
. Or returnundef
if$n <= 1
(the top of the tree).This is simply Nparent = floor(N/2), ie. strip the least significant bit from
$n
. (Undo whattree_n_children()
appends.) $depth = $path>tree_n_to_depth($n)

Return the depth of node
$n
, orundef
if there's no point$n
. The top of the tree at N=1 is depth=0, then its children depth=1, etc.This is simply floor(log2(N)) since the tree has 2 nodes per point. For example N=4 through N=7 are all depth=2.
The L tree starts at N=0 and the calculation becomes floor(log2(N+1)) there.
$n = $path>tree_depth_to_n($depth)
$n = $path>tree_depth_to_n_end($depth)

Return the first or last N at tree level
$depth
in the path, orundef
if nothing at that depth or not a tree. The top of the tree is depth=0.The structure of the tree means the first N is at
2**$depth
, or for the L tree2**$depth  1
. The last N is2**($depth+1)1
, or for the L tree2**($depth+1)
.
Tree Descriptive Methods
$num = $path>tree_num_children_minimum()
$num = $path>tree_num_children_maximum()

Return 2 since every node has 2 children so that's both the minimum and maximum.
$bool = $path>tree_any_leaf()

Return false, since there are no leaf nodes in the tree.
OEIS
The trees are in Sloane's Online Encyclopedia of Integer Sequences in various forms,
http://oeis.org/A007305 (etc)
tree_type=SB
A007305 X, extra initial 0,1
A047679 Y
A057431 X,Y pairs (initial extra 0/1,1/0)
A007306 X+Y sum, Farey 0 to 1 part (extra 1,1)
A153036 int(X/Y), integer part
A088696 length of continued fraction SB left half (X/Y<1)
tree_type=CW
A002487 X and Y, Stern diatomic sequence (extra 0)
A070990 YX diff, Stern diatomic first diffs (less 0)
A070871 X*Y product
A007814 int(X/Y), integer part, count trailing 1bits
which is count trailing 0bits of N+1
A086893 N position of Fibonacci F[n+1]/F[n], N = binary 1010..101
A061547 N position of Fibonacci F[n]/F[n+1], N = binary 11010..10
A047270 3or5 mod 6, being N positions of X>Y not both odd
which can generate primitive Pythagorean triples
tree_type=AYT
A020650 X
A020651 Y (Kepler numerator)
A086592 X+Y sum (Kepler denominator)
A135523 int(X/Y), integer part,
count trailing 0bits plus 1 extra if N=2^k
tree_type=HCS
A229742 X, extra initial 0/1
A071766 Y
A071585 X+Y sum
tree_type=Bird
A162909 X
A162910 Y
A081254 N of row Y=1, N = binary 1101010...10
A000975 N of column X=1, N = binary 101010...10
tree_type=Drib
A162911 X
A162912 Y
A086893 N of row Y=1, N = binary 110101..0101 (ending 1)
A061547 N of column X=1, N = binary 110101..010 (ending 0)
tree_type=L
A174981 X
A002487 Y, same as CW X,Y, Stern diatomic
A047233 0or4 mod 6, being N positions of X>Y not both odd
which can generate primitive Pythagorean triples
tree_type=SB,CW,AYT,HCS,Bird,Drib,L
A008776 total X+Y in row, being 2*3^depth
A000523 tree_n_to_depth(), being floor(log2(N))
A059893 permutation SB<>CW, AYT<>HCS, Bird<>Drib
reverse bits below highest
A258746 permutation SB<>Bird,
flip every second bit, from 3rd highest downward
A003188 permutation SB>HCS, Gray code shift+xor
A006068 permutation HCS>SB, Gray code inverse
A154435 permutation HCS>Bird, Lamplighter bit flips
A154436 permutation Bird>HCS, Lamplighter variant
A258996 permutation CW<>Drib, phase shift code high to low
A153153 permutation CW>AYT, reverse and unGray
A153154 permutation AYT>CW, reverse and Gray code
A154437 permutation AYT>Drib, Lamplighter low to high
A154438 permutation Drib>AYT, unLamplighter low to high
A054429 permutation SB,CW,Bird,Drib N at transpose Y/X,
(mirror binary tree, runs 0b11..11 down to 0b10..00)
A004442 permutation AYT N at transpose Y/X, from N=2 onwards
(xor 1, ie. flip least significant bit)
A063946 permutation HCS N at transpose Y/X, extra initial 0
(xor 2, ie. flip second least significant bit)
A054424 permutation DiagonalRationals > SB
A054426 permutation SB > DiagonalRationals
A054425 DiagonalRationals > SB with 0s at noncoprimes
A054427 permutation coprimes > SB right hand X/Y>1
A044051 N+1 of those N where SB and CW have same X,Y
same Bird<>Drib and HCS<>AYT
begin N+1 of N binary palindrome below high 1bit
The sequences marked "extra ..." have one or two extra initial values over what the RationalsTree here gives, but are the same after that. And the Stern first differences "less ..." means it has one less term than what the code here gives.
SEE ALSO
Math::PlanePath, Math::PlanePath::FractionsTree, Math::PlanePath::CfracDigits, Math::PlanePath::ChanTree
Math::PlanePath::CoprimeColumns, Math::PlanePath::DiagonalRationals, Math::PlanePath::FactorRationals, Math::PlanePath::GcdRationals, Math::PlanePath::PythagoreanTree
Math::NumSeq::SternDiatomic, Math::ContinuedFraction
HOME PAGE
http://user42.tuxfamily.org/mathplanepath/index.html
LICENSE
Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Kevin Ryde
This file is part of MathPlanePath.
MathPlanePath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version.
MathPlanePath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with MathPlanePath. If not, see <http://www.gnu.org/licenses/>.