Enumerations of Ordered Trees : of Illinois at Urbana-Champrzign, Urbana, IL 61801, USA
Enumerations of Ordered Trees : of Illinois at Urbana-Champrzign, Urbana, IL 61801, USA
We deal with the class T,, of ordered trees with n edges. Several enumeration problems
concerning Tn and some of its combinatorial properties are studied.
Closed-form expressions for the following enumerations are given: (1) the number of trees in
T, with k leaves, (2) the number of nodes in 7; with d children, (3) the number of trees in T,,
with root degree r, and (4) the number of nodes in T, on level I with d children.
1. Introduction
The next section presents the basic definitions and several one-to-one corres-
pondences among ordered trees and other combinatorial objects that are used in
subsequent sections. In Section 3 we investigate the functions 2, 9 and 9; WC
9
IO N. Dcrsltowilr, S. Zaks
show that
(3) 92,,(r) = L:
I1 (
2t;-_1-
)
We also derive other combinatorial and statistical properties; for example: for
each tree in this class with k leaves there is a corresponding tree with n + 1 - k
leaves. the expected number of leaves is $I + 1). and the expected root degree is
3rr/5~1+2). Sections 2 and 3 include two reflection lemmata that illuminate
relationsh$s between ordered trees and their corresponding binary-tree represen-
tations. For completeness we have included a discussion of the functions 9? and 9;
although the results are not new, our proofs may be of interest.
Section 4 is devoted to the function A*; our main result is the closed-form
expression
(41 .v;,(f. d, = -
From it. the expressions for the functions 9 and B-independently proved in
Section 3-may be derived.
2. Fdiminaries
2.1. Defmitions
The combinatorial structures that WC shall be dealing with are (unlabelled)
ordered trees. Our terminology is, in the main, borrowed from Knuth [3]. Ordered
drees may be defined recursively as follows: if t,, t2, . . . , t,,, are ordered trees,
.
.
111MO, then
node-called
t =
( 11 $x2 +m
is also an ordered tree. The trees t,., tv, . . . , t,,, are subtrees of the distinguished
them. The roots of the subtrees are
clzifdren of the root of the tree. The trc;tes are ordered in the sense that the order
amor\ subtrees (or children) is significant.
Wi $1each node x in a tree t we associate two values: its degree and level. The
plm u= +I o=:z JEq)
qms ;!(!v)
s.~G;r~u!
any%uuou 1 + u JO samanbas 8uyvyurop 30 13s aq,~ :I
ysaq,u;aJed
U! aq$ uaaMlaq sampuodsaxoa
dlaq IE!M s)as thl!MO~l[OJ aq, waq~ %UOUI~ '([v-j
gsu!qm)I ald~exaJojaas)sl~afqo ~ynolmuquroa ray10 pue saa.11
pa~ap.xo~o spas
asay 30 s~ua~ua~a uaalu\\laq sauapuodswoa auo-o)-au0 snomunu a.x~ aray&
*([z-JXNI~JIZ~ aIdu.mxa .IOJ aas)
:"2 SK3qLUnU Ut?lL'Jk?~
UMOUy-[IaM
aq1 ale asaq~ -* zpjq s 2 1 I sayas e UUOJ s1as asay JO S3Z!S aq1 Z '%LJ
U! U33S 3q UB3 SV Sa%p3
U ql!M SX3Jl pX3pJO JO 13s 3ql 3JOU3,p (0e.u) "J,137
2 la/ml - - - - - -04
1 (anal _ _ _ ___
0 lanai _--___
0 pq uo apou Quo aJ# S! 100.xaq,L 3pou ~v,unalaA~
LIEpaIIE3 si ,T asy.Iaq~o yva/
B paum~ so (1aa.&p JO apou v 3 30 100.1aql u10.13(~~2iuytmdas @pa 30 mqwnu
aql) am~~s!p sl! s! x $ b pnq aqi put3 wq i!uaqy!qD 30 mqwnu aqi s! x 30 aa8ap
II
Iv. Derskowifz, S. Zaks
! A
I A
I 1,
ft? Y
All steps in aIlattice path are either up or to the right; a path is admissible if it
does not pass below the diagonal y = x.
S,: the set of fulr binary trees with n internal nodes. An ordered tree is full
binary if all nodes are either of degree 0 (leaves) or 2 (have a left child and a
righr child).
The correspondences between these five sets are described below and illus-
trated in Fig. 3 using a tree t E Ts (Fig. 3.1) and its reflection t* E jTs (Fig. 3.2). We
shall alternate between the two trees t and t* for a reason that will become
apparent later.
T, @Pp,, In: Given a tree t E T,, traveme it in preorder (visit the root, then
traverse its subtrees from left to right), writing an open parenthesis for each edge
passed on the way down and a close parenthesis for each edge passed on the way
up. For the tree t in Fig. 3.1, .this yields the legal parenthetic expression p(t) E P8
shown in Fig. 3.3. Similarly, if the degree of each node in t* is recorded on the
. then the dominating sequence i( t*) E Is shown in Fig. 3.4 is obtained.
In gem- il, if cl, t2, . . . , fm are the subtrees of a tree t, then p(t)=
~~~~,~~~94$7,) (pft,,,)) and i(t) = m i(tl) i(@
i(c)*
tI ri)
Enumerarions of o:dered trees 13
$1 0
1
:8,8)
w
l-n-II
(
(
P/t/= (
0
3.5. An admissible lattice path corresponding to 3.6. An admissible lattice path corresponding to
P(f). i(P).
3.7. A full binary tree corresponding to Z(t). 3.8. A full binary tree corresponding to ((I).
Fig. 3. Correspondences between I,,, P,, I,,, L,, and I?,,.
14 N. L?ershowit:itz,
S. Zaks
er*6, a+L, : Lattice paths may be easily obtained from sequences of parenthe-
scs or integers. Given a legal parenthetic expression p E P,, we start at (0,O) and
up one coordinate for each open parenthesis and go right one coordinate for
each close parenthesis. This yields a path r(t) that remains in the upper left half of
the lattice and ends at (n, n). Alternatively, given a dominating sequence i E I,,
the function I defines an admissible lattice path by letting each integer in i
determine the number of coordinates to move up before moving rig!lrt one
coordkte. The paths f(t) in Fig. 3.5 and r(t*) in Fig. 3.6 may be obtained in such
a manner from Figs. 3.3 and 3.4, respectively.
L,, c-, B,, : Given an admissible lattice path 1E L, corresponding to an ordered
T,,, a unique full binary tree b( tj E B,, is constructed in the folhowing
manner: Build the binary tree in precjrder, each step up on the path correspond-
ing to an internai node and each step to the righi. corresponding to a leaf. A final
leaf must be added. In the same manner, the function 6 defines a full binary tree
based on the path I. See Figs. 3.7 and 3.8.
By this construction, if
then h(t) is as shown in Diagram 1 (note that this is the same as the full binary
tree obtainable from the binary tree representation given ;n Knuth [3, Section
2.X2]). while b(t) is as shown in Diagram 2.
b /t/ =
Diagram 1
Enumerations of ordered trees 15
Diagram 2
First Reflection Lemma. The ordered trees t, t* E T, are reflectionsof each other, if
and only if the full binmy trees b(t), b( t*) E B,, are reflections of each other.
For example, the ordered tree t* in Fig. 3.2 is the reflection of the tree t in Fig.
3.1, and the full binary tree b(t*) in Fig. 3.8 is the reflection of the full binary tree
b(t) in Fig. 3.7.
Each of the above correspondences is one-to-one and can be used in either
direction. Four properties follow directly from these correspondences, and are
summarized in the following
Characterization Lemma
(1) The number of leaves in a (not edgeless) tree t
= the number of () patterns in p(t)
= the number of corners (i.e. path segments of the form r
) in r(t)
= the number of left leaves in b(t)
= the number of right leaves in b(t).
(2) The number of internal nodes in a (not edgeless) tree t
= one more than the number of 6,((or equivalently)) ) patterns in p(t)
cycle lLemma (Qvoretzky and lUotzkin [I]). For any sequence p1 p2 pm+,, of
m open parentheses and n close parentheses, where m > n, there exist exactly nz - n
cyclic permutations
For example, of the sip cyclic permutations of the sequence )((()(, only two are
legal prefixes: ((()() and (()()(.
We can use this lemma to determine the number f(i, j) of admissible lattice
paths from (0.0) to Ci,j). The obvious recurrence for the function f is:
f&i,=
c 1
0
Ci,j- l)+f(i- l,,i)
if i = 0,
if j<i,
crtherwise.
(40)
3.1. Introduction
In this section we study the number Z,,(k) of trees with n edges and exactly k
leaves, the number 9,(d) of nodes of degree d among the trees with n eciges, and
the number B,(r) of trees with n edges and root-degree r. Our main results are
closed-form expressions for these numbers. Some consequences and additional
statistical properties of T, are discussed.
3.2. Z&(k)
The following closed-form expression for Z$(k) was given by NarayanG [S] in
connection with partial orders on partitions:
Theorem 1. The number Z,,(k) of ordered trees with n edges and k leaves is
Examples. Of the 14 ordered trees with 4 edges, J&(2) = 6 have 2 ieavcs and
.Za13) = 6 have 3 leaves. (See Fig. 2.)
Proof. The number of trees in 7;, with Ik leaves is equal to the n:lmber of legal
parenthetic expressions in P, with k occurrences of () (Characterization Lemma),
It? N. Dershowitr, S. Zaks
which in turn equals the number of legal prefixes with n + 1 (s, n )s, and k 0s.
By the Cycle Lemma, of the k possible cyclic arrangements of a parenthetic
expression beginning with (, ending with ), and having n + 1 (s, n )s, and k Os,
exactly one is a legal prefix. The total number of such expressions (legal or not) is
fk?, I(2 i) (the number of ways to partition both the (s and the )s into k
nonempty runs); thus
which proves
-em 2. The number of trees in T, with k Zeaues is equal to the number cf trees
in 7; with n + I - k leaves.
&rolJary 2.1. The expected number of leaves in a tree in T,, is $(n + 1); the
expected number of internal nodes is also $(n -I-1).
Since the sum of the degrees of the internal nodes in T, is equal to the number
of edges (n/in + I))(?) and the number of internal nodes is i(y), we have
At~Uary 2.2. The expected degree of an internal node in a tree in T, is 2nl(n + 1).
We present two additional proofs of Theorem 2; they are both direct and
constructive. The two proofs are then related to each other by another reflection
kmma.
b
.
Diagram 3
reflection
Diagram 4
20 N. Dershowiir, S. Zaks
Note that the second proof uses the three correspondences t *b(t) 41(t) * t,
whereas the third proof uses the short-cut TV t.
Combining the two reflection lemmata, we have the
Corollary. Let t* be the reflection of t and b*(t) be the reflection of b(t). Then
)= b(f) = b(t)*
2n- l-d
S,,(d)= n 1 .
( - 1
Example. There are S.J,J1) = 20 unary nodes in the trees 01 T4. (See Fig. 2.)
Cordky 3.1. The expected number of nodes ofdegree d in a tree in T, is less than
(I?-g 1 j/2.
2n-1-d
(
2,,(d)
--= n- 1 n(n-l)*.=(n-d+l) .-
cn -- 1 2n = 2c(2n - 1) (2n--d+l)(2nnd)
12+1 ( n 1
It follows that the expected number of nodes of degree greater than d is also less
than (cz-+ 1 ,/2d. This demonstrates the extrelne paucity of nodes of high degree.
Enumerations of ordered trees 21
Theorem 4. The number 92,,(r) of trees with n edges and root of degree r is
Example. B4(2) = 5 of the 14 trees in T4 have a root of dlegree 2. (See Fig. 2.)
Proof. Suppose a tree t E T, has a root of degree r. Then the lattice path Z(t)
begins with (0 ,O)-+(O ,r)-+( 1, r) (see Section 2.2). The number of admissible
paths (1, r)--, (n, n) is by symmetry equal to the numbdr of iidmissible paths
(0 ,O)-+(n - r, n - 1): (r/n)(*t?;) (see Section 2.3). cl
Note that B,,(r) = (r/n)%&(r), i.e. exactly r/n of the (2t?;r) nodes of degree r in
T, are roots.
But
f r2(2~-_1[r)=+J-2(n2_nl)
r=O
(this may be derived from the formula xkco (s,)(Lk) = (~~~~,), where m 2 t a0
and I, s 30; see Knuth [3, Section 1.2.6, Eq. (25)]) and the result follows. 0
A recurrence relation and the closed-form expression for S,,(r) are given in
Ruskey and Hu [6], where @I,(r) counts the number of trees in B, whose
rightmost leaf is on level r. The correspondence between these two interpretations
of 9&(r) follows immediately from the alternative interpretations of the lattice
path I; see Figs. 3.6, 3.2, and 3.7.
Note that
(a) the expected degree of a node ii1 any tree in T, is (obviously) almost 1
(n/(n + I));
(b) the expected degree of an internal node in T, is (Corollary 2.2) almost 2
(2n/(n + 1));
(c) the expected degree of a root in T,l is (Corollary 4.1) almost 3 (3n/(n + 2)).
EqIes. There are &(2. 1) = 5 unary nodes on level 2 of the 14 trees in 7a (set
Fig. 2); there are A&,1(4,2) = 1700 binary nodes on level 4 of the 16,796 trees in
To=
A b
Xi
a
e
level#+I
R
Be- e-m
B
4 u
-- -----em.-__
- - level&
e
x --- --_- .---__--- DIII
--- level I + I
8d -
e
Enumerations of ordered trees 23
Since x either (a) has an older sibling or (b) does not, and the above correspon-
dences are one-to-one, it follows that
---- levelll
Since xs eldest child either (a) has children or (b) does not, and the above
correspondences are nrle-to-one, it follows that
X
:
.
...
1 1 edges
------- level 1
LJ
n-8 leaves
(4) Obviously the root must be of degree greater than zero if there is at least
one edge in the tree.
24 N. Dershowitr. S. Zaks
Since the function (*,;:)(21+ d)/(2n - d) also satisFes the recurrence relation
( 1) together with the boundary conditions (2, 3, 4), it follows that
21+d 2n-d
&,(I, d) = -
2n-d ( n+z )
Recall that the function f( i, j) = <i+)(j - i + l)/(j + i + l), used at the end of
Section 2 to yield the number of admissible lattice paths from (0,O) to (i, j), was
ha.sed on the recurrence f<i, j) = f(i, j - 1) + f( i - 1, j) with the boundary conditions
f(f), j) - 1 and f(i, j) = 0 if j < i. It turns out that
degree d
Each entry in the triangle is the sum of the two entries above it. Also,
Together. these two facts yield various relations among the values of N, e.g.
PrOOf.XJi, 0) = (I/&??,). Cl
Nm(l,d+l)=N,,(l+l,d)-N,,(l+l,d+l).
Hence,
cl
Proof.
d&K,(L
d)=Kdl, O)+ f &,(l, d)
d=l
Alternative lattice-path arguments for these results are given in the Appendix.
Proof.
Corollary 5.5. The number %Jr) of trees in T, with root of degree r is (r/ir)(2&.J.
Proof. Since the number of such trees equals the number of roots of degree r, we
have
The last two results have already been independently and directly proved in the
previous sectio I (Theorems 3 and 4).
26 N. Dershowitz, S. Zaks
5. Disnssion
We have investigated various enumerations of the class T,, of ordered trees with
n edges. Our main result was the closed-form expression (2,;$(2L+d)/(2n 4)
for the number &(I, d) of nodes of degree d on level 1 in trees in T,,. Other
results were derived from this formula, for some of which we also gave indepen-
dent direct proofs.
In searching fc r closed-form ex:pressions, we made considerable use of
computer-generated data and of the handbook of sequences by Sloane [7].
Appendix
In this appendix we give alternative lattice-path proofs (A) for the number
2!TH(k)of trees in T, with k leaves (Theorem 1) and (B) for the total number of
Ctzaves and of internal nodes on level 1 in T, (Corollaries 5.1 and 5.2).
(A) We first prove that the number of inadmissible paths from (i, j) to (n, n),
with k corners, where 0 G i G j sn, is equal to the number of paths from
iJ=+ 1, i-- 1) to (n, n) with k corners. Note that the point (j + 1, i - 1) is symmetric
to the point (i, j) with respect to the line y = x - 1. The proof is by induction on
the number of corners, k.
Consider the inadmissible paths from (i, j) to (n, n) with k 20 corners that
begin with (i, j)-, (i, j)+( i, j + 1), where i > j. They are in one-to-one corres-
pondence with the paths beginning with (j + 1, i - l)-,(i, i - l)+i, j + 1) and
continuing with the same k corners to (pt, n) (see Diagram 5). (This serves as
?hc base case of the induction.)
Diagram: 5
ow consider those inadmissible paths from (i, j) to (n, n) with k > 0 corners
that begin with (i, j)+(i- 1, j)-+(i- 1, j+(i, j), where i- 1 <j< j. They are
in one-to-one correspondence with the pijths from (j + 1, i - 1) to (n, n) with k
corners b :ginning with (j + 1, i - 1) + (j. i - 1) -+ (j, i- 1) + (j+ 8, i- l), since,
Enumerations of ordered trees 27
by the inductive hypothesis, the number of inadmissible paths from (i, j) to (n, n)
with the remaining k - 1 corners is equal to the number of paths from (j+ 1, i-
1) to (n, n) with k - 1 corners (see Diagram 6).
I)
Diagram 6
The number of lattice paths (admissible or not) from1 (0,O) to (IS, n) with
exactly k corners is (r)(z). To see this, choose k Xis such that O<xl c x2< C
~k<m and k YiS such that 0<y1<y2< *Cyk s n; the k corners are at the
points (Xi, yi). Thus, the total number of paths from (0,O) to (n, n) with k corners
is (t)(i), while the total number of paths from (1, - 1) to (n, n) with k corners is
(L)(~:). It follows that
(B) Recall that a leaf is characterized by a corner in the lattice path (Charac-
terization Lemma). Accordingly, a leaf on level I corresponds to a corner on
the Ith diagonal above y = X, i.e. a path segment (i, i + 1- 1) + (i, i + I)-,
(i + 1, i + 1) (see diagram 7).
/n,n)
Diagram 7
N. Dershowitz, S. Zaks
Recall further (Section 2.3) that the number of admissible paths from (0,O) to
(i, +--denote that by [CO,0) -3 (i, j)]-is (j:)(j - i + l)/( j + i + 1). Thus,
number of leaves on level f
P-l
= 1 [(O, O)+(i, i + I- l)][(i + 1, i + I)+(n, n)]
i=O
=C[(O,O)+(i,i+Z-l)][(O,O)+(n-i-I,n-i-l)] (by symmetry)
n4 I
= c
f+2i
i=#,I+2i ( i ) 2n-ii-[(,f-)
-
._! 2n
n ( n-l )
using the identity (see, e.g. Knuth [3, Section 1.2.6, Eq. (31)])
t-8
r s
c
&=,,Pkt ( )
v-kt
k s - mt + kc
s-mt+kt
( m-k ) =r+s-mt
r+s (*+:?q
=gy,,.
References
A Dboretzky and Th. Motzkin. A problem of arr:ingements, Duke Math. J. 14 (1947) 3~5-313.
%I. Gardrtcr. Mathematical games: Catalan ilumbers, Scientific American 234 (6) (1976)
l2o-l25.
D.E. Knuth. The Art of Computer Programmi;kg, Vol. 1: Fundamental Algorithms (Addison-
Wesley. Reading. MA. 1968).
X1-J. Kuchinski. Catalan structures and correspondences, M.S. Thesis, Dept. of Mathematics,
West Virginia Univ.. Morgantown, WV (May 1977).
T.V. Nantyana, A partial order and its application to probability, Sankhya 21 (1959) 91-98.
F. Ruske), and T.C. Hu, Generating binary trees lexicographically, SIAM J. Comput. 6 (4) (1977)
74%75 ;c.
p9.J.A %~ane. A Handbook of Integer Sequences (Academic Pre;s, New York, 1973).