Pattern avoidance in binary trees

# Pattern avoidance in binary trees

Eric S. Rowland Mathematics Department
Tulane University
New Orleans, LA 70118, USA
February 8, 2010
###### Abstract.

This paper considers the enumeration of trees avoiding a contiguous pattern. We provide an algorithm for computing the generating function that counts -leaf binary trees avoiding a given binary tree pattern . Equipped with this counting mechanism, we study the analogue of Wilf equivalence in which two tree patterns are equivalent if the respective -leaf trees that avoid them are equinumerous. We investigate the equivalence classes combinatorially. Toward establishing bijective proofs of tree pattern equivalence, we develop a general method of restructuring trees that conjecturally succeeds to produce an explicit bijection for each pair of equivalent tree patterns.

## 1. Introduction

Determining the number of words of length on a given alphabet that avoid a certain (contiguous) subword is a classical combinatorial problem that can be solved, for example, by the principle of inclusion–exclusion. An approach to this question using generating functions is provided by the Goulden–Jackson cluster method [6, 8], which utilizes only the self-overlaps (or “autocorrelations”) of the word being considered. A natural question is “When do two words have the same avoiding generating function?” That is, when are the -letter words avoiding (respectively) and equinumerous for all ? The answer is simple: precisely when their self-overlaps coincide. For example, the equivalence classes of length- words on the alphabet are as follows.

 equivalence classself-overlap lengths{0001,0011,0111,1000,1100,1110}{4}{0010,0100,0110,1001,1011,1101}{1,4}{0101,1010}{2,4}{0000,1111}{1,2,3,4}

In this paper we consider the analogous questions for plane trees. All trees in the paper are rooted and ordered.

Our focus will be on binary trees — trees in which each vertex has or (ordered) children. A vertex with children is a leaf, and a vertex with children is an internal vertex. A binary tree with leaves has internal vertices, and the number of such trees is the Catalan number . The first few binary trees are depicted in Figure 1. We use an indexing for -leaf binary trees that arises from the natural recursive construction of all -leaf binary trees by pairing each -leaf binary tree with each -leaf binary tree, for all . In practice it will be clear from context which tree we mean by, for example, ‘’.

Conceptually, a binary tree avoids a tree pattern if there is no instance of anywhere inside . Steyaert and Flajolet [13] were interested in such patterns in vertex-labeled trees. They were mainly concerned with the asymptotic probability of avoiding a pattern, whereas our focus is on enumeration. However, they establish in Section 2.2 that the total number of occurrences of an -leaf binary tree pattern in all -leaf binary trees is

 (2n−mn−m).

In this sense, all -leaf binary trees are indistinguishable; the results of this paper refine this statement by further distinguishing -leaf tree patterns by the number of -leaf trees containing precisely copies of each.

We remark that a different notion of tree pattern was later considered by Flajolet, Sipala, and Steyaert [5], in which every leaf of the pattern must be matched by a leaf of the tree. Such patterns are only matched at the bottom of a tree, so they arise naturally in the problem of compactly representing in memory an expression containing repeated subexpressions. The enumeration of trees avoiding such a pattern is simple, since no two instances of the pattern can overlap: The number of -leaf binary trees avoiding depends only on the number of leaves in . See also Flajolet and Sedgewick [4, Note III.40].

The reason for studying patterns in binary trees as opposed to rooted, ordered trees in general is that it is straightforward to determine what it should mean for a binary tree to avoid, for example,

 t7=\vbox\includegraphics[]{binarytree5-7},

whereas a priori it is ambiguous to say that a general tree avoids

 \vbox\includegraphics[]{tree5-8}.

Namely, for general trees, ‘matches a vertex with children’ for could mean either ‘has exactly children’ or ‘has at least children’. For binary trees, these are the same for , so there is no choice to be made.

However, it turns out that the notion of pattern avoidance for binary trees induces a well-defined notion of pattern avoidance for general trees. This arises via the natural bijection between the set of -leaf binary trees and the set of -vertex trees; using this bijection, one simply translates the problem into the setting of binary trees.

One main theoretical purpose of this paper is to provide an algorithm for computing the generating function that counts binary trees avoiding a certain tree pattern. This algorithm easily generalizes to count trees containing a prescribed number of occurrences of a certain pattern, and additionally we consider the number of trees containing several patterns each a prescribed number of times. All of these generating functions are algebraic. Section 4 is devoted to these algorithms, which are implemented in TreePatterns [9], a Mathematica package available from the author’s website.

By contrast, another main purpose of this paper is quite concrete, and that is to determine equivalence classes of binary trees. We say that two tree patterns and are equivalent if for all the number of -leaf binary trees avoiding is equal to the number of -leaf binary trees avoiding . In other words, equivalent trees have the same generating function with respect to avoidance. This is the analogue of Wilf equivalence in permutation patterns. Each tree is trivially equivalent to its left–right reflection, but there are other equivalences as well. The first few classes are presented in Section 3. The appendix contains a complete list of equivalence classes of binary trees with at most leaves, from which we draw examples throughout the paper. Classes are named with the convention that class  is the th class of -leaf binary trees.

