In this paper we consider the enumeration of binary trees avoiding non-contiguous binary tree patterns. We begin by computing closed formulas for the number of trees avoiding a single binary tree pattern with 4 or fewer leaves and compare these results to analogous work for contiguous tree patterns. Next, we give an explicit generating function that counts binary trees avoiding a single non-contiguous tree pattern according to number of leaves and show that there is exactly one Wilf class of -leaf tree patterns for any positive integer . In addition, we enumerate binary trees that simultaneously avoid more than one tree pattern. Finally, we explore connections between pattern-avoiding trees and pattern-avoiding permutations.
Non-Contiguous Pattern Avoidance in Binary Trees
Michael Dairyko111Partially supported by NSF grant DMS-0851721
Department of Mathematics
Claremont, CA 91711, USA
Lara Pudwell11footnotemark: 1
Department of Mathematics and Computer Science
Valparaiso, IN 46383, USA
Samantha Tyner11footnotemark: 1
Department of Mathematics
Rock Island, IL 61201, USA
Casey Wynn11footnotemark: 1
Department of Mathematics and Computer Science
Conway, AR 72032, USA
The notion of one object avoiding another has been studied in permutations, words, partitions, and graphs. Much recent work has been devoted to the study of pattern-avoiding permutations. Given permutations and , we say that contains as a (classical) pattern if there exist indices such that is order-isomorphic to ; that is, if and only if . Otherwise, is said to avoid . For example contains the pattern as evidenced by , , and , but avoids the pattern because has no decreasing subsequence of length 3. One variation on this definition of pattern avoidance is to place the restriction on the indices for . If there exists such a subsequence of that is order-isomorphic to , then is said to contain as a consecutive permutation pattern. For each of these definitions, two natural questions arise: “Given a permutation , how many permutations of length avoid ?” and “When do two distinct permutations and yield the same avoidance generating function?” Patterns and with this property are said to be Wilf-equivalent.
In this paper we consider analogous questions of pattern avoidance for plane trees. All trees in the paper are rooted and ordered. We will focus on full binary trees, that is, trees in which each vertex has 0 or 2 (ordered) children. Two children with a common parent are sibling vertices. A vertex with no children is a leaf and a vertex with 2 children is an internal vertex. A binary tree with leaves has internal vertices, and the number of such trees is (OEIS A000108). For simplicity of computation, we adopt the convention that there are zero rooted binary trees with zero leaves. The first few binary trees are depicted in Figure 2.
Conceptually, a plane tree avoids a tree pattern if there is no instance of anywhere inside . Pattern avoidance in vertex-labeled trees has been studied in various contexts by Steyaert and Flajolet , Flajolet, Sipala, and Steyaert , Flajolet and Sedgewick , and Dotsenko . Recently, Disanto  studied pattern containment of caterpillar subgraphs in binary trees while Khoroshkin and Piontkovski  considered generating functions for general unlabeled tree patterns in a different context.
In 2010, Rowland  explored contiguous pattern avoidance in binary trees (that is, rooted ordered trees in which each vertex has 0 or 2 children). He chose to work with binary trees because there is natural bijection between -leaf binary trees and -vertex trees. His study had two main objectives. First, he developed an algorithm to find the generating function for the number of -leaf binary trees avoiding a given tree pattern; he adapted this to count the number of occurrences of the given pattern. Second, he determined equivalence classes for binary tree patterns, classifying two trees and as equivalent if the same number of -leaf binary trees avoid as avoid for . He completed the classification for all binary trees with at most eight leaves, using these classes to develop replacement bijections between equivalent binary trees.
In 2012, Gabriel, Peske, Tay, and the second author  considered Rowland’s definition of tree pattern in ternary, and more generally in -ary, trees. After generalizing Rowland’s algorithmic approach to compute functional equations for the avoidance generating functions of arbitrary ternary tree patterns, they explored bijections between equinumerous sets of pattern-avoiding trees. Along the way they found sets of pattern-avoiding trees whose enumeration yielded a number of well-known sequences.
In this paper, we extend Rowland’s work in a new direction. The work of  and  may be seen as parallel to the definition of consecutive permutation patterns given at the beginning of this section. In those papers, tree was said to contain tree as a (contiguous) pattern if was a contiguous, rooted, ordered, subtree of . In this paper, we modify the definition of tree pattern to mirror the idea of classical pattern avoidance in permutations. In the case of pattern-avoiding permutations, there are more Wilf-classes for consecutive patterns of a given length than for classical patterns (as a small example, there are 7 Wilf classes of consecutive permutation patterns of length 4, compared to 3 Wilf classes for classical permutation patterns of length 4). This parallel holds true in the case of trees. In fact, as we show in Section 4, there is precisely one Wilf class of -leaf patterns for any .
As with previous work, given any binary tree pattern we present a technique to compute the generating function that enumerates trees avoiding according to the number of leaves. This enumeration shows that there is exactly one Wilf class of -leaf patterns. We also consider trees avoiding multiple tree patterns and explore relationships between sets of pattern-avoiding trees and pattern-avoiding permutations.
2 Definitions and Notation
In this paper, a tree contains as a (non-contiguous) tree pattern if there is a tree , obtained from via a finite sequence of edge contractions, such that is a contiguous, rooted, and ordered subtree of . Conversely, avoids if there is no such that contains as a subtree. For example, consider the three trees shown in Figure 1. avoids as a contiguous pattern, but contains non-contiguously (contract all non-bolded edges). On the other hand, avoids both contiguously and non-contiguously since no vertex of has a left child and a right child, both of which are internal vertices.
We define to be the set of -leaf binary trees that avoid the pattern non-contiguously, and . We will also write for the set of -leaf binary trees that avoid contiguously, and . We will be particularly interested in determining the generating function
for various tree patterns .
Before we explore particular binary tree patterns, we list all of the 1, 2, 3, and 4-leaf binary trees. We label trees with a double subscript notation. The first subscript gives the number of leaves of the tree, and the second subscript distinguishes between distinct tree patterns with the same number of leaves. We also note that if is the left-right reflection of tree , then by symmetry, so we omit left-right reflections. We will use these labels throughout the remainder of the paper.
3 Avoiding Simple Tree Patterns
In this section, we find generating functions for the number of trees avoiding each of the tree patterns in Figure 2. For each tree, we discuss the structure of trees that avoid the given tree pattern and explain how to find a recurrence and generating function from this structure. We also compare these generating functions to previously known results for contiguous tree patterns.
3.1 Avoiding , , and
Clearly, a tree avoids if and only if it has no vertices. This is also true for contiguous avoidance, so we have
Next, a tree avoids if and only if it has no vertex with two children. In other words, only avoids . Again, we have
Finally, we observe that tree avoids if and only if has no vertex whose left child is not a leaf. For each there is precisely one such tree, so we have
From these few cases, it may seem that non-contiguous and contiguous avoidance is the same for many trees. The reader should suspect that this similarity does not hold for larger tree patterns based on the example of Figure 1. This suspicion is confirmed when we consider 4-leaf tree patterns.
Rowland showed that for
where is the th Motzkin number (OEIS A001006).
It turns out that non-contiguous avoidance is even more well-behaved for 4-leaf tree patterns. We will show that for
To find , we consider the structure of a general -leaf binary tree that avoids . We have two cases. Let be the root of . In the first case, ’s left child has no children, while ’s right child is the root of an -leaf subtree avoiding . In the second case, ’s left child has two children, but the leftmost of these children is a leaf. If the right child of ’s left child is the root of a subtree with leaves (), then ’s right child is the root of a subtree with leaves. Further, the -leaf subtree must avoid the tree pattern .
In the first case, we considered possible trees. In the second case, we considered trees. However, since we know that for , we have
Together with the base case , we see that
Next, we consider the structure of a general -leaf binary tree that avoids where is the root of . Again, we have two cases. As before, in the first case, ’s left child has no children, while ’s right child is the root of an -leaf subtree avoiding . In the second case, ’s left child has two children, but now the rightmost of these children is a leaf. The subtree whose root is the left child of ’s left child must avoid the tree pattern . After a nearly identical calculation to that of avoiding , we see that
Finally, we consider the structure of a general -leaf binary tree that avoids where is the root of . Again, we have two cases: either ’s left child has children or ’s right child has children, but not both. There are trees that fall into the first case, and trees that fall into the second case, which yields
4 Generating Functions for Pattern-Avoiding Trees
We have just seen that all 4-leaf binary tree patterns produce the same avoidance sequence when considered as non-contiguous patterns. This observation leads naturally to the main theorem of this paper, which provides a particularly clean answer to both questions stated in the introduction. Namely, “given a tree pattern , how many trees with leaves avoid ?” and “given two distinct tree patterns and , when do and produce the same avoidance sequence?” In fact, we see that not only is exact enumeration possible for any non-contiguous tree pattern, we see that all avoidance generating functions are rational and of a particularly attractive form.
Let and let be a binary tree pattern with leaves. Then
The reader can check that this indeed matches the generating functions computed in the previous section when . Further, notice that the generating function given in Theorem 1 depends only on the number of leaves of , and not on itself; that is, there is precisely one Wilf class of -leaf tree patterns for each . As we will see in Section 7 and the Appendix, the analogous statement for pairs of trees is not true. In this section we build the necessary framework to prove Theorem 1.
First, following [8, 13] we say that contains pattern at the root if contains a contiguous copy of where the root of coincides with the root of . Now, let be the generating function that enumerates binary trees avoiding tree pattern non-contiguously and containing the contiguous tree pattern at their root according to number of leaves. Because all binary trees have a root vertex, it follows that the generating function for all trees avoiding is given by . Also, let and denote the subtrees descending from the left and right children of the root of respectively.
Since we are working with full binary trees, the root has either zero or two children. When there are zero children, we have a 1-leaf tree. When there are two children, we have a tree with pattern . Thus, we haveat the root. Such trees are enumerated with the generating function
Next, we determine a recurrence for . Consider a tree that avoids and has the contiguous pattern at the root. Either the subtree extending from the left child of the root of avoids , the subtree extending from the right child of the root of avoids , or both. Inclusion-exclusion gives
Now, solve for to obtain
This computation yields one immediate result:
For any tree pattern , is a rational function of .
We have already seen that is rational for all tree patterns with leaves. Thus, by induction and Equation 3, the proposition follows. ∎
Equation 3 simplifies further for one particular family of binary tree patterns. Let the -leaf left comb be the unique -leaf binary tree where every right child is a leaf. Write for the -leaf left comb. Then , , , and .
Let in Equation 3. Then and . We have:
Since , this simplifies to
This nice relationship between and seems natural because of the large overlap between copies of and . Our next lemma shows that there is a simple relationship between generating functions for non-comb tree patterns as well.
Fix . Let and be two -leaf binary tree patterns. Then
Assume the lemma holds for tree patterns with leaves where . Since we suppose that all -leaf trees have the same avoidance generating function, we have that any -leaf tree has avoidance generating function . Suppose that tree has leaves to the left of its root and leaves to the right of its root. Similarly, suppose that tree has leaves to the left of its root and leaves to the right of its root. We will show that .
From Equation 3, we have
Since this holds for , by transitivity, all -leaf tree patterns have the same non-contiguous avoidance generating function. ∎
Finally, to prove the main theorem, it suffices to show that
It is a straightforward induction proof, using the fact that to show that this formula holds in general.
We have now explicitly enumerated trees avoiding any single binary tree pattern non-contiguously and determined all equivalences for when two trees exhibit the same avoidance sequence. We display the explicit generating function, first 8 sequence terms, and appropriate OEIS entry for tree patterns with leaves where in Table 1.
|Sequence||Growth rate||OEIS number|
Because all generating functions are rational it follows that for any -leaf binary tree the sequence satisfies a linear recurrence with constant coefficients. In fact, when tree pattern has leaves, grows exponentially. Because there are Catalan-many binary trees with leaves, the growth of these sequences is bounded above by . In Table 1 we display also the asymptotic growth rate for .
5 Recurrences for Binary Trees
While we gave explicit combinatorial explanations for the recurrences satisfied by when has or leaves in Section 3, the rest of our work has been algebraic. It is possible, however, to derive the recurrences satisfied by tree enumeration sequences through other techniques. In this section, we give a combinatorial explanation for these recurrences by considering the structure of trees that avoid the -leaf left comb.
First, we define a many-to-one correspondence between the set of -leaf binary trees and the set of -leaf binary trees. Given a tree with leaves, let be rightmost leaf whose sibling vertex is also a leaf. Then, let the parent tree of be the -leaf tree obtained by deleting and its sibling. If is the parent tree of , then we say that is a descendent tree of . While the parent tree of a tree is unique, a given tree may have multiple descendent trees. For example, , , and are descendent trees of . Similarly, and are descendent trees of .
Now, for any -leaf tree , we can use this parent/descendent relationship to generate a list where , has leaves for and is a descendant tree of . We refer to such a sequence of trees as the ancestry of . Because parent trees are unique, the ancestry of tree must be unique. For example, the tree has ancestry
Given an -leaf tree , we may also generate all descendent trees of in a systematic way. Call an internal vertex of closed if ’s right child is not a leaf. The number of descendent trees of is equal to the number of leaves of that appear to the right of all closed vertices. In fact, to produce a descendent tree of , we need only attach a pair of children to any one such leaf. The descendent tree relationship is further articulated in the following proposition.
Suppose that tree has descendant trees. Then those descendant trees will have descendant trees respectively. Further, if has descendant trees then the leftmost vertex to which one may add children is the leftmost vertex in a copy of an -leaf left comb.
Both claims in the proposition are consequences of the fact that the number of descendent trees of is equal to the number of leaves of appearing to the right of all closed vertices.
Let be the th leaf from the right of these leaves. When we append two children to , ’s parent is closed, and this new tree has only descendents.
Further, if has leaves to the right of all closed vertices, by definition of closed, none of these leaves’ parents have right children that are internal vertices. The only way to arrange a collection of vertices so that no internal vertices are right children is in the shape of an -leaf left comb. ∎
Now, consider the descendant relation restricted to trees that avoid the -leaf left comb. Any tree with descendant trees contains an -leaf comb, so we are only concerned with trees that have at most descendent trees. As in Proposition 2, a tree with descendant trees will have descendant trees with descendants respectively.
Notice further that for a tree which would have descendent trees, one of these descendent trees contains the -leaf left comb, so such a tree only has descendent trees that avoid the -leaf comb. Consequently, for a tree with descendent trees, those descendents will have descendent trees respectively that avoid the -leaf left comb.
Let be the number of -leaf trees that avoid the -leaf left comb and have exactly -leaf descendants, and let .
We have the base cases and .
More generally, for
Ultimately, we seek a recurrence for . Adding the above equations and algebraic manipulation produces the result
From the discussion above, trees avoiding an -leaf comb can be constructed with a finitely labeled generating tree (in particular with a generating tree using precisely labels), and one may use the transfer matrix method to obtain an alternate proof of Theorem 1 for the case of avoiding the -leaf left comb.
6 Connections to pattern-avoiding permutations
As evidenced in the Appendix, many sequences obtained by counting trees that avoid non-contiguous binary tree patterns are already known in the literature for other reasons. In this section we present a theorem that fully explains this connection for the case of avoiding a single binary tree pattern.
To this end, let denote the set of permutations of length . As in the introduction, given and we say that contains as a pattern if there exist indices such that if and only if . Let , and . For example, for since the only way to avoid the pattern 12 is to be the decreasing permutation of length . It is also well-known that if , then where is the th Catalan number.
Let be any binary tree pattern with leaves. Then
It is well known that the set of binary trees with leaves is in bijection with the set of permutations of length which avoid the pattern 231.
To see this, label the root of tree with the label . Now, suppose there are internal vertices to the left of the root and internal vertices to the right of the root. The vertices on the left will receive labels from the set and the vertices on the right will receive labels from the set . For each subtree, give the root the largest available label and continue recursively until each internal vertex has been labeled.
Now, there is a natural left-to-right ordering of the vertices of . Read the labels of the vertices from left to right to obtain a permutation . Necessarily, avoids because all labels to the left of a given vertex have smaller labels than all labels to the right.
Further, we can see that the -leaf right comb encodes the unique decreasing permutation of length . It is not hard to see that if a tree avoids the -leaf right comb, then the corresponding permutation avoids the decreasing permutation of length and vice versa. ∎
We note that this correspondence between -avoiding permutations and binary trees is not new. If one ignores the leaves in our trees, the bijection given in the proof of Theorem 2 is identical to the correspondence between postorder-labeled trees with inorder-read permutations found in . Further work connecting permutations to binary trees in the context of sorting can be found in , , , , , and .
It is worth considering when Theorem 2 generalizes and how. The correspondence given in the proof of Theorem 2 does not necessarily work with trees other than the right comb. For example, while the permutation corresponding to is 123. However, () while (). Thus even if the sequence obtained from avoiding a set of tree patterns is also the avoidance sequence for some set of pattern-avoiding permutations, there may be other bijections required to demonstrate the equivalence. It remains open to give a natural interpretation of Theorem 2 for trees other than the right comb.
7 Avoiding two or more binary trees simultaneously
Thus far, we have analyzed generating functions and enumeration sequences for trees avoiding a single non-contiguous binary tree pattern. We will now consider trees that avoid two tree patterns simultaneously. To this end, let be the generating function that enumerates trees avoiding and with pattern at the root according to number of leaves. For brevity, we may abbreviate .
This latter expression follows again from inclusion-exclusion. One can solve this pair of equations for to obtain a rational combination of rational generating functions and see that for any pair of binary trees, is indeed rational.
To compactly state the equivalent expression to Equation 7 for trees avoiding binary tree patterns (), we need to introduce more efficient notation. Notice that all terms on the right hand sides of Equations 2 and 7 are of the form where for each tree , there are 3 possibilities: (i) and , (ii) and , or (iii) and . Let and let where (i) if then and , (ii) if then and , and (iii) if then and . Further, for , define .
By inclusion-exclusion we have:
Notice that this expression is linear in . In fact, only appears in two terms: the ones corresponding to and , so we see that is rational for any finite set of tree patterns.
In light of Theorem 2 one might wonder if for every set of binary tree patterns , is identical to the avoidance sequence for some set of permutation patterns. This, however turns out not to be the case. For example consider the sequence
If this were the avoidance sequence for some set of permutation patterns , we see from the fact that that contains a pattern of length 3. Further, since it is known that for any pattern , must contain precisely two patterns of length 4. However, exhaustive checking of for sets of patterns consisting of one pattern of length 3 and two patterns of length 4 yields no match for this sequence.
Throughout this paper, we have investigated non-contiguous pattern avoidance in binary trees. Unlike Rowland’s work for contiguous patterns, our avoidance generating functions are always rational, and the Wilf classes obtained for avoidance of single patterns are easy to describe: two tree patterns are Wilf-equivalent if and only if they have the same number of leaves. The results in this paper not only give a complete characterization of trees avoiding a single pattern, but we also present a computational technique to quickly determine the number of trees avoiding any finite set of non-contiguous tree patterns. Finally we explore combinatorial proofs of our results and give an explicit bijection between certain pattern-avoiding trees and pattern-avoiding permutations.
The authors thank Eric Rowland for assistance with generating the tree graphics for this paper and for several helpful presentation suggestions.
This appendix lists pairs of binary tree patterns each of which have at most 5 leaves, classifying them by their avoidance generating function and sequence. For each class, we give the generating function , and we list the first 15 terms of the corresponding avoidance sequence. If the avoidance sequence for a class is listed in the Online Encyclopedia of Integer Sequences , we include the appropriate reference. For brevity, left–right reflections are omitted.
Avoiding a 3-Leaf & a 4-Leaf Tree
Avoiding a 3-Leaf & a 5-Leaf Tree
Avoiding a 4-Leaf & a 5-Leaf Tree
OEIS A016777: for