Nicolas Gast
email: nicolas.gast@imag.fr

Bruno Gaujal
email: bruno.gaujal@imag.fr

Infinite Labeled Trees: from Rational to Sturmian Trees

Keywords: Infinite trees, Sturmian words, Sturmian trees

This paper studies infinite unordered d-ary trees with nodes labeled by {0, 1}. We introduce the notions of rational and Sturmian trees along with the definitions of (strongly) balanced trees and mechanical trees, and study the relations among them.

In particular, we show that (strongly) balanced trees exist and coincide with mechanical trees in the irrational case, providing an effective construction. Such trees also have a minimal factor complexity, hence are Sturmian. We also give several examples illustrating the inclusion relations between these classes of trees.

Introduction

Let us consider the following question: how to distribute ones and zeros over an infinite sequence w = (w n ) n∈N such that the ones (and the zeros) are spread as evenly as possible. In a more formal way, the sequence w is balanced if the number of ones in a factor w i , . . . , w i+ -1 of length , does not vary by more than 1, for all i and all . Such sequences exist and are called Sturmian words when they are not periodic.

Sturmian words are quite fascinating binary sequences: they have many different characterizations formulated in terms coming from as many mathematical frameworks, in which they always prove very useful. For example, Sturmian words have a geometric description as digitalized straight lines and as such have been used in computer visualization (see [START_REF] Klette | Digital straightness-a review[END_REF]) for a review). They can also be defined with an arithmetic characterization using a repetitive rotation on a torus or continued fraction decompositions. From a combinatorial point of view, yet another characterization of Sturmian words is based on the balance between ones and zeros in all factors, as mentioned before. They are also used in symbolic dynamic system theory because they are aperiodic words with minimal factor complexity or because they have palindromic properties. Most of these equivalences have been known since the seminal work in [START_REF] Morse | Symbolic dynamics ii. sturmian trajectories[END_REF]. More recently, Sturmian sequences have also been used for optimization purposes: they are extreme points of multimodular functions [START_REF] Hajek | Extremal splittings of point processes[END_REF][START_REF] Altman | Discrete-Event Control of Stochastic Networks: Multimodularity and Regularity[END_REF][START_REF] Gaujal | Optimal routing policy in two deterministic queues[END_REF] and this has applications is scheduling theory [START_REF] Gaujal | On the optimal open-loop control policy for deterministic and exponential polling systems[END_REF].

Since then, there have been several constructions of generalized Sturmian words in the literature.

The first one concerns words over more than two letters. Billiard sequences in hypercubes extent the torus definition of Sturmian sequences while episturmian sequences [START_REF] Berstel | Sturmian and episturmian words (a survey of some recent result results)[END_REF] extend the palindromic characterization of Sturmian words, however, the other characterizations of Sturmian words are lost in both cases. Another extension is to two dimensions. A complete characterization of two-dimensional non-periodic sequences with minimal complexity is given in [START_REF] Cassaigne | Double sequences with complexity mn+1[END_REF], here again the alternative characterizations are lost. Yet another extension of Sturmian words concerns discrete planes. Indeed, several characterizations of Sturmian lines can be extended to discrete planes. There exists interesting relations between multidimensional continued fraction decomposition of the normal direction of an hyperplane and the patterns of its discretization. These relations mimic what happens for Sturmian sequences, [START_REF] Fernique | Fractions continues et géométrie discrète[END_REF]. Finally, another generalization is to ordered trees [START_REF] Berstel | Sturmian trees. Theory of Computing Systems[END_REF], where Sturmian trees are defined as infinite binary automata such that the number of factors (subtrees) of size n is n + 1. The other characterizations of Sturmian words are lost once more.

The aim of this paper is to do the same for unordered trees where things work better in the sense that several extensions coincide. We introduced in [START_REF] Gast | Balanced labeled trees: density, complexity and mechanicity[END_REF] a new type of infinite trees: unordered labeled trees, for which the left and right children of each node are not distinguishable and gave a brief presentation of their main properties. Here, we make an exhaustive study of such trees. We show that the balance property (even distribution of the labels over the vertices of the tree) coincides with a characterization of trees using integer parts of affine functions (called mechanicity). Furthermore these strongly balanced trees have a minimal factor complexity. Therefore, they can be seen as a natural extension of Sturmian sequence in more than one aspect. This brings some hope to use them as extreme points for adapted optimization problems.

Our purpose in the paper is two-fold. The first part of the paper is dedicated to the study of general unordered infinite trees with binary labels. In section 2, we provide definitions of the main concepts as well as the basic properties of unordered trees with a special focus on the notion of density (the average number of ones) and rationality. Section 3 is dedicated to the study of the rational trees.

The second part of the paper investigates balanced unordered trees and their properties. In particular, we show that strongly balanced trees (defined in section 4) are mechanical (so that they have a density and all labels can be constructed in almost constant time). Furthermore their factor complexity is minimal among all non-periodic trees. We also investigate the general shape of strongly balanced rational trees (section 5). We show that there essentially exists a unique strongly balanced tree with a given rational density. Also, once a strongly balanced tree is given, its density is easy to compute and we provide an efficient algorithm with polynomial complexity to test whether a rational tree is strongly balanced. Finally, Section 6 presents several examples and counter examples that illustrate the different notions presented in the paper.

Infinite Trees

Ordered Infinite Trees or Tree-automata

Ordered infinite trees (also called tree-automata here) have been studied in [START_REF] Courcelle | Fundamental properties of infinite trees[END_REF][START_REF] Berstel | Sturmian trees. Theory of Computing Systems[END_REF]. Ordered infinite trees are automata with an infinite number of states. An automata is a tree-automaton if it has one initial state and each state has a uniform in-degree equal to one (except for the initial state, whose in-degree is 0) and a uniform out-degree d with labels a 1 , • • • , a d on the arcs. Every node v is labeled by (v) = 1 (resp. 0) if it is final (resp. non-final).

The language accepted by the tree-automaton T is a subset of A * (where the alphabet A = {a 1 , . . . a d }) and is denoted by L(T ). Thus, a word w in the free monoid A * corresponds to a node in T , and a word w in L(T ) corresponds to a node in T with label 1. Conversely, a unique tree-automaton can be associated to any subset L of A * , by labeling by one the nodes corresponding to the words in L.

Classically for automata, a family of equivalence relations can be defined over the nodes of tree

T : v ∼ 0 u if (v) = (u), v ∼ n+1 u if v ∼ n u
and for all i, the ith child of u, ua i and the ith child of v, va i satisfy ua i ∼ n va i . By definition of ∼ n , u ∼ n v if and only if the subtree rooted in u of height n is the same as the subtree rooted in v of height n.

L(T ) is recognized by its minimal deterministic automaton (possibly infinite), say A(T ). Actually, A(T ) can be obtained from the tree T by merging all the states in the tree in the same equivalence classe of ∼ n for all n.

An example is given in Figure 1 where the infinite tree-automaton and the minimal automaton recognizing all the prefixes of the Fibonacci1 word over the alphabet {a, b} is given together with the corresponding minimal automaton (which has an infinite number of states). There is a transition between nodes i and i + 1 labeled by the ith letter of f and one between nodes i and ∞ labeled by the opposite of this ith letter.

The number of distinct subtrees of height n in T is called the complexity P (n), of T . P (n) is the number of equivalence classes of ∼ n . If P (k) ≤ k for at least one k, then it can be shown [START_REF] Berstel | Sturmian trees. Theory of Computing Systems[END_REF] that the complexity P (n) is bounded by k. This implies that the minimal automaton A(T ) has less than k states. The tree is therefore rational, since it recognizes a rational language.

If a tree-automaton T is such that P (n) = n + 1 for all n, then it has a minimal complexity among all non-rational trees. Such trees have been shown to exist and are called Sturmian in [START_REF] Berstel | Sturmian trees. Theory of Computing Systems[END_REF] by analogy with the factor complexity definition of Sturmian words (Figure 1 gives an example). In [START_REF] Berstel | Sturmian trees. Theory of Computing Systems[END_REF] several classes of Sturmian tree-automata are presented. However such trees are not balanced and no constructive definition (as the mechanical construction for words) is known.

Unordered Trees and Minimal Graph

In this paper, we rather consider a different type of trees, namely infinite directed graphs with labels 0 or 1 on nodes and with uniform in-degree 1 and out-degree d ≥ 2. Up to our knowledge, these types of trees have not yet been considered in the literature. The similarities as well as the discrepancies with ordered trees will be discussed all along the paper.

In such trees, one node is special (with in-degree 0) and is called the root. Also, the children of a node are not ordered. Thus, the main difference with the previous type of trees is the fact that arcs are not labeled. Therefore such trees cannot be bijectively associated with languages.