We seek to understand equivalence classes of binary trees combinatorially, and this is the third purpose of the paper. By analogy with words, one might hope for a simple criterion such as “ and are equivalent precisely when the lengths of their self-overlaps coincide”; however, although the set of self-overlap lengths seems to be preserved under equivalence, this statement is not true, for corresponds to both classes 6.3 and 6.7. In lieu of a simple criterion, we look for bijections. As discussed in Section 3.5, in a few cases there is a bijection between -leaf binary trees avoiding a certain pattern and Dyck -words avoiding a certain (contiguous) subword. In general, when and are equivalent tree patterns, we would like to provide a bijection between trees avoiding and trees avoiding . Conjecturally, all classes of binary trees can be established bijectively by top-down and bottom-up replacements; this is the topic of Section 5. Nearly all bijections in the paper are implemented in the package TreePatterns.

Aside from mathematical interest, a general study of pattern avoidance in trees has applications to any collection of objects related by a tree structure, such as people in a family tree or species in a phylogenetic tree. In particular, this paper answers the following question. Given related objects (e.g., species) for which the exact relationships aren’t known, how likely is it that some prescribed (e.g., evolutionary) relationship exists between some subset of them? (Unfortunately, it probably will not lead to insight regarding the practical question “What is the probability of avoiding a mother-in-law?”) Alternatively, we can think of trees as describing the syntax of sentences in natural language or of fragments of computer code; in this context the paper answers questions about the occurrence and frequency of given phrase substructures.

## 2. Definitions

### 2.1. Avoidance

The more formal way to think of an -vertex tree is as a particular arrangement of pairs of parentheses, where each vertex is represented by the pair of parentheses containing its children. For example, the tree

 T=\vbox\includegraphics[]{binarytree3-1}

is represented by (()(()())). This is the word representation of this tree in the alphabet . We do not formally distinguish between the graphical representation of a tree and the word representation, and it is the latter that is useful in manipulating trees algorithmically. (Mathematica’s pattern matching capabilities provide a convenient tool for working with trees represented as nested lists, so this is the convention used by TreePatterns.)

Informally, our concept of containment is as follows. A binary tree contains if there is a (contiguous, rooted, ordered) subtree of that is a copy of . For example, consider

 t=\vbox\includegraphics[]{binarytree3-1}.

None of the trees

contains a copy of , while each of the trees

contains precisely one copy of , each of the trees

contains precisely two (possibly overlapping) copies of , and the tree

contains precisely three copies of . This is a classification of binary trees with at most leaves according to the number of copies of .

We might formalize this concept with a graph theoretic definition as follows. Let be a binary tree. A copy of in is a subgraph of (obtained by removing vertices) that is isomorphic to (preserving edge directions and the order of children). Naturally, avoids if the number of copies of in is .

An equivalent but much more useful definition is a language theoretic one, and to provide this we first distinguish a tree pattern from a tree.

By ‘tree pattern’, informally we mean a tree whose leaves are “blanks” that can be filled (matched) by any tree, not just a single vertex. More precisely, let , and let be the language on containing (the word representation of) every binary tree. Consider a binary tree , and let be the word on the three symbols obtained by replacing each leaf () in by . We call the tree pattern of . This tree pattern naturally generates a language on , which we obtain by interpreting the word as a product of the three languages . Informally, is the set of words that match . We think of and interchangeably. (Note that a tree is a tree pattern matched only by itself.)

For example, let

 τ=\vbox\includegraphics[]{binarytree3-1}=(()(()()));

then the corresponding tree pattern is , and the language consists of all trees of the form , where are binary trees.

Let denote the set of all finite words on . The language is the set of all binary trees whose word has a subword in . Therefore we say that a binary tree contains the tree pattern if is in the language . We can think of this language as a multiset, where a given tree occurs with multiplicity equal to the number of ways that it matches . Then the number of copies of in is the multiplicity of in .

Continuing the example from above, the tree

 T=\vbox\includegraphics[]{binarytree5-3}=(()((()())(()())))

contains copies of since it matches in ways: with and , and with and .

Our notation distinguishes tree patterns from trees: Tree patterns are represented by lowercase variables, and trees are represented by uppercase variables. To be absolutely precise, we would graphically distinguish between terminal leaves () of a tree and blank leaves of a tree pattern, but this gets in the way of speaking about them as the same objects, which is rather convenient.

In Sections 4 and 5 we will be interested in taking the intersection of tree patterns and (by which we mean the intersection of the corresponding languages and ). The intersection of two or more explicit tree patterns can be computed recursively: , and .

### 2.2. Generating functions

Our primary goal is to determine the number of binary trees with vertices that avoid a given binary tree pattern , and more generally to determine the number of binary trees with vertices and precisely copies of . Thus we consider two generating functions associated with : the avoiding generating function

 Avt(x)=∑T avoids txnumber of vertices% in T=∞∑n=0anxn

and the enumerating generating function

 EnL,t(x,y) =∑T∈Lxnumber of vertices in Tynumber% of copies of t in T =∞∑kn=0∞∑k=0an,kxnyk.

The avoiding generating function is the special case .

###### Theorem 1.

is algebraic.

The proof is constructive, so it enables us to compute , and in particular , for explicit tree patterns. We postpone the proof until Section 4.2 to address a natural question that arises: Which trees have the same generating function? That is, for which pairs of binary tree patterns and are the -leaf trees avoiding (or containing copies of) these patterns equinumerous?

We say that and are avoiding-equivalent if . We say they are enumerating-equivalent if the seemingly stronger condition holds. We can compute these equivalence classes explicitly by computing and for, say, all -leaf binary tree patterns . In doing this for binary trees with up to leaves, one comes to suspect that these conditions are in fact the same.

###### Conjecture 2.

If and are avoiding-equivalent, then they are also enumerating-equivalent.

In light of this experimental result, we focus attention in the remainder of the paper on classes of avoiding-equivalence, since conjecturally they are the same as classes of enumerating-equivalence.

## 3. Initial inventory and some special bijections

In this section we undertake an analysis of small patterns. We determine for binary tree patterns with at most leaves using methods specific to each. This allows us to establish the equivalence classes in this range.

### 3.1. 1-leaf trees

There is only one binary tree pattern with a single leaf, namely

 t=\vbox\includegraphics[]{binarytree1-1}=L.

Every binary tree contains at least one vertex, so . The number of binary trees with vertices is , so

 EnL(x)=x+x3+2x5+5x7+14x9+42x11+⋯=∞∑n=1Cn−1x2n−1.

### 3.2. 2-leaf trees

There is also only one binary tree pattern with precisely leaves:

 t=\vbox\includegraphics[]{binarytree2-1}={(}LL{)}.

However, is a fairly fundamental structure in binary trees; the only tree avoiding it is the -vertex tree (). Thus , and

 EnL,t(x,y)=∞∑n=1Cn−1x2n−1yn−1=1−√1−4x2y2xy.

### 3.3. 3-leaf trees

There are binary trees with leaves, and they are equivalent by left–right reflection:

 \vbox\includegraphics[]{binarytree3-1}and\vbox%\includegraphics[]binarytree3−2.

There is only one binary tree with leaves avoiding

 \vbox\includegraphics[]{binarytree3-2}={(}{(% }LL{)}L{)},

namely the “right comb” . Therefore for these trees

 Avt(x)=x+x3+x5+x7+x9+x11+⋯=x1−x2.

### 3.4. 4-leaf trees

Among -leaf binary trees we find more interesting behavior. There are such trees, pictured as follows.

They comprise equivalence classes.

#### Class 4.1

The first equivalence class consists of the trees

 t1=\vbox\includegraphics[]{binarytree4-1}andt5=\vbox\includegraphics[]{binarytree4-5}.

The avoiding generating function for each of these trees satisfies

 x3f2+(x2−1)f+x=0

because the number of -leaf binary trees avoiding is the Motzkin number :

 Avt(x)=x+x3+2x5+4x7+9x9+21x11+⋯=∞∑n=1Mn−1x2n−1.

This fact is presented by Donaghey and Shapiro [3] as their final example of objects counted by the Motzkin numbers. They provide a bijective proof which we reformulate here. Specifically, there is a natural bijection between the set of -leaf binary trees avoiding and the set of Motzkin paths of length — paths from to composed of steps , , that do not go below the -axis. We represent a Motzkin path as a word on encoding the sequence of steps under .

Let be the usual bijection from -leaf binary trees to -vertex general trees that operates by contracting every rightward edge. To obtain the Motzkin path associated with a binary tree avoiding :

1. Let . No vertex in has more than children, since

 β(t1)=\vbox\includegraphics[]{tree4-5}

and avoids .

2. Create a word on by traversing in depth-first order (i.e., for each subtree visit first the root vertex and then its children trees in order); for each vertex, record less than the number of children of that vertex.

3. Delete the last letter of (which is ).

The resulting word contains the same number of s and s, and every prefix contains at least as many s as s, so it is a Motzkin path. The steps are easily reversed to provide the inverse map from Motzkin paths to binary trees avoiding . (For the larger context of this bijection, see Stanley’s presentation leading up to Theorem 5.3.10 [12].)

#### Class 4.2

The second equivalence class consists of the three trees

 t2=\vbox\includegraphics[]{binarytree4-2},t3=\vbox% \includegraphics[]{binarytree4-3},andt4=\vbox% \includegraphics[]{binarytree4-4}

and provides the smallest example of nontrivial equivalence. Symmetry gives . To establish , for each of these trees we give a bijection between -leaf binary trees avoiding and binary words of length . By composing these two maps we obtain a bijection between trees avoiding and trees avoiding .

First consider

 t3=\vbox\includegraphics[]{binarytree4-3}.

If avoids , then no vertex of has four grandchildren; that is, at most one of a vertex’s children has children of its own. This implies that at each generation at most one vertex has children. Since there are two vertices at each generation after the first, the number of such -leaf trees is for :

 Avt3(x)=x+x3+2x5+4x7+8x9+16x11+⋯=x+∞∑n=22n−2x2n−1=x(1−x2)1−2x2.

Form a word corresponding to by letting the th letter be or depending on which vertex (left or right) on level has children.

Now consider

 t2=\vbox\includegraphics[]{binarytree4-2}.

A “typical” binary tree avoiding looks like