We define the minimal multigraph (i.e. with multiple arcs) G(T ), associated with the tree T , mimicking the construction of the minimal automaton for ordered trees. To do that, we first introduce a family of equivalence relations ≡ n over the nodes of T :

• v ≡ 0 u if u and v have the same label: (u) = (v) • v ≡ n+1 u if v ≡ n u
and if there exists a bijection F between the children of v and the children of u such that for all child w of v, w ≡ n F (w).

Therefore, v ≡ n u if and only if the subtree with root v of height n is isomorphic to the subtree with root u of height n. By merging the nodes of T when they belong to the same equivalence classe ≡ n for all n, one gets the minimal multigraph G(T ) of the factors of T : all nodes merged in the same vertex of G(T ) are roots of the same subtrees, of every height. In G(T ), the node corresponding to the root of T is distinguished.

(graphically, this is done by adding an arrow pointing to the node). An example of an unordered tree T is given in Figure 2. Actually, most figures in this paper will represent binary trees (with out-degree d = 2), although all the discussion is carried throughout for arbitrary degrees. The nodes of the associated multigraph G(T ) are numbered arbitrarily and nodes with label 1 are displayed with a bold circle. The node corresponding to the root of the tree is pointed by an arrow.

There exists a way to associate an ordered tree-automaton T to a tree T by choosing an order on the children of each node. This can be done by seeing G(T ) as an automaton by labeling arcs in G(T ) with letters a 1 , . . . a d in an arbitrary fashion. Conversely, a tree-automaton T can be converted into a graph T by removing the labels on the arcs. This graph is called the unordered version of T . Figure 2 is the unordered version of the tree recognizing the Fibonacci word displayed in Figure 1. Note that while the minimal automaton is infinite, the minimal graph G(T ) is finite, with only two nodes; one corresponds to the subtree where all labels are 0 and the other one to the subtree with a branch with label 1 everywhere and all the other nodes with label 0 (Figure 2).

Irreducibility and periodicity

By analogy with Markov chains, we say that a tree T is irreducible if G(T ) is strongly connected.

A non-irreducible tree, G(T ) is made of strongly connected components , inter-connected by an acyclic graph. Also, an irreducible tree T is periodic with period p if the greatest common divisor of the lengths of all cycles in G(T ) is p. A tree with period 1 is also called aperiodic.

Factors, complexity and Sturmian trees

In this paper, we will study properties of factors of infinite trees. For this purpose, we introduce two definitions:

• A factor of height n (and base 0, by default) is a subgraph of T which is a complete subtree of height n.

The number of nodes in a factor of height n is denoted by S(n

) def = d n -1 d-1 .
• A factor of height n and base k (with root v), is a subgraph of T which is the subtree of height k + n rooted in v minus the subtree of height k, rooted in v (see Figure 3 for an illustration). Such a subgraph is also called a factor of shape (n, k) in the following. The number of nodes of a factor of height n and base k is S(n, k)

def = d n+k -d k d-1 .
Similarly to what has been done for words or ordered trees, the factor complexity P T (n) of a tree T is the number of distinct factors of height n and base 0.

The complexity of a tree P T (n) can be bounded by the total number of ways to label trees of height n and degree d, say A n . It should be clear that A 1 = 2 (a node can be labeled 0 or 1) and that A n+1 = 2M (A n , d) where M (x, y) is the number of multisets with y elements taken from a set with x elements. Therefore using binomial coefficients,

A n+1 = 2 A n + d -1 A n -1 .
This is a polynomial recurrence equation of degree d. A change of variable,

u n = log A n + 1 d-1 log 2 d! yields a new recurrence equation u n+1 = (d + ε n )u n where n = o(1). This implies that A n = φ d n +o(d n ) for some φ with 1 < φ < 2.
As for lower bounds on the complexity of a tree, it will be shown in Section 3 that trees such that P T (n) ≤ n for at least one n are rational, i.e. have a bounded number of factors of any size (this implies that its minimal multigraph is finite). Therefore, trees T such that G(T ) is infinite and with a minimal complexity should satisfy P T (n) = n + 1. These trees will be called Sturmian trees by analogy with words. This definition is close to the one of [START_REF] Berstel | Sturmian trees. Theory of Computing Systems[END_REF] for ordered trees. It is not difficult to exhibit such trees. For example, for any Sturmian word w, a d-ary tree such that all nodes on level i have label w i is Sturmian.

Another more interesting example is the Dyck tree, represented on Figure 4. This tree is the unordered version of the tree-automata recognizing the Dyck language (language generated by the context-free grammar S → aSbS| ), introduced in [START_REF] Berstel | Sturmian trees. Theory of Computing Systems[END_REF] and it is not hard to see that this tree is Sturmian. For that, consider the graph G(T ) associated with the Dyck tree T , also displayed in Figure 4. There are two factors of height 1 in T : those with a root labeled 1 (all associated with node 0 in G(T )) and those with a root labeled 0 (associated with nodes ∞, 1, 2, • • • in G(T )). This corresponds to the equivalence classes for ≡ 1 . All factors of height n with a root associated to nodes ∞, n, n + 1, n + 2, . . . have labels equal to 0: no path of length n in G(T ) reaches the only node with label 1, namely node 0. The factors of height n starting with a root i of G(T ) with 0 ≤ i < n are distinct: their first node with label 1 is at level i + 1. In other words, the equivalence classes for ≡ n are {∞, n, n + 1, . . .}, {0}, {1}, . . . , {n -1}. The number of distinct factors of height n is n + 1.

1 2 3 4 0 ∞ ...

Density

The density of a tree T is meant to capture the proportion of ones in the tree. For any node v and any height n ≥ 0, the proportion of nodes with label 1 in the factor of height n with root v is denoted by d v (n). Let r be the root of the tree T . If the following limits exist, they define four notions of density:

• The rooted density of the tree is the limit of the density of the subtrees of the root r:

lim n→∞ d r (n).
• The rooted average density of the tree is the Cesaro limit of these densities:

lim n→∞ 1 n n i=1 d r (i).
• The density of the tree is α if it has an identical rooted density for all nodes:

∀v : lim n→∞ d v (n) = α.
• The average density of the tree is α if it has an identical rooted average density for all nodes:

∀v : lim n→∞ 1 n n i=1 d v (i) = α.
From the definitions, the following implications are direct: if a tree admits a density, then it admits an average density. In turn, a tree with an average density also has a rooted average density. Also, a tree with a density has a rooted density. See Figure 5 for some examples. These examples will be further developed in the following section on rational trees. The first tree has a density of 1/2, the second one an average density equal to 1/2 but no density. The last one has a rooted density 1/2 but no average density.

Ordered trees vs unordered trees

One of the main features of ordered labeled trees is the fact that there exists a bijection between ordered trees with finite degree and languages over finite alphabets, so that ordered trees benefit from the power of language theory formalism [START_REF] Courcelle | Fundamental properties of infinite trees[END_REF]. However, as shown in [START_REF] Berstel | Sturmian trees. Theory of Computing Systems[END_REF], the generalization of binary words to ordered trees with binary labels is surprisingly difficult. One of these difficulties comes from the combinatorial explosion due to the distinction of left and right children replacing a unique successor for words. This is the basis for the introduction of unordered trees, where the unique successor is replaced more naturally by a pair (or more) of successors and indeed more properties of words can be generalized. Let us anticipate with the results shown in the following sections. First, the notion of density of a tree is very natural for unordered trees (definitions in Section 2.5) and leads to an algorithmic construction of balanced trees using a mechanical process based on the density and the phase of the root (Proposition 4.4). This can be viewed as a natural extension to trees of the mechanical construction of balanced words.

Also, the two main results of the paper, namely the fact that the strongly balanced trees are the mechanical trees and have minimal complexity (Theorems 4.5 and 4.11) as well as the fact that rational strongly balanced trees are unique once the density is given (Theorem 5.1) are specific to unordered trees and generalize nicely the corresponding results for words.

Rational Trees