and is determined by the length of its spine and the length of each arm. Starting from the root, traverse the internal vertices of a tree avoiding according to the following rule. Always move to the right child of a vertex when the right child is an internal vertex, and if the right child is a leaf then move to the highest unvisited internal spine vertex. By recording and for left and right movements in this traversal, a word on is produced that encodes uniquely. We have since we obtain one symbol from each internal vertex except the root. Since every word corresponds to an -leaf binary tree avoiding , there are such trees.

More formally, let be a map from binary trees to binary words defined by , where

 κi(T)={ϵif T={(}{)% }iω(T)otherwise.

Then the word corresponding to is .

For the inverse map , begin with the word . Then read left to right. When the symbol is read, replace the existing by ; when is read, replace the existing by () and the existing by . After the entire word is read, replace the remaining and with (). One verifies that has leaves. The tree avoids because the left child of an vertex never has children of its own.

### 3.5. Bijections to Dyck words

We mention that in some cases the set of trees avoiding a pattern is in bijection to the set of Dyck words avoiding a certain subword. For example, trees avoiding a pattern in class 4.1 are in bijection to Dyck words avoiding the subword . Recall from Section 3.4. For we have

 β(\vbox\includegraphics[]{binarytree4-5})=\vbox% \includegraphics[]{tree4-1},

whose corresponding Dyck word is , and has the feature that contains if and only if the Dyck word corresponding to contains .

In general there is a bijection between -leaf binary trees avoiding and -Dyck words avoiding whenever is a characteristic feature of , that is, some feature of the tree that is preserved locally by .

For example, for in class 4.2 we observe that

 β(\vbox\includegraphics[]{binarytree4-2})=\vbox% \includegraphics[]{tree4-3}=010011,

and containing as a subword is necessary and sufficient for the corresponding tree to contain . Thus binary trees avoiding a pattern in class 4.2 are in bijection to Dyck words avoiding . Other bijections can be found similarly: Classes 5.2, 5.3, 6.3, and 6.6 correspond to the words , , , and .

Notes on sequences counting Dyck words avoiding a subword have been contributed by David Callan and Emeric Deutsch to the Encyclopedia of Integer Sequences [11]. The subject appears to have begun with Deutsch [2, Section 6.17], who enumerated Dyck words according to the number of occurrences of . Sapounakis, Tasoulas, and Tsikouras [10] have considered additional subwords. Via the bijections just described, their results provide additional derivations of the generating functions .

## 4. Algorithms

In this section we provide algorithms for computing algebraic equations satisfied by , , and the more general defined in Section 4.3. Computing or for all -leaf binary tree patters allows one to automatically determine the equivalence classes given in the appendix.

We draw upon the notation introduced in Section 2.1. In particular, the intersection of two tree patterns plays a central role. Recall that is the set of trees matching at the top level.

The depth of a vertex in a tree is the length of the minimal path to that vertex from the root, and is the maximum vertex depth in the tree .

### 4.1. Avoiding a single tree

Fix a binary tree pattern we wish to avoid. For a given tree pattern , we will make use of the generating function

 weight(p)=weight(Lp):=∑T∈Lpweight(T),

where

 weight(T)={xnumber of vertices in T%if$T$avoids$t$0if T contains t.

The case was covered in Section 3.1, so we assume . Then for some tree patterns and . Since matches precisely when matches and matches , we have

 (1) weight({(}plpr{)})=x⋅(weight(pl)⋅weight(pr)−weight(pl∩tl)⋅weight(pr∩tr)).

The coefficient is the weight of the root vertex of that we destroy in separating this pattern into its two subpatterns.

We now construct a polynomial (with coefficients that are polynomials in ) that is satisfied by , the weight of the language of binary trees. The algorithm is as follows.

Begin with the equation

 weight(L)=weight({(}{)})+weight({(}LL{)}).

The variable is “new”; we haven’t yet written it in terms of other variables. So use Equation (1) to rewrite . For each expression that is introduced, we compute the intersection . This allows us to write as for some pattern that is simply a word on (i.e., does not contain the operator).

For each new variable , we obtain a new equation by making it the left side of Equation (1), and then as before we eliminate by explicitly computing intersections.

We continue in this manner until there are no new variables produced. This must happen because , so since there are only finitely many trees that are shallower than , there are only finitely many variables in this system of polynomial equations.

Finally, we compute a Gröbner basis for the system in which all variables except and are eliminated. This gives a single polynomial equation in these variables, establishing that is algebraic.

Let us work out an example. We use the graphical representation of tree patterns with the understanding that the leaves are blanks. Consider the tree pattern

 t=\vbox\includegraphics[]{binarytree5-2}={(}L%(L{(}{(}LL{)}L{)}{)}{)}

from class 5.2. The first equation is

 weight(\vbox\scalebox{.7}{\includegraphics[]{binarytree1-1% }})=x+weight(\vbox\scalebox{.7}{\includegraphics[]{% binarytree2-1}}).

We have and , so Equation (1) gives

 weight(\vbox\scalebox{.7}{\includegraphics[]{% binarytree2-1}}) =x⋅(weight(\vbox\scalebox{.7}{% \includegraphics[]{binarytree1-1}})⋅weight(\vbox% \scalebox{.7}{\includegraphics[]{binarytree1-1}})−weight(\vbox\scalebox{.7}{\includegraphics[]{binarytree1-1}}∩\vbox% \scalebox{.7}{\includegraphics[]{binarytree1-1}})⋅weight(\vbox\scalebox{.7}{\includegraphics[]{binarytree1-1}}∩\vbox% \scalebox{.7}{\includegraphics[]{binarytree4-2}})) =x⋅(weight(\vbox\scalebox{.7}{% \includegraphics[]{binarytree1-1}})2−weight(\vbox% \scalebox{.7}{\includegraphics[]{binarytree1-1}})⋅weight(\vbox\scalebox{.7}{\includegraphics[]{binarytree4-2}}))

since for any tree pattern . The variable is new, so we put it into Equation (1):

 weight(\vbox\scalebox{.7}{\includegraphics[]{% binarytree4-2}}) =x⋅(weight(\vbox\scalebox{.7}{% \includegraphics[]{binarytree1-1}})⋅weight(\vbox% \scalebox{.7}{\includegraphics[]{binarytree3-2}})−weight(\vbox\scalebox{.7}{\includegraphics[]{binarytree1-1}}∩\vbox% \scalebox{.7}{\includegraphics[]{binarytree1-1}})⋅weight(\vbox\scalebox{.7}{\includegraphics[]{binarytree3-2}}∩\vbox% \scalebox{.7}{\includegraphics[]{binarytree4-2}})) =x⋅(weight(\vbox\scalebox{.7}{% \includegraphics[]{binarytree1-1}})⋅weight(\vbox% \scalebox{.7}{\includegraphics[]{binarytree3-2}})−weight(\vbox\scalebox{.7}{\includegraphics[]{binarytree1-1}})⋅% weight(\vbox\scalebox{.7}{\includegraphics[]{binarytree5-7}})).

There are two new variables:

 weight(\vbox\scalebox{.7}{\includegraphics[]{% binarytree3-2}}) =x⋅(weight(\vbox\scalebox{.7}{% \includegraphics[]{binarytree2-1}})⋅weight(\vbox% \scalebox{.7}{\includegraphics[]{binarytree1-1}})−weight(\vbox\scalebox{.7}{\includegraphics[]{binarytree2-1}}∩\vbox% \scalebox{.7}{\includegraphics[]{binarytree1-1}})⋅weight(\vbox\scalebox{.7}{\includegraphics[]{binarytree1-1}}∩\vbox% \scalebox{.7}{\includegraphics[]{binarytree4-2}})) =x⋅(weight(\vbox\scalebox{.7}{% \includegraphics[]{binarytree2-1}})⋅weight(\vbox% \scalebox{.7}{\includegraphics[]{binarytree1-1}})−weight(\vbox\scalebox{.7}{\includegraphics[]{binarytree2-1}})⋅% weight(\vbox\scalebox{.7}{\includegraphics[]{binarytree4-2}})); weight(\vbox\scalebox{.7}{\includegraphics[]{% binarytree5-7}}) =x⋅(weight(\vbox\scalebox{.7}{% \includegraphics[]{binarytree2-1}})⋅weight(\vbox% \scalebox{.7}{\includegraphics[]{binarytree3-2}})−weight(\vbox\scalebox{.7}{\includegraphics[]{binarytree2-1}}∩\vbox% \scalebox{.7}{\includegraphics[]{binarytree1-1}})⋅weight(\vbox\scalebox{.7}{\includegraphics[]{binarytree3-2}}∩\vbox% \scalebox{.7}{\includegraphics[]{binarytree4-2}})) =x⋅(weight(\vbox\scalebox{.7}{% \includegraphics[]{binarytree2-1}})⋅weight(\vbox% \scalebox{.7}{\includegraphics[]{binarytree3-2}})−weight(\vbox\scalebox{.7}{\includegraphics[]{binarytree2-1}})⋅% weight(\vbox\scalebox{.7}{\includegraphics[]{binarytree5-7}})).

We have no new variables, so we eliminate the four auxiliary variables

 weight(\vbox\scalebox{.7}{\includegraphics[]{binarytree2-1% }}),weight(\vbox\scalebox{.7}{\includegraphics[]{% binarytree3-2}}),weight(\vbox\scalebox{.7}{% \includegraphics[]{binarytree4-2}}),weight(\vbox% \scalebox{.7}{\includegraphics[]{binarytree5-7}})

from this system of five equations to obtain

 x3weight(\vbox\scalebox{.7}{\includegraphics[]{% binarytree1-1}})2−(x2−1)2weight(\vbox% \scalebox{.7}{\includegraphics[]{binarytree1-1}})−x(x2−1)=0.

### 4.2. Enumerating with respect to a single tree

To prove Theorem 1, we make a few modifications in order to compute instead of . Again

 weight(p):=∑T∈Lpweight(T),

but now for all . We modify Equation (1) to become

 (2) weight({(}plpr{)})=x⋅(weight(pl)⋅weight(pr)+(y−1)⋅weight(pl∩tl)⋅weight(pr∩tr))

since in addition to accounting for the trees that avoid we also account for those that match , in which case is contributed.

The rest of the algorithm carries over unchanged, and we obtain a polynomial equation in , , and .

### 4.3. Enumerating with respect to multiple trees

A more general question is the following. Given several binary tree patterns , what is the number of binary trees containing precisely vertices, copies of , …, copies of ? We consider the enumerating generating function

 EnL,p1,…,pk(xL,xp1,…,xpk) =∑T∈Lxα0Lxα1p1⋯xαkpk =∞∑n0=0∞∑n1=0⋯∞∑nk=0an0,n1,…,nkxn0Lxn1p1⋯xnkpk,

where and is the number of copies of in . (We need not assume that the are distinct.) This generating function can be used to obtain information about how correlated a family of tree patterns is. We have the following generalization of Theorem 1.

###### Theorem 3.

is algebraic.

Keeping track of multiple tree patterns is not much more complicated than handling a single pattern, and the algorithm for doing so has the same outline. Let

 weight(p):=∑T∈Lpweight(T)

with

 weight(T)=xα0Lxα1p1⋯xαkpk,

where again is the number of copies of in . Let . First we describe what to do with each new variable that arises. The approach used is different than that for one tree pattern; in particular, we do not make use of intersections. Consequently, it is less efficient.

Let be the number of leaves in . If is a tree matching , then for each leaf of there are two possibilities: Either is matched by a terminal vertex () in , or is matched by a tree matching . For each leaf we make this choice independently, thus partitioning the language into disjoint sets represented by tree patterns that are disjoint in the sense that each tree matching matches precisely one of these patterns. For example, partitioning the pattern into patterns gives

 (3) weight({(}LL{)})=weight({(}{(}{)}{(}{)}{)})+weight({(}{(}{% )}{(}LL{)}{)})+weight({(}{(}LL{)}{(}% {)}{)})+weight({(}{(}LL{)}{(}LL{)}{)}).

We need an analogue of Equation (2) for splitting a pattern into the two subpatterns and . For this, examine each of the patterns that arose in partitioning . For each pattern whose language is infinite (that is, the word contains the symbol ) and has , rewrite

 weight(p)=weight(pl)⋅weight(pr)⋅∏0≤i≤kp matches pixpi,

where ‘ matches ’ means that every tree in matches (so ). If is infinite but , keep intact as a variable.

Finally, for all tree patterns whose language is finite (i.e., is a tree), rewrite

 weight(p)=∏0≤i≤kxnumber of copies % of pi in ppi.

The algorithm is as follows. As before, begin with the equation

 weight(L)=weight({(}{)})+weight({(}LL{)}).

At each step, take each new variable and obtain another equation by performing the procedure described: Write it as the sum of other variables, split the designated patterns into subpatterns, and explicitly compute the weights of any trees appearing. Continue in this manner until there are no new variables produced; this must happen because we break up whenever , so there are only finitely many possible variables. Eliminate from this system of polynomial equations all but the variables , , …, , and to obtain a polynomial equation satisfied by .

## 5. Replacement bijections

In this section we address the question of providing systematic bijective proofs of avoiding-equivalence. Given two equivalent binary tree patterns and , we would like to produce an explicit bijection between binary trees avoiding and binary trees avoiding . It turns out that this can often be achieved by structural replacements on trees. We start by describing an example in full, and later generalize.

### 5.1. An example replacement bijection

Consider the trees

 t2=\vbox\includegraphics[]{binarytree4-2}andt3=\vbox\includegraphics[]{binarytree4-3}

in class 4.2. The idea is that since -leaf trees avoiding are in bijection to -leaf trees avoiding , then somehow swapping all occurrences of these two tree patterns should produce a bijection. However, since the patterns may overlap, it is necessary to specify an order in which to perform the replacements. A natural order is to start with the root and work down the tree. More precisely, a top-down replacement is a restructuring of a tree in which we iteratively apply a set of transformation rules to subtrees of , working downward from the root.

Take the replacement rule to be

 \vbox\includegraphics[]{binarytree4-3abcd}→\vbox% \includegraphics[]{binarytree4-2cabd},

where the variables represent trees attached at the leaves, rearranged according to the permutation . Begin at the root: If itself matches the left side of the rule, then we restructure according to the rule; if not, we leave unchanged. Then we repeat the procedure on the root’s (new) children, then on their children, and so on, so that each vertex in the tree is taken to be the root of a subtree which is possibly transformed by the rule. For example,

 \vbox\includegraphics[]{binarytree9-1243}→\vbox% \includegraphics[]{binarytree9-1085}→\vbox\includegraphics[]{% binarytree9-1037}→\vbox\includegraphics[]{binarytree9-1033}

shows the three replacements required to compute the image (on the right) of a tree avoiding . The resulting tree avoids .

This top-down replacement is invertible. The inverse map is a bottom-up replacement with the inverse replacement rule,

Rather than starting at the root and working down the tree, we apply this map by starting at the leaves and working up the tree.

We now show that the top-down replacement is in fact a bijection from trees avoiding to trees avoiding . It turns out to be the same bijection given in Section 3.4 via words in .

Assume avoids ; we show that the image of under the top-down replacement avoids . It is helpful to think of as broken up into (possibly overlapping) “spheres of influence” — subtrees which are maximal with respect to the replacement rule in the sense that performing the top-down replacement on the subtree does not introduce instances of the relevant tree patterns containing vertices outside of the subtree. It suffices to consider each sphere of influence separately. A natural focal point for each sphere of influence is the highest occurrence of . We verify that restructuring this to under the top-down replacement produces no above, at, or below the root of the new in the image of .

1. Since has depth , can occur at most one level above the root of the new while overlapping it. Thus it suffices to consider all subtrees with occurring at level . There are two cases,

 \vbox\includegraphics[]{binarytree5-3eabcd}and\vbox\includegraphics[]{binarytree5-12abcde}.

The first case does not avoid , so it does not occur in . The second case may occur in . However, we do not want the subtree itself to match (because we assume that the at level is the highest in this sphere of influence), so we must have . Thus this subtree is transformed by the top-down replacement as

 \vbox\includegraphics[]{binarytree5-12abcd-}→\vbox% \includegraphics[]{binarytree5-11cabd-}.

The image does not match at the root, so does not appear above the root of the new .

2. Since avoids , every subtree in matching in fact matches the pattern . Such a subtree is restructured as

 \vbox\includegraphics[]{binarytree4-3ab-d}→\vbox% \includegraphics[]{binarytree4-2-abd}

under the replacement rule, and the image does not match (because is terminal). Therefore the new cannot itself match .

3. A general subtree matching and avoiding is transformed as

 \vbox\includegraphics[]{below1}→\vbox\includegraphics[]{% below2}→\vbox\includegraphics[]{below3}→⋯→\vbox\includegraphics[]{below4}→⋯.

Clearly can only occur in the image at or below the subtree . However, since is preserved by the replacement rule, any transformations on can be considered independently. That is, is the top of a different sphere of influence, so we need not consider it here. We conclude that does not occur below the root of the new .

If we already knew that and are equivalent (for example, by having computed as in Section 4.1), then we have now obtained a bijective proof of their equivalence. Otherwise, it remains to show that if avoids , then performing the bottom-up replacement produces a tree that avoids ; this can be accomplished similarly.

### 5.2. General replacement bijections

A natural question is whether for any two equivalent binary tree patterns and there exists a sequence of replacement bijections and left–right reflections that establishes their equivalence. For tree patterns of at most leaves the answer is “Yes”, which perhaps suggests that these maps suffice in general.

###### Conjecture 4.

If and are equivalent, then there is a sequence of top-down replacements, bottom-up replacements, and left–right reflections that produces a bijection from binary trees avoiding to binary trees avoiding .

In this section we discuss qualitative results regarding this conjecture.

Given two -leaf tree patterns and , one can ask which permutations of the leaves give rise to a top-down replacement that induces a bijection from trees avoiding to trees avoiding . Most permutations are not viable. Candidate permutations can be found experimentally by simply testing all permutations of leaves on a set of randomly chosen binary trees avoiding ; one checks that the image avoids and that composing the top-down replacement with the inverse bottom-up replacement produces the original tree. This approach is feasible for small , but it is slow and does not provide any insight into why certain trees are equivalent. A question unresolved at present is to efficiently find all such bijections.

We return briefly to the replacement rule of Section 5.1 to mention that a minor modification produces a bijection on the full set of binary trees. Namely, take the two replacement rules

Again we perform a top-down replacement, now using both rules together. That is, if a subtree matches the left side of either rule, we restructure it according to that rule. Of course, it can happen that a particular subtree matches both replacement rules, resulting in potential ambiguity; in this case which do we apply? Well, if both rules result in the same transformation, then it does not matter, and indeed with our present example this is the case. To show this, it suffices to take the intersection of the two left sides and label the leaves to represent additional branches that may be present:

 \vbox\includegraphics[]{binarytree5-7labeled}.

Now we check that applying each of the two replacement rules to this tree produces the same labeled tree, namely

 \vbox\includegraphics[]{binarytree5-7labeledimage}.

Therefore we need not concern ourselves with which rule is applied to a given subtree that matches both. Since the replacement rules agree on their intersection, the top-down replacement is again invertible and is therefore a bijection from the set of binary trees to itself. By the examination of cases in Section 5.1, this bijection is an extension of the bijection between binary trees avoiding and binary trees avoiding .

Thus we may choose from two types of bijection when searching for top-down replacement bijections that prove avoiding-equivalence. The first type is from binary trees avoiding to binary trees avoiding , using one rule for the top-down direction and the inverse for the bottom-up direction; these bijections in general do not extend to bijections on the full set of binary trees. The second type is a bijection on the full set of binary trees, using both rules in each direction, that induces a bijection from binary trees avoiding to binary trees avoiding .

We conclude with a curious example in which two tree patterns can only be proven equivalent by a two-rule bijection that does not involve them directly. The trees

 t7=\vbox\includegraphics[]{binarytree6-7}andt11=\vbox\includegraphics[]{binarytree6-11}

in class 6.5 are avoiding-equivalent by the permutation , but neither

 t17=\vbox\includegraphics[]{binarytree6-17}

nor its left–right reflection has an equivalence-proving permutation to , , or their left–right reflections. Thus, this equivalence cannot be established by a bijection that swaps -leaf tree patterns. However, it can be established by a bijection that swaps -leaf tree patterns: The previously mentioned bijection consisting of the two replacement rules

induces a top-down replacement bijection from trees avoiding to trees avoiding . The reason is that and are formed by two overlapping copies of the class 4.2 trees

 \vbox\includegraphics[]{binarytree4-2}and\vbox%\includegraphics[]binarytree4−3

respectively, and that and are mapped to each other under this bijection.

## Acknowledgements

I thank Phillipe Flajolet for helping me understand some existing literature, and I thank Lou Shapiro for suggestions which clarified some points in the paper. Thanks to the referee for the reference to Stanley’s book.

I am indebted to Elizabeth Kupin for much valuable feedback. Her comments greatly improved the exposition and readability of the paper. In addition, the idea of looking for one-rule bijections that do not extend to bijections on the full set of binary trees is hers, and this turned out to be an important generalization of the two-rule bijections I had been considering.

## Appendix. Table of equivalence classes

This appendix lists equivalence classes of binary trees with at most leaves. Left–right reflections are omitted for compactness. For each class we provide a polynomial equation satisfied by ; an equation satisfied by is obtained in each case by letting .

The data was computed by the Mathematica package TreePatterns [9] using Singular via the interface package by Manuel Kauers and Viktor Levandovskyy [7]. Pre-computed data extended to -leaf binary trees is now also available in TreePatterns. The number of equivalence classes of -leaf binary trees for is .

#### Class 1.1 (1 tree)

 xyf2−f+xy=0

#### Class 2.1 (1 tree)

 xyf2−f+x=0

#### Class 3.1 (2 trees)

 xyf2+(−x2(y−1)−1)f+x=0

#### Class 4.1 (2 trees)

 (xy−x3(y−1))f2+(−x2(y−1)−1)f+x=0

#### Class 4.2 (3 trees)

 xyf2+(−2x2(y−1)−1)f+(x3(y−1)+x)=0

#### Class 5.1 (2 trees)

 −x4(y−1)f3+(xy−x3(y−1))f2+(−x2(y−1)−1)f+x=0

#### Class 5.2 (10 trees)

 (xy−x3(y−1))f2+(x2(x2−2)(y−1)−1)f+(x3(y−1)+x)=0

#### Class 5.3 (2 trees)

 xyf3+(−3x2(y−1)−1)f2+(3x3(y−1)+x)f−x4(y−1)=0

#### Class 6.1 (2 trees)

 −x5(y−1)f4−x4(y−1)f3+(xy−x3(y−1))f2+(−x2(y−1)−1)f+x=0

#### Class 6.2 (8 trees)

 −x4(y−1)f3+x(x2(x2−1)(y−1)+y)f2+(x2(x2−2)(y−1)−1)f+(x3(y−1)+x)=0

#### Class 6.3 (14 trees)

 x(x2(x2−2)(y−1)+y)f2+(2x2(x2−1)(y−1)−1)f+(x3(y−1)+x)=0

#### Class 6.4 (8 trees)

 (xy−x3(y−1))f3+(x2(2x2−3)(y−1)−1)f2+(−x5(y−1)+3x3(y−1)+x)f−x4(y−1)=0

#### Class 6.6 (2 trees)

 −xyf4+(4x2(y−1)+1)f3+(−6x3(y−1)−x)f2+4x4(y−1)f−x5(y−1)=0

#### Class 6.7 (2 trees)

 x4(x2(y−1)−y)(y−1)f3+(−2x7(y−1)2+x5(y−1)(3y−2)−x3(y−1)+xy)f2+(x2(x6(y−1)−3x4(y−1)+x2−2)(y−1)−1)f+(x7(y−1)2+x3(y−1)+x)=0

## References

• [1]
• [2] Emeric Deutsch, Dyck path enumeration, Discrete Mathematics 204 (1999) 167–202.
• [3] Robert Donaghey and Louis Shapiro, Motzkin numbers, Journal of Combinatorial Theory, Series A 23 (1977) 291–301.
• [4] Philippe Flajolet and Robert Sedgewick, Analytic Combinatorics, Cambridge University Press, 2009.
• [5] Philippe Flajolet, Paolo Sipala, and Jean-Marc Steyaert, Analytic variations on the common subexpression problem, Lecture Notes in Computer Science: Automata, Languages, and Programming 443 (1990) 220–234.
• [6] Ian Goulden and David Jackson, An inversion theorem for cluster decompositions of sequences with distinguished subsequences, Journal of the London Mathematical Society (second series) 20 (1979) 567–576.
• [7] Manuel Kauers and Viktor Levandovskyy, Singular [a Mathematica package], available from http://www.risc.uni-linz.ac.at/research/combinat/software/Singular/index.html.
• [8] John Noonan and Doron Zeilberger, The Goulden–Jackson cluster method: extensions, applications, and implementations, Journal of Difference Equations and Applications 5 (1999) 355–377.
• [9] Eric Rowland, TreePatterns [a Mathematica package], available from the author’s web site.
• [10] Aris Sapounakis, Ioannis Tasoulas, and Panos Tsikouras, Counting strings in Dyck paths, Discrete Mathematics 307 (2007) 2909–2924.
• [11] Neil Sloane, The Encyclopedia of Integer Sequences, http://www.research.att.com/~njas/sequences.
• [12] Richard Stanley, Enumerative Combinatorics volume 2, Cambridge University Press, New York, 1999.
• [13] Jean-Marc Steyaert and Philippe Flajolet, Patterns and pattern-matching in trees: an analysis, Information and Control 58 (1983) 19–58.