Definition 3.1. A tree T is rational if the associated minimal multigraph G(T ) is finite.
An example of a rational tree T is displayed in Figure 6 together with its multigraph G(T ). Note that this tree is not irreducible. It has one final strongly connected component of period 2 (corresponding to the alternating subtrees starting with ones and zeros, displayed as a left child) and a strongly connected component with period one (corresponding to the subtree with all its labels equal to one (displayed as a right child). It is also possible to characterize rational trees using their complexity P(n), as shown in the following theorem.

Theorem 3.2. The following statements are equivalent 1. the tree T is rational; 2. there exists n such that P(n) ≤ n; 3. there exists n such that P(n) = P(n + 1); 4. There exists B such that for all n, P(n) ≤ B.

Proof. The proof of this result is similar to the proof for words. 1 implies 2 : If G(T ) is finite, then the number of factors of height n in T is smaller than the size of G(T ), therefore, there exists n such that P(n) ≤ n. 2 implies 3 : Since P(1) = 2 and P(n) ≤ n and since P is non-decreasing with n, there exists 1 < k < n such that P(k) = P(k + 1).

3 implies 4 : If P(n) = P(n + 1) = p then let us call by A n 1 , . . . A n p all the distinct factors of height n in T . Since P (n + 1) = p, each A n i is prolonged in a unique way into a tree of height n + 1, called A n+1 i . Now, each subtree A n+1 i is composed of a root and d factors of height n, in the set {A n 1 , . . . A n p }.
In turn, they are all prolonged into trees of height n in a unique way. Therefore, P(n + 2) = p. By a direct induction, P(k) = p for all k ≥ n. 4 implies 1 : If the number of factors of height n is smaller than B for all n, then this means that the number of equivalence classes for ≡ n is smaller than B for all n, this means that G(T ) has less than B nodes.

Density of rational trees

Let T be a rational tree and let G(T ) be its minimal multigraph. The nodes of G(T ) are numbered

v 1 • • • , v K , with v 1 corresponding to the root of T .
G(T ) can be seen as the transition kernel of a Markov chain by considering each arc of G(T ) as a transition with probability 1/d. If G(T ) is irreducible then the Markov chain admits a unique stationary measure π on its nodes. The density of T and the stationary measure π are related by the following theorem.

Theorem 3.3. Let T be an irreducible rational tree with a minimal multigraph G(T ) with K nodes. Let = ( 1 , . . . K ) be the labels of the nodes of G(T ) and let π = (π 1 , . . . , π K ) be the stationary measure of the Markov chain over the nodes of G(T ). If T is aperiodic, then T admits a density α = π t (where t stands for the transpose of ). If T is periodic with period p then T admits an average density α = π t .

Proof. Let V be the Markov chain corresponding to G(T ). Since G(T ) is irreducible, V admits a unique stationary measure , say π = (π 1 , . . . , π K ). Let us call P the kernel of this Markov chain:

P i,j = a/d if there are a arcs in G(T ) from v i to v j .
Now, let us consider all the paths of length n in T , starting from an arbitrary node v i . By construction of G(T ), the number of paths that end up in the node v i of G(T ) is given by the vector d n e i P n , where e i is the vector with all its coordinates equal to 0 except the ith coordinate, equal to 1.

The number of ones in a subtree of height n starting in

v i is h n (v i ) = e i n-1 k=0 d k P k t .
Let us first consider the case where P is aperiodic. We denote by Π the matrix with all its lines equal to the stationary measure, π and by

D k the matrix P k -Π. When P is aperiodic, then lim k→∞ ||D k || 1 = 0. Therefore, for all k > n, ||D k || 1 < n → 0.
Then the density of ones

d 2n (v i ) = d-1 d 2n -1 h 2n (v i
) can be estimated by splitting the factors of height 2n into a factor of height n at the root and d n factors of height n. One gets

d 2n (v i ) = d -1 d 2n -1 e i n k=1 d k P k t + d -1 d 2n -1 e i 2n-1 k=n+1 d k P k t , = d -1 d 2n -1 e i ( n k=1 d k P k + 2n-1 k=n+1 d k D k + 2n k=n+1 d k Π) t .
When n goes to infinity, the first term goes to 0 because e i n k=1 d k P k t ≤ d n+1 . As for the second term

d-1 d 2n -1 e i 2n-1 k=n+1 d k D k t ≤ 1 d 2n -1 d 2n n .
This goes to 0 when n goes to infinity. As for the last term, d-1 d 2n -1 e i 2n-1

k=n+1 d k Π t = 1 d 2n -1 (d 2n -d n+2
)(e i Π) t goes to π t when n goes to infinity.

The same holds by computing the density of factors of shape 2n + 1 by splitting them into the first n + 1 levels and the last n levels. This shows that the rooted density of all the trees in T is the same, equal to π t .

Let us now consider the case when the tree is periodic with period p. In that case, the kernel of p steps of the Markov chain can be put under the form

P p =       P 1 0 • • • 0 0 P 2 . . . 0 . . . . . . . . . . . . 0 0 . . . P m      
.

The submatrices P 1 . . . P m are the kernels of aperiodic chains defined on a partition S 1 . . . S m of the nodes of G(T ). Let us denote by α 1 . . . α m the densities of the factors of height np, starting in S 1 . . . S m , respectively (they exist because this has just been proved for aperiodic trees).

Starting from a node v the average density of a tree of size n = pq n + r n , r n < p is

1 n n k=0 d k (v) = 1 pq n + r n ( qn a=0 p b=0 d ap+b+r (v) + 1 pq n + r n rn k=0 d k (v)).
The first term goes to (α 1 + . . . + α m )/m while the second term goes to zero, when n goes to infinity, independently of the root. Finally, (α 1 + . . .

+ α m )/m = (π 1 t 1 + • • • + π m t m )/m = π t where π 1 , • • • , π m
are the stationary probability for the kernels P 1 , . . . , P m and 1 , . . . m are the vectors of the labels in S 1 . . . S m .

An example illustrating the computation of the density of an aperiodic irreducible rational tree is given in Figure 7. The stationary measure of the Markov chain is π = (2/9, 3/9, 4/9). Therefore, the density is α = 2/9 1 + 3/9 2 + 4/9 3 = 4/9. 1 2 3

Figure 7: An irreducible aperiodic rational tree and its minimal graph. The stationary probabilities over the associated Markov chain are π = (2/9, 3/9, 4/9). The density of the tree is α = 4/9.

As for the reducible case, it should be easy to see that a rational tree may have different (average) densities for some of its subtrees (this is the case for the rightmost tree in Figure 5). Therefore, a reducible tree does not have a density nor an average density in general.

Let us call S 1 • • • S m the final strongly connected components of G(T ). Let α 1 . . . α m be the average densities of the components S 1 • • • S m respectively. Finally, let r = (r 1 . . . r m ) be the probability of reaching the components S 1 • • • S m starting from the root v 1 , in the Markov chain associated with G(T ). Then, the following theorem holds.

Theorem 3.4. A rational tree has a rooted average density α = (α 1 , . . . α m )r t .

Proof. If P is reducible, P can be decomposed into

P =      Q K 1 • • • K m 0 P 1 • • • 0 . . . • • • . . . . . . 0 0 • • • P m      and P n =      Q n K 1 • • • K m 0 P n 1 • • • 0 . . . • • • . . . . . . 0 0 • • • P n m     
, where P 1 . . . P m are the transition matrices of the final strongly connected components. Considering all the paths in G(T ) of length n, starting in the root, the number of paths ending in component S is

N (n) = d n i∈S P n 1i .
Let us decompose all the paths ending in S into two subpaths: one (of length k) before entering S and one (of length n -k) inside S , we get from the decomposition of P n ,

N (n) = d n n k=0 (1, 0 . . . 0)Q k K u
, where u is a vector whose coordinates are 1 in S and 0 everywhere else.

The number of 1 in the rooted subtree of T of height 2n is the number of ones in all the paths of length n plus the number of ones in the subtrees of height n. When n is large, the number of ones in the paths can be neglected with respect to the number of ones in the end trees.

Finally, the number of ones in a tree of height 2n is the number of ones in each possible end-tree of height n times the number of such trees, namely N (n). When n goes to infinity, the density of ones goes to

=1..m α (1, 0, . . . , 0)(I -Q) -1 K u = (α 1 • • • α m )r t , with r = (1, 0, . . . , 0)(I -Q) -1 K u .
An example of a reducible rational tree is given in Figure 6. The previous result can be used to compute its rooted average density. The graph G(T ) has two final components, one aperiodic component with density 1 and another one with period 2 with average density 1/2. Starting from the root, both components are reached with probability 1/2. Therefore, such a tree has an average rooted density α = 1/2.(1/2) + 1/2 = 3/4. Also, it is not difficult to show that if all final components have a density (rather than an average density), then the tree has a rooted density, given by the formula given by Theorem 3.4.

Finally, it is fairly straightforward to prove that since the transition matrix P of the Markov chain associated with G(T ) has all its elements of the form a/d, then the stationary probabilities π as well as the average rooted density α of a rational tree are rational numbers of the form c/b with 0 ≤ c ≤ b ≤ d K+1 . This fact will be used in the algorithmic section 5 to make sure that the complexities of the algorithms do not depend on the size of the numbers.

Balanced and Mechanical Trees

In this section, we introduce the notions of strongly balanced trees and mechanical trees and explore the relations between them. In particular we will prove that in the irrational case they represent the same set of trees, giving us a constructive representation of this class of trees. These results are very similar to the ones on words, which are summarized below.

Sturmian, Balanced and Mechanical Words

One definition of a Sturmian word uses the complexity of a word. The complexity of an infinite word w is a function P w : N → N where P w (n) is the number of distinct factors of length n of the word w. A word is periodic if there exists n such that P w (n) ≤ n. Sturmian words are aperiodic words with minimal complexity, i.e such that for any n:

P w (n) = n + 1. ( 1
)
If x is a factor of w, its height h(x) is the number of letters equal to 1 in x. A balanced word is a word where the letters 1 are distributed as evenly as possible:

∀x, y factors of w, |x| = |y| ⇒ |h(x) -h(y)| ≤ 1. (2)
A mechanical word can be constructed using integer parts of affine functions. Let α ∈ [0; 1] and φ ∈ [0; 1). The lower (resp. upper) mechanical word of slope α and phase φ, w = w 1 w 2 . . . (resp. w = w 1 w 2 . . . ) is defined by: ∀i ≥ 1

w i = (i + 1)α + φ -iα + φ , w i = (i + 1)α + φ -iα + φ . (3)
These three definitions represent almost the same set of words. In the case of aperiodic words, they are equivalent: a word is Sturmian if and only if it is balanced and aperiodic if and only if it is mechanical of irrational slope. For periodic words, there are similar relations:

• A rational mechanical word is balanced.

• A periodic balanced word is ultimately mechanical.

A word is called ultimately mechanical if it can be written as xw where x is a finite word and w is a mechanical word. An example of a balanced word which is not mechanical (and just ultimately mechanical) is the infinite word only made of zeros except for one letter 1. For a more complete description of Sturmian words, we refer to [START_REF] Lothaire | Algebraic combinatorics on words[END_REF].

Balanced and strongly balanced trees

Using the two definitions of factors of a tree, we define two notions of balance for trees: the first one and probably the most natural one, is what we call balanced trees and the other one is called strongly balanced trees.

Definition 4.1 (Balanced and strongly balanced trees). A tree is balanced if for all n ≥ 0, the number of nodes with label 1 in any two factors of height n, differs by at most 1.

A tree is strongly balanced if for all n, k ≥ 0, the number of nodes with label 1 in any two factors of height n and base k, differs by at most 1.

As the name suggests, strong balance implies balance (by taking k = 0). Actually, this notion is strictly stronger (Section 6 displays an example of a balanced tree that is not strongly balanced). Although the balance property is weaker and seems more natural for a generalization from words, the following mostly focuses on strongly balanced trees that have almost the same properties as their counterparts on words.

Density of a balanced tree

Before beginning the full investigation of balanced trees, we start with a rather straightforward property: a balanced tree has a density.

Let us recall the definition of the density (section 2.5): for all node v and all height n, we call h v (n)

the number of 1 in the factor of root v of height n and d v (n) the density of this factor,

d v (n) def = 1 S(n) h v (n).
Using this notation, we can write the following result.

Proposition 4.2 (Density of balanced tree). A balanced tree has a density α.

Moreover for all node v and for all height n:

|h v (n) -S(n)α | ≤ 1. (4)
Proof. Let m n be the minimal number of 1 in all factors of height n. Since the tree is balanced, for all nodes v and n ≥ 1:

m n ≤ h v (n) ≤ m n + 1. ( 5
m k + d k m n ≤ m n+k ≤ m k + 1 + d k (m n + 1). ( 6
)
The density of a factor of height n is mn S

(n) ≤ d v (n) = hv(n) S(n) ≤ mn+1 S(n) .
Using these facts, we can bound

d v (n + k) -d v (n): m n+k S(n + k) - m n + 1 S(n) ≤ d v (n + k) -d v (n) ≤ m n+k + 1 S(n + k) - m n S(n) .
Using ( 6), the left inequality can be lower bounded by

(d -1) d k m n + m k d n+k -1 - m n + 1 d n -1 = (d -1) m n + m k /d k d n -1/d k - m n + 1 d n -1 ≥ (d -1) m n d n -1 - m n + 1 d n -1 ≥ - 1 S(n) .
The same method can be used to prove that

d v (n + k) -d v (n) ≤ 1 S(n) , which shows that for n big enough, |d v (n + k) -d v (n)| is smaller than , regardless of k. Thus d v (n)
is a Cauchy sequence and has a limit α = lim n→∞ mn S(n) . Because of Equation ( 5), this limit does not depend on v and the tree has a density.

Let us now prove that |d v (n) -S(n)α | ≤ 1: dividing the Inequality (6) by S(n, k) and taking the limit when k goes to ∞ leads to:

(d -1)m n + α d n ≤ α ≤ (d -1)m n + 1 + α d n .
This shows that: S(n)α -1 ≤ m n ≤ S(n)α, which implies Equation (4).

Similar ideas can be used to show that Equation ( 4) can be improved in the case of strongly balanced trees. In a strongly balanced tree, for all base and height k, n ≥ 0, the number of ones h(n, k) in a factor of height n and base k satisfies:

h(n, k) -S(n, k)α ≤ 1. ( 7
)
This is false in general for balanced trees.

Mechanical Trees

Building balanced tree is not that easy. According to formula (4), each factor of height n must have αS(n) or αS(n) + 1 or αS(n) -1 nodes labeled one. This leads to the following construction, inspired by the construction of mechanical words.

Definition 4.3 (Mechanical tree).

A tree is mechanical with density α ∈ [0; 1] if for all nodes v, there exists a phase φ v ∈ [0; 1) that satisfies one of the two following properties:

∀n : h v (n) = S(n)α + φ v , (8)
or ∀n :

h v (n) = S(n)α -φ v . (9)
In the first case, φ v is an inferior phase of v. In the second case, φ v is a superior phase of v.

This definition suggests that the phases of all nodes could be arbitrary. In fact, we will see that there exists a unique mechanical tree once the phase of the root is given. The second question raised by this definition is the existence and uniqueness of the phase: we call φ v "a" phase of a node φ v and not "the" phase of φ v since there may exist several phases leading to the same tree. This is further discussed at the end of this section.

We begin by a characterization of mechanical trees.

Proposition 4.4 (Characterization of mechanical trees). Given α ∈ [0; 1] and φ ∈ [0; 1), there exists a unique mechanical tree of density α such that φ is an inferior (resp. superior) phase of the root.

Moreover, if φ is an inferior (resp. superior) phase of a node then φ 0 ≤ • • • ≤ φ d-1 are inferior (resp. superior) phases of its d children, with

φ i = φ + α + i -α + φ d resp. φ i = φ -α + i -α -φ d . ( 10
)
Proof. The proof will be done in two steps. Firstly, we will see that if we define the phases as in (10) then the tree is mechanical. Secondly, we will see that this is the only way to do so.

Existence. Let α ∈ [0; 1] and φ ∈ [0; 1). We want to build a mechanical tree whose root has an inferior phase φ ( the case of a superior phase if similar and is not detailed here). Let A be an infinite tree. To each node v, we associate a number φ v defined by:

• φ root = φ.
• If the phase of a node v is φ v , its d children satisfy Equation ( 10).

Then we build a labeled tree by putting to each node v the label α + φ v . Let us prove by induction on n that the following relation holds.

For all

v : h v (n) = S(n)α + φ v . ( 11
)
By definition of the labels, (11) holds when n = 1. Let n ≥ 0 and let us assume that (11) holds for n. Let v be a node with phase φ v and let φ 0 . . . φ d-1 be the phases of its children. We assume that α + φ v < 1 , which means that the label of the node is 0 (a similar calculation can be done in the other case (α + φ v > 1)).

Using the well-known formula

d-1 i=0 x + i d = dx , we can compute h v (n + 1): h v (n + 1) = d-1 i=0 S(n)α + φ i = d-1 i=0 d n -1 d -1 α + α + φ + i d = d( d n -1 d -1 α + α + φ d ) = S(n + 1)α + φ .
Therefore, (11) holds for all n which means that the tree is mechanical.

Uniqueness. Now, let A be a mechanical tree of density α. Let v be a node and φ 0 , . . . , φ d-1 be the phases of its children. Let i and j be two children and let h i (n) be the number of ones in the ith subtree (of phase φ i ). We want to prove that either for all n: h i (n) ≤ h j (n) or for all n: h i (n) ≥ h j (n) . If the two nodes are both inferior (resp. superior), this is clearly true:

h i (n) ≤ h j (n) if and only if φ i ≤ φ j (resp. φ i ≥ φ j ). If i is inferior and j is superior, it is not difficult to show that φ i < 1 -φ j implies h i (n) ≤ h j (n) and φ i ≥ 1 -φ j implies h i (n) ≥ h j (n).
Therefore we can assume (up to an exchange of the order of the children) that for all n:

h 0 (n) ≤ h 1 (n) ≤ • • • ≤ h d-1 (n). Moreover as h d-1 (n) -h 0 (n) ≤ 1, there exists k such that h 0 (n) = h 1 (n) = • • • = h k (n) < h k+1 (n) = • • • = h d-1 (n). As d-1 i=0 h i (n)
does not depend on φ 0 , . . . , φ d-1 , then for each n there is only one k that works and therefore there is only one possibility for h i (n) for all n and all i. By induction of the depth of the children, this implies that for every node v in the subtree of root v, h v (n) is fixed and therefore the tree with root v is unique.

As we have seen in the beginning of the proof, the phases φ i defined in (10) provide correct values for h i (•). Therefore such a phase φ i is a possible phase for the ith child.

This theorem shows that when the phase is fixed the tree is unique. The converse is false and one can find several phases that lead to the same tree (for example, when α = 0 all phases define the tree with label 0 everywhere) but we will show next that the set of densities α for which the phases are not necessarily unique has Lebesgue measure zero.

If for all n, S(n)α + φ ∈ N, then S(n)α + φ = S(n)α + φ -1 . In that case, if φ is an inferior phase of a node then 1 -φ is a superior phase of the node. Therefore -except for particular cases-there exists at least two phases of a node: one inferior and one superior. Let us now look at the possible uniqueness of the inferior phase.

Let us denote frac(x) ∈ [0; 1) the fractional part of a real number x and let us consider the sequence {frac(S(n)α + φ)} n∈N . If this sequence can be arbitrarily close to 0, this means that for all ψ < φ, there exists k such that S(k)α + ψ < S(k)α + φ and ψ can not be a phase of the tree. Also, if this sequence can be arbitrarily close to 1, then one can show similarly that for all ψ > φ, ψ is not a phase of the node.

Conversely, if there exists δ > 0 such that frac(S(n)α + φ) > δ (resp. < 1 -δ) for all n and if we set φ = φ -ε (resp. φ = φ + ε), with ε < δ, then S(n)α + φ = S(n)α + φ for all n.

Thus, a phase φ is unique if and only if 0 and 1 are accumulation points of the sequence {frac(S(n)α + φ)} n .

Let us call x def =1 d-1 α and y def = φ -x and x 1 , . . . , x k , . . . (resp. y 1 , y 2 , . . . ) be the sequence of the digits of x (resp. y) in base d (also called the d-decomposition). We want to study the sequence frac(S(n)α + φ) = frac(xd n -y).

xd n -y = n k=1 x k d n-k ∈N + ∞ k=1 (x k+n -y k )d -k .
Therefore, frac(xd n -y) is arbitrarily close to 0 implies that for arbitrarily big k, there exists n such that

x n . . . x n+k-2 = y 1 . . . y k-1 , x n+k-1 > y k , or frac(xd n -y) = 0. ( 12
)
Also, frac(xd n -y) is arbitrarily close to 1 implies that for arbitrarily big k, there exists n such that

x n , . . . , x n+k-2 = y 1 , . . . , y n-1 , x n+k-1 < y n , or the d-development of y is finite (i.e. with only zeros after some point : y = y 1 , . . . , y , 1, 0, 0 . . .) and that for arbitrarily big k, there exists n such that

x n , . . . , x n+k-2 = y 1 , . . . , y , 0, 1, . . . , 1. ( 13) Using this characterization, three cases can be distinguished.

• If α d-1 is a number such that all finite sequences over 0, . . . , d -1 appear in its d-decomposition, then every phase is unique. In particular, all normal numbers 1 in base d verify this property and it is known that almost every number in [0, 1] is normal (see [START_REF] Borel | Les probabilités dénombrables et leurs applications arithmétiques[END_REF] or [START_REF] Durrett | Probability: theory and examples[END_REF]).

• If α ∈ Q, then the sequence frac(S(k)α + φ) is periodic and there are no phase φ such that φ is unique.

• If α is neither rational nor has the property that all d-sequences appear in α, then some φ can be unique and some others may not. For example, for d = 2, if α is (in base 2) the number α = 0.101100111000111100001111100000 . . . , then if frac(α -φ) = 0, φ is unique (because α satisfies both Equations ( 12) and ( 13)). However φ 1 and φ 2 such that frac(α -φ 1 ) = 0.10100 and frac(α -φ 2 ) = 0.1010 are equivalent (generate the same tree).

Other examples of the same type are the rewind trees, drawn on figure 16. The sequence of digits in base 2 of the density of such a tree is a Sturmian word. Half of the nodes of the tree are associated with node 0 in the minimal graph and therefore could have the same phase whereas the phases computed using Equation ( 10) are not all the same. Therefore, phases are not unique here.

Phases of a tree

Let us call Φ v the set of numbers that can be phases of a node v and Φ the set of the possible phases of a tree. Φ is the union of all possible phases of its nodes: Φ = ∪ v Φ v . The set Φ may be countable or uncountable. Countable for example when α/(d -1) is normal since there are at most as many phases as nodes. Uncountable for example for the tree with all label 0, for which for each node, all phases in [0; 1) work.

In all cases, the set of possible phases is dense is [0; 1). Indeed, at least all phases defined by the relation (10) are in Φ. If φ is the phase of the root, then all nodes at level k have a phase which is the fractional part of:

φ+α+i k d +α+i k-1 d + • • • + α + i 1 d = α( 1 d k . . . 1 d ) + φ d k + i k d k + • • • + i 1 d 1 , ( 14
)
with 0 ≤ i j < d for all j. Conversely all of these numbers are the phases of some node at level k.

As k goes to infinity and using a proper choice of i 1 , . . . , i k the fractional part of this number can be as close as possible to any number in [0; 1]. Thus the set of phases of the tree is dense in [0; 1].

If the density is p(d-1) d n+k -d k (with n + k minimal) one can show that the set of all possible phases for a given node is

[ d m -1 d-1 α; min( d m+1 -1 d-1 α, 1
)) for some m ∈ 0, . . . , n + k -1. As Φ is dense in [0; 1), it contains all these intervals. Therefore, Φ = [0; 1) and the tree has exactly n + k different factors of height greater than n + k. Hence its minimal graph has exactly n + k nodes.

Equivalence between strongly balanced and mechanical trees

As seen in section 4.1, there are strong relations between balanced and mechanical words. This part shows the same results between strongly balanced and mechanical trees. This result is formally stated in the following theorem.

A tree is ultimately mechanical if all nodes are mechanical (i.e. satisfies Equation ( 8) or ( 9)), except finitely many.

Theorem 4.5. The following statements are true.

(i) A mechanical tree is strongly balanced. (ii) An irrational strongly balanced tree is mechanical. (iii) A rational strongly balanced tree is ultimately mechanical.

This theorem is the analog of the theorem linking balanced and mechanical words. The word 0 k 10 ∞ is balanced but not mechanical, only ultimately mechanical. Its counterpart for trees would be a tree with all labels equal to 0 except for one node which has label 1. The label 1 can be put as deep as desired, which shows that we can not bound the size of the "non-mechanical" beginning of the tree. A more complicated example is drawn in Figure 8.

Let us begin by the proof of the first part of the theorem:

Lemma 4.6. A mechanical tree is strongly balanced.

Proof. Let n, k ∈ N. For all nodes v, h v (n, k) is the number of 1 in the factor of height n and base k rooted in v. We want to prove that for all pairs of nodes v and v :

|h v (n, k) -h v (n, k)| ≤ 1.
By proposition 4.4, we can assume that all phases of the tree are inferior (the case where all phases are superior is similar). We call φ (resp. φ ) a phase of the node v (resp. v ).

h v (n, k) -h v (n, k) = d n+k -1 d -1 α + φ - d k -1 d -1 α + φ - d n+k -1 d -1 α + φ + d k -1 d -1 α + φ .
Using the well-known inequality

x -x -1 < x -x < x -x + 1, one can show that -2 < h v (n, k) -h v (n, k) < 2. As h v (n, k) and h v (n, k) are integers, we have -1 ≤ h v (n, k) -h v (n, )
k ≤ 1 which ends the proof of the lemma.

We will see in the next section 4.5 that a strongly balanced tree is rational if and only if its density can be written as p S(n,k) (p, k, n ∈ N), therefore we will do the proof of theorem 4.5 distinguishing strongly balanced tree with density of this form from the others. Lemma 4.7. If A is a strongly balanced tree of density α which can not be written as p S(n,k) (p, k, n ∈ N) then A is mechanical.

Proof. Using Equation ( 7), each factor of shape (n, k) has p -1, p or p + 1 nodes labeled by 1. As the tree is strongly balanced, either there is no factor with p -1 ones or no factor with p + 1 ones. Let us assume that there is no factor with p -1 ones (the other case is similar). We claim that there are at most n factors of shape (n, k) with p + 1 nodes labeled by 1.

Indeed, let f be a factor of shape (n , k ) with n = n, i ∈ N, k ≥ k. This tree is composed of j blocks of shape (n, k) (where j depends on and k ) and using Equation ( 7) again, the number of nodes with label 1 is either jp -1, jp or jp + 1. Therefore at most one of the (n, k) blocks has p + 1 nodes labeled by 1.

If there were more than n + 1 blocks of shape (n, k) with p + 1 ones in the whole tree, starting respectively at line l 1 , . . . and l n+1 , there would be two blocks with l i = l j mod n and the block of height l j -l i + n, l i would have jp + 2 ones, which is not possible. Therefore there are at most n blocks of shape n, k with p + 1 nodes labeled by 1 in the whole tree.

An example of a rational tree strongly balanced but not mechanical is presented in Figure 8.

A 4 A 3 A 4 A 3 A 3 0 1 2 3 4
Figure 8: Example of a rational tree that is strongly balanced but not mechanical. On the left is the tree itself. In the middle the mechanical suffixes of the tree are displayed and its minimal graph (reducible) is displayed on the right. There is one strongly connected component -the one corresponding to the nodes 3-4 -and two corresponding suffixes: A 3 , starting with a 0, and A 4 , starting with a 1. One can verify on the picture that the beginning of this tree is strongly balanced and as it continues with density exactly 1/3, the whole tree is strongly balanced. However this tree is ultimately mechanical but not mechanical since in a mechanical tree of density 1/3, all factors of height 2 should have 1 + φ = 1 node labeled by one.

Lemma 4.10. A strongly balanced tree with density α = p S(n,k) , p, n, k ∈ N, is ultimately mechanical. Furthermore, if the tree is irreducible, it is mechanical.

Proof. Using Lemma 4.9, there are at most n factors of height n and base k with p + 1 nodes labeled 1, in the rest of the tree all factors of shape (n, k) have exactly p ones. Then the tree is ultimately mechanical by Lemma 4.8.

If the tree is irreducible, a factor appears either 0 or an infinite number of times. As there are at most n factors of shape (k, n) with p + 1 nodes labeled 1, there are no such factors and the tree is mechanical by Lemma 4.8.

Note that this lemma concludes the proof of Theorem 4.5.

Link with Sturmian trees

In the case of words, Sturmian words are exactly the balanced (or mechanical) aperiodic words. The case of trees does not work as well since the Dyck Tree (Figure 4) and more generally all examples of Sturmian trees given in [START_REF] Berstel | Sturmian trees. Theory of Computing Systems[END_REF] are not balanced. However, the reverse implication holds as seen in the following theorem:

Theorem 4.11. The following propositions are true.

• A strongly balanced tree of density different from p S(n,k) (for any p, n, k ∈ N) is Sturmian.

• A strongly balanced tree of density p S(n,k) (p, n, k ∈ N) is rational.

This result has a simple implication: a strongly balanced tree is rational if and only if there exist p, n, k ∈ N such that its density is p S(n,k) .

Proof. Let us consider the case of inferior mechanical trees (the superior case being similar).

Let A be a mechanical tree of density α, let v be a node and let n ≥ 0. According to Proposition 4.4, the factor of root v of height n only depends on the phase φ v of its root. In fact, one can show in the proof of Proposition 4.4 that this factor only depends on the values

d i -1 d-1 α + φ v (1 ≤ i ≤ n). If we write f i (φ) def = d i -1 d-1 α + φ (i ≥ 0, φ ∈ [0 : 1]
), the number of factors of height n only depends on the values f 1 (φ) . . . f n (φ).

As seen in ( 14), the set of phases is dense in [0; 1], therefore they are exactly as many trees as tuples f 1 (φ), . . . , f n (φ) when φ ∈ [0; 1) by right-continuity of f i .

Each f i is an increasing functions taking integer values and h i (1) -h i (0) = 1. Thus there are at most n + 1 different tuples and then at most n + 1 factors of height n and a mechanical tree is either rational or Sturmian.

if α ∈ p d k S(n) /p, n, k ∈ N , we can not have i = j and d i -1 d-1 α + φ, d j -1 d-1 α + φ ∈ N and then there are exactly n + 1 factors of height n.

If α = p S(n,k) , then the number of factors of height n is at most n. Therefore the tree is rational using Theorem 3.2 (see Section 4.3.1).

If the the tree is not mechanical, then Theorem 4.5 says that the tree has a density α = p S(n,k) and is ultimately mechanical: there exists a depth D ≥ 1 after which the tree is mechanical. Therefore, there are at most S(D) + n factors of any height (n in the mechanical children because of the value of α plus S(D) in the prefix subtree). In that case the tree is rational by Theorem 3.2.

Algorithmic issues

Testing if a rational tree is strongly balanced

Given a finite description of a rational tree, let us consider the problem of checking whether this tree is strongly balanced. An algorithm that works in time 0(N 3 ) where N is the number of vertices of the minimal graph of the tree is presented.

The first focus is on the description of the special structure of the minimal graph of a rational strongly balanced tree. Then an algorithm for irreducible rational trees is described as well as a sketch of the algorithm for the general case.

Graphs of rational strongly balanced trees

The aim of this section is to study the general form of the minimal graphs of rational strongly balanced trees. In fact, we will see that they have a very particular form. The main results of this section are summarized in Theorem 5.1 and illustrated by Figures 9 and10.

Theorem 5.1. (i) Two rational mechanical trees of the same density α have the same minimal graph G α , up to the choice of the initial node of this graph. Moreover G α is irreducible.

(ii) The minimal graph of a strongly balanced tree of density α has a unique strongly connected component that is final, G α .

Proof. (i) Let us first consider a rational mechanical tree of density α. We know that there exist p, k, n ≥ 0 such that α = p(d-1) d k (d n -1) . Using section 4.3.1, the minimal graph has exactly n + k nodes, and for any node, the set of all possible phases of all its descendants is [0; 1). Therefore, the graph is strongly connected and unique. The only difference between two rational mechanical trees of the same density is to which node the root of the tree is associated. Figure 9 displays several examples. The (unique) minimal graph of the mechanical trees of density 1/3, 1/7, 4/15 and 2/15 are displayed.

(ii) If the tree is strongly balanced but not mechanical, it is ultimately mechanical (see proposition 4.10) which means that after a finite depth k, all suffixes are mechanical trees with the same density. All of these 0 1 0 1 2 0 2 3 1 0 3 1 2

Figure 9: All mechanical trees of the same density α have the same minimal graph Gα. These graphs represent Gα for α = 1/3, 1/7, 4/15 and 6/15 = 2/5. For all graphs with n nodes, there are exactly n different mechanical trees of this particular density, depending on which node is associated to the root. Note that the first three graphs have a very similar structure (Figure 16 displays more mechanical trees with this structure).

trees have the same graph, therefore the minimal graph has a unique final strongly connected component which is reached in at most k steps. Therefore, the minimal graph of a strongly balanced tree can be decomposed into a finite acyclic graph and one final strongly connected component, like in Figure 10.

Irreducible trees

Testing if two graphs with a given fixed out-degree are isomorphic can be done in polynomial time [START_REF] Luks | Isomorphism of graphs of bounded valence can be tested in polynomial time[END_REF]. Therefore using the result shown in the previous section 5.1.1, an algorithm to test if a graph represents a mechanical tree can be obtained by computing the density α of the graph and testing if the graph is isomorphic to the graph of all mechanical trees with density α. However this is not very efficient and here we propose an algorithm that tests the balance property directly.

Consider an irreducible rational tree A and let n 0 be the number of vertices of its minimal graph. Theorem 4.11 says that it is strongly balanced if and only if it is mechanical. In that case its density is p S(n0,k0) for some p, k 0 ∈ N and all subtrees of shape (k 0 , n 0 ) have exactly p nodes with label 1. Such factors will be called basic blocks in the following.

Recall that the tree is strongly balanced if all factors of shape (n, k) have αS(n, k) or αS(n, k) + 1 nodes of label one. We want to show that testing it for all n, k < n 0 + k 0 is sufficient.

Let v be a node and n, k ≥ 0 and let h v (F ) be the number of labels 1 in the factor F of shape (n, k) with root v.

Starting from F , we construct a new factor F by adding a new factor on top of F of shape n 0 , k -n 0 . This new factor can be partitioned into d k-n0-k0 basic blocks. The total factor F is of shape (n + n 0 , k -n 0 ) and its number of ones is h v (F ) = h v (F ) + d k-n0-k0 p (see Figure 11).

The augmentation of the factor can be repeated until its shape n , k is such that k ≤ k 0 + n 0 . Its number of ones is h v (F ) = h v (F ) + H where H does not depend on v.

The second phase consists in building a new factor F by removing a factor from F of shape n 0 , k +n -n 0 . The removed part can be partitioned into d n -n0-k0 basic blocks. Therefore the number of ones in F is h

v (F ) = h v (F ) -d n -n0-k0 p. This transformation is illustrated in Figure 12. . . . k n k 0 n 0 p p → k -n 0 n + n 0 h(n, k) h(n, k) + T k
Figure 11: The first transformation: if k > n 0 + k 0 , we add a level of factors of shape n 0 , k 0 that all contain exactly p ones. The shape of the factor becomes (n + n 0 , k -n 0 ). We repeat the transformation until the shape is (n , k ) with k < n 0 + k 0 . In the figure ,T

k stands for pd k-n 0 -k 0 p. h(n , k ) k 0 n 0 k n . . . p p → k n -n 0 h(n , k )-T n
Figure 12: The second transformation: if n > n 0 + k 0 , we can remove a level of factors of shape (n 0 , k 0 ). The shape of the factor becomes (n -n 0 , k ). We repeat the transformation until the shape is (n , k ) with n < n 0 + k 0 (here, T n = pd n -n 0 -k 0 ).

By repeating this transformation as long as n > n 0 + k 0 , we get a final factor F whose shape is (n , k ) with n < n 0 + k 0 , k < n 0 + k 0 and whose number of ones is h v (F ) = h v (F ) + H -K, where H and K do not depend on v but only on n and k.

Since h v (F ) = h v (F ) -H + K, it is enough to compute the number of ones in all factors with shape (n , k ) where n < n 0 + k 0 , k < n 0 + k 0 , to be able to obtain the number of ones in all factors on any shape.

Also, it is enough to test if all factors with shape (n , k ) where n < n 0 + k 0 , k < n 0 + k 0 satisfy the strong balance property for all factors on any shape to have the same property.

There are at most n factors of a given height and base. For b < m, let us call h i,h,m the number of 1 in the i th factor of height b and base b + m. Let us call v(i) = (v 1 (i), . . . , v d (i)) the set of the d children of the tree rooted in i. h i,b,m can be computed using the formula:

h i,b,m =    h i,1,0 = (i) h i,b,0 = (i) + j∈v(i) h j,b-1,0 h i,b,m = j∈v(i) h j,b-1,m-1 (19)
These considerations yield the Algorithm 1. The main steps of the algorithm are:

1. Compute the density α of the tree (cf Theorem 3.4).

2. If α can not be written as p d-1 d N -d k , the tree is not strongly balanced. 3. Check the strongly balanced property on the factors of shape (n, k) < (N, N ).

According to [START_REF] Sloane | The On-Line Encyclopedia of Integer Sequences[END_REF], there is no simple solution of this equation but using the method described in [START_REF] Aho | Some doubly exponential sequences[END_REF], one can show that a n is the nearest integer close to θ 2 n -1/2, where θ ≈ 1.597910218 . . . is the exponential of the rapidly convergent series ln(3/2) + n≥0 ln(1 + (2a n + 1) -2 ).

In section 4.5, we have seen that the number of factors of height n of a mechanical tree is the number of tuples (f 1 (φ, α), . . . , f n (φ, α)) where f i (φ, α) = (2 n -1)α + φ . Let us call u n this number.

To count the number of these tuples, consider the lines α → (2 n -1)α mod 1, with 0 ≤ α ≤ 1(see Figure 13). The number of tuples is the number of different zones in this figure. An exact computation of u n is cumbersome but good bounds can be computed easily. u n+1 -u n corresponds to the number of zones added by adding the lines α → (2 n+1 -1)α -i. Each of these 2 n+1 -1 lines:

• add at least a new zone if it only crosses other lines at points φ = 0 or φ = 1. This is a very low estimate since it is only true for i = 0 or i = 2 n -2, in the other cases it crosses at least the line α → φ.

• add at most 1 + n zones if it crosses the n lines corresponding to α → (2 j -1)α -i j , 1 ≤ j ≤ n and if all these points are pairwise distinct.

Therefore we have an estimation for all n ≥ 2:

2 + 2(2 n+1 -3) ≤ u n+1 -u n ≤ (n + 1)(2 n+1 -1). ( 22
)
This leads to the bounds for n ≥ 3:

2 n+2 ≤ u n ≤ n2 n+1 . (23)
Improving these bounds seems difficult. To do so, one would have to count whether a "new" intersection has already been counted or if it is on the boundary φ = 0. By simulation, it seems that the number of trees is closer to n2 n+1 than to 2 n+1 .

Glossary

The aim of this part is to show the big picture and to provide several examples of trees that are either balanced, strongly balanced, reducible, irreducible, rational or Sturmian. In particular, we will give counterexamples that show that the inclusions between these classes are strict. The Figure 14 Each number refers to an example detailed in section 6. For example, zone 6 is the set of trees that are rational, reducible, ultimately mechanical, strongly balanced, balanced and neither mechanical nor Sturmian.

1. Reducible Sturmian tree that is not balanced -contrarily to the case of words where Sturmian words are balanced, there exist Sturmian trees that are not balanced. The Dyck tree (Figure 4), is one of them. 2. Irreducible Sturmian trees that are not balanced -An example of a Sturmian tree that is irreducible (but not balanced) is the reflected random walk tree represented in The reflected random walk tree: each node of type n is followed by one of type n -1 and one of type n + 1 (except for 0 that is followed by 0 and 1).

3. Irreducible rational trees -see Figure 7. 4. Reducible rational trees -see Figure 6. 5. Irreducible strongly balanced rational tree -see discussions in section 5.1.1 and Figure 9. 6. Rational reducible strongly balanced trees that are not mechanical -strongly balanced trees are not necessarily mechanical: if they are reducible, they are only ultimately mechanical, see Figure 8 for an example. 7. Reducible mechanical trees -let α be a normal number and consider the mechanical tree of density α and phase 0 at the root. As α is normal, there is a unique phase corresponding to each node of order k which is the fractional part of:

α( 1 d k + • • • + 1 d ) + i k d k + • • • + i 1 d , (24)
for a unique sequence i 1 , . . . , i k (see the end of section 4.3 for details about normal numbers and phases). If two phases corresponding to i 1 , . . . , i k and i 1 , . . . , i k are equal, then we have frac α(

1 d k + • • • + 1 d k +1 ) + k j=1 i j d j - k j=1 i j d j = 0.
As α is normal, it is irrational. Therefore k = k and frac( j≤k ij d j -j≤k i j d j ) = 0. By uniqueness of the decomposition of a number in base d, this implies that the two sequences are equal. This shows that two different nodes in the tree have a different phase. Thus the minimal graph of this tree is exactly the tree itself which is in a sense the most reducible tree. 8. Irreducible mechanical trees -let w be a mechanical word and consider a graph with vertices {0, 1, . . . , }, where a node i ≥ 0 has label one if and only if w i = 1. The node i has two outgoing arcs: one ending in i + 1, one ending in 0. We call this graph a restart tree since for a node n, we have the choice between restarting back in 0 or continuing in n + 1, an example is displayed in Figure 16. As seen in Figure 17, the number of ones in a factor of height n that corresponds to the node i is

h i (n) = w i + • • • + w i+n-1 + h 0 (n -1) + • • • + h 0 (1), (25)
and the number of ones in a factor of height n and base k is

h i (n, k) = h i (n) -h i (k) = w k + • • • + w i+n-1 + h 0 (n -1) + • • • + h 0 (k). ( 26
)
Therefore the tree is strongly balanced if and only if the word w is balanced. Since the tree is irreducible, in that case the tree is also mechanical. Moreover one can show that for any word w the tree has a density which is lim n→∞ h0(n)

2 n -1 = w0 2 + w1 4 + w2 8 + • • • . Thus for any aperiodic balanced word, this provides an example of an irreducible irrational strongly balanced tree. 9. Rational balanced tree that is not strongly balanced -An example of a rational tree that is balanced but not strongly balanced is presented in Figure 18. One can show that all of its factors of height 3 have exactly 4 nodes with label one. Using this fact, one can show that the number of ones in a factor of height 3n + i (0 ≤ i ≤ 3) rooted in a node j is:

Looking at the extremal cases for x + a < 1 and x + a ≥ 1 which are x = 0, 1 -a, 1, one gets 4 relations:

2(1 -a) -2φ + α < 1

-1 ≤ -2φ + α 2 -2φ + α < 2 0 ≤ 2(1 -a) -2φ + α.
Therefore the tree is balanced if and only if

α 2 < φ ≤ α + 1 2 < φ + a < 1. ( 28
)
Moreover if α + φ ≥ 1 and 3α + φ < 2, the tree is not strongly balanced since its beginning is There are lots of triples α, φ, a satisfying conditions (28). For example a tree with α = 1 3 + , φ = 0.6 and a = 0.2 where ∈ R \ Q with small enough (for example < 0.01 works since α 2 ≈ 0.21 < 0.6 < α+1 2 ≈ 0.71 ≤ 0.8 < 1 and α + φ > 1, 3α + φ ≈ 1.9 < 2).

Figure 1 :

 1 Figure1: The tree-automaton recognizing the Fibonacci word f and the corresponding minimal automaton. The states of this later are 0, 1 . . . , ∞. The final nodes are filled in black. There is a transition between nodes i and i + 1 labeled by the ith letter of f and one between nodes i and ∞ labeled by the opposite of this ith letter.

0 ∞Figure 2 :

 02 Figure2: A tree T and the associated minimal multigraph G(T ). The label of the black (white) nodes is 1 (0). The arcs are implicitly directed from top to bottom.

Figure 3 :

 3 Figure 3: Example of factors of a tree. On the left a factor of height 3 (and base 0) is surrounded in black. On the right is a factor of height 2 and base 2.

Figure 4 :

 4 Figure 4: The Dyck tree and its minimal graph.

Figure 5 :

 5 Figure5: The first tree has a density of 1/2, the second one an average density equal to 1/2 but no density. The last one has a rooted density 1/2 but no average density.

Figure 6 :

 6 Figure 6: A rational tree made of two distinct subtrees and its associated multigraph

)

  Now let us consider a factor of height n + k and root v. It can be decomposed into a factor of height k of root v and d k factors of height n at the leaves of the previous factor. The number of ones in these factors can be bounded by expressions depending on m n and m k :

Figure 10 :

 10 Figure 10: General form of the graph of a reducible strongly balanced tree: an acyclic graph ending in a unique strongly connected component.

Figure 13 :

 13 Figure 13: On the left picture (resp. on the right one) the number of distinct factors of height 3 (resp. 4) are represented. The lines drawn are α → (2 n -1)α mod 1 for 1 ≤ n ≤ 3 (resp. n ≤ 4). Each zone corresponds to a distinct factor of height 3 (resp. 4) of all mechanical trees. On the left picture, we can count that there are 20 factors of height 3. The difference between the left and the right picture is the addition of the lines α → (2 4 -1)α mod 1. This leads to 60 factors of height 4.

Figure 14 :

 14 Figure14: Relations of inclusion linking the different classes of trees. Each number refers to an example detailed in section 6. For example, zone 6 is the set of trees that are rational, reducible, ultimately mechanical, strongly balanced, balanced and neither mechanical nor Sturmian.

Figure 15 .

 15 It is Sturmian since the equivalence classes of the relation ≡ n are {0}, . . . , {n -1}, {n, n + 1, . . . }.

Figure 15 :

 15 Figure15: The reflected random walk tree: each node of type n is followed by one of type n -1 and one of type n + 1 (except for 0 that is followed by 0 and 1).

Figure 16 :

 16 Figure 16: Example of the restart tree corresponding to the word aabaaab . . .

Figure 17 :

 17 Figure 17: Number of ones in a factor of the restart tree of height 5

the Fibonacci word is the limit of the sequence f n+2 = fnf n+1 with f 0 = a and f 1 = b, see(Lothaire,

2002) for more details.

A number is normal in base d if all sequences of length k appear uniformly in its d-decomposition

Proof. Let τ be a real number and v a node. At least one of the two following properties is true:

∀n ≥ 1 :

To prove this, assume that it is not true. Then there exists k, n such that h v (n) < S(n)α + τ and h v (k) > S(k)α + τ . In that case the number of 1 in the factor of height n and base n -

α -1 which violates Formula (7). Let us now define the number φ as the minimum τ that satisfies (15):

For all τ > φ, the equation ( 15) is true, while for all τ < φ, the equation ( 16) is true. This means that for all > 0 and all n:

Taking the limit when tends to 0 shows that:

Therefore, unless

Lemma 4.8. Let A be a strongly balanced tree such that there exist n and k such that all factors of shape (n, k) have the same number of nodes with label 1. Then the tree is mechanical.

Proof. Let us take n and k satisfying the property, such that n + k is minimal and let p be the common number of ones in the factors of shape (n, k). Obviously, the tree as a density α = p(d-1) d k (d n -1) . Let v be the root of the tree. The same proof as in the irrational case can be used to establish that there exists φ such that

and that the root is inferior of phase φ if there is no

Therefore the tree is mechanical unless there exist i and j satisfying these equalities. Let us show that if there exist such i and j, there is a contradiction.

. Either i < j or i > j, let us assume that j < i, the other case is similar. The number of ones in the factor of height i -j and base j is

In that case we have i ≥ k + n, otherwise this would violate the minimal property of n + k. If j -i > n the factor of height i -j and base j is composed of a factor of height i -n and base j and d i-n-k factors of height n and base k -that have exactly p nodes labeled one as assumed in the previous paragraph -and then the number of 1 in this subtree is:

which violates the minimality of i.

Then if all factors of shape (k, n) have exactly p nodes labeled 1, the tree is mechanical.

Lemma 4.9. If A is a strongly balanced tree with a density α = p S(n,k) then it has at most n factors of shape (n, k) with p + 1 ones or p -1 ones.

Algorithm 1 Testing if a irreducible rational tree is strongly balanced Require: Minimal graph G of a irreducible rational tree Ensure: The tree corresponding to G is strongly balanced N:= number of vertices of G Compute the density α of the Markov Chain if for all k:

α + 1 then return "not strongly balanced" end if end for return "strongly balanced" Solving the Markov chain to get α takes at most O(N 3 ) operations. Writing the density under the form p d N -d k is linear in N and computing all h i,b,m takes O(N 3 ) operations using the formula (19). Therefore the algorithm runs in time O(N 3 ).

General case

The general case is more complicated since there can be some factors of shape (n 0 , k 0 ) with p + 1 (or p -1) nodes labeled by 1. However the structure of the minimal graph of strongly balanced trees made in Section 5.1.1 can be useful.

• Indeed, the minimal graph must have only one strongly connected component and it must corresponds to a strongly balanced tree.

• If the density of the strongly connected component is p 2 n 0 Ck0 , all factors of shape n 0 , k 0 in the strongly component have exactly p nodes labeled by 1.

Therefore, using the same techniques of reduction of the size as in Figure 11, one can show that we just have to test the balanced property for factors of shape at most (N, N ) where N is the number of vertices in the graph.

Counting

In this part, we address the problem of counting all possible factors of a mechanical tree. We will focus on trees of degree 2 and will compare this to the total number of possible factors of binary trees.

There are 2 n finite words on a binary alphabet of length n. Not all these words can be factors of a Sturmian words, for example 0011 can not since it is not balanced. In fact, the number of factors of length n of Sturmian words (see for example [START_REF] Berstel | Random generation of finite sturmian words[END_REF]) is:

where φ is the Euler function (φ(i) is the number of integers less than i and coprime with i). Asymptotically, the number of factors is equivalent to n 3 /π 2 . The number a n of unordered complete binary trees of height n satisfies the equation: This shows that the tree is balanced. It is not strongly balanced since there are factors of shape (1, 1) with 2 nodes labeled by one and others with 0 nodes labeled by one as seen in the bottom right part of figure 18. Also its minimal graph is not isomorphic to the unique minimal graph of a mechanical tree of density 4/7 that has only 3 nodes (see the discussion about graphs of strongly balanced tree in Section 5.1.1).

1 2 3 4

Figure 18: A Rational Balanced Tree that is not strongly balanced 10. Irrational balanced tree that is not strongly balanced -Building an irrational tree not strongly balanced requires more work. We consider a tree which has a root r labeled by 0 and two children that are mechanical trees of density α and respective phases φ and φ + a. We will see that under some conditions on α, φ and a, this will be an irrational tree that is balanced but not strongly balanced nor rational, nor Sturmian.

α, φ α, φ + a

The two children of the root are balanced trees which means that the tree is balanced if and only if for all n:

(2 n+1 -1)α ≤ h r (n + 1) ≤ (2 n+1 -1)α + 1.

Let us call k = (2 n -1)α + φ and x = frac((2 n -1)α + φ).

h r (n + 1) = (2 n -1)α + φ + (2 n -1)α + φ + a

As (2 n+1 -1)α = 2k + 2x + α -2φ, the equation 27 holds if for all x ∈ [0; 1), we have:

which holds if for all x ∈ [0; 1):

This equation is satisfied if and only if (x + a < 1 and -1 ≤ 2x -2φ + α < 1) or (x + a ≥ 1 and 0 ≤ 2x -2φ + α < 2).