Content uploaded by Raphael Finkel
Author content
All content in this area was uploaded by Raphael Finkel on Feb 29, 2016
Content may be subject to copyright.
A Note on Enumerating Binary Trees
MARVIN SOLOMON AND RAPHAEL A. FINKEL
Umverstty of Wtsconsm, Madtson, Wtsconsm
ABSTRACT Gary Knott has presented algorithms for computing a bljectton between the set of binary trees on n
nodes and an mmal segment of the posmve integers Rotem and Varol presented a more complicated algorithm
that computes a different bljectmn, clmmmg that thetr algorithm is more efficient and has advantages ff a
sequence of several consecutive trees is reqmred A modlficatmn of Knott's algorithm that ts simpler than Knott's
and as effioent as Rotem and Varol's is presented Also given is a new hnear-time algorithm for transforming a
tree mto ~ts successor m the natural ordering of binary trees
KEY WORDS AND PHRASES binary trees, permutations, generators, enumeratmn, combmatortcs, stack-sortable
permutations
CR CATEGORIES 3 79, 5.39
1. Introduction
Gary Knott [1] has pubhshed algorithms for computing a bljection
Rank
from the set of
binary trees with n nodes to an initial segment of the integers and for computing
Rank -1.
His method for computing
Rank -~
revolves generating certain permutations of {1, 2 .....
n} called
tree permutattons,
which are m one-to-one correspondence with the binary trees
and from which the binary trees may be easily constructed. Rotem and Varol [2] propose
an alternative technique for generating the trees. Instead of constructing the tree permu-
tations (which they call
stack-sortable permutattons)
directly, they construct the
inverston
tables
of these permutations, which are sequences of nonnegative integers called
ballot
sequences.
They show that the ballot sequences may be generated in lexicographlc order
and present a clever and efficient algorithm for converting the ballot sequences into trees.
In this note, we correct some misconceptions put forth by Rotem and Varol and describe
modifications of Knott's algorithms that map
dtrectly
between trees and Integers, as well
as a new algorithm that transforms a given tree to the next one in sequence.
In comparing their technique to Knott's, Rotem and Varol concede that their method
does not generate the trees in the "natural" order, but claim two advantages: (1) They say
their technique is more efficient, since it is "well known" that the mapping of permutations
to trees requires
O(n 2)
operations In the worst case for trees with n nodes In contrast, their
method requires only
O(n
log n) operations to create an n-node tree, provided a table ofn 2
values has been precomputed. They also imply a similar advantage for their calculaUon of
Rank.
(2) They point out that a ballot sequence may be efficiently generated from the
previous one and converted to a tree, yielding a technique for generating a sequence of
trees. They claim that using Knott's method "to generate k trees corresponding to
consecutive permutations would require the transformation of k ind~ces, since there is no
Permission to copy without fee all or part of this materml ~s granted prowded that the copies are not made or
dtstnbuted for direct commercial advantage, the ACM copyright notice and the utle of the pgbhcauon and its
date appear, and nottce is given that copying is by permission of the Assooatlon for Computing Machinery To
copy otherwtse, or to repubhsh, reqmres a fee and/or specific permission
This research was supported m part by the Umted States Army under Contract No DAAG29-75-C-0024
Authors' address Computer Soences Department, Umverslty of W~sconsm, 1210 West Dayton Street, Madison,
Wl 53706
© 1980 ACM 0004-5411/80/0100-0003 $00 75
Journal of the Assooatlon for Computing Maclunery, Vol 27, No I, January 1980, pp 3-5
M. SOLOMON AND R. A. FINKEL
simple way of deriving stack-sortable permutations in their order corresponding to the
natural order of trees" [ 1, p. 404].
With regard to the first claim, we point out that whereas the mapping of an
arbitrary
permutation to the corresponding tree may take O(n 2) operations, a
tree
permutation may
be converted to a tree in
O(n)
operations, due to its special properties. Rather than prove
this result here, however, we present a modification of Knott's algorithm that translates
directly from indices to trees.
With regard to the generation of sequences of trees, we show how to transform a tree to
the next one in the natural order by an algonthm that works directly on the tree and
requires
O(n)
operations.
2. Definitions
A binary tree
Tis either a
null tree
or consists of a node called the
root
and two binary trees
denoted
Left(T)
and
Right(T).
In the former case, the
size
of T is zero; m the latter case,
Size(T) ffi 1 + Size(Left(T)) + Size(Right(T)).
We will often identify a tree with its root.
Define a relation on trees by T~ < T2 if and only if one of the foUowmg condmons hold:
(i)
Size(T1) < Size(T2)
or
(ii)
Size(T 0 ffi Size(T2)
and
Left(T 0 < Left(T2)
or
(iii)
Size(T1) = Size(T2)
and
Left(T~) ffi Left(T2)
and
Right(T1) < Right(T2).
This ordering is called the
natural
ordering of trees. Let { T~, T2 ..... Tnn } be the sequence
of all trees of size n, ordered by the natural ordering. It is well known that B~ is the
nth
Catalan number
[3]:
n+l
Define
Rank(TO = i, Rank-nO, n) = T,,
and
Next(TJ = T,÷i,
and let
First(n)
denote the
tree
T1,
depicted in Figure l(a). The
last
tree of size n Is
TB~,
depicted in Figure l(b).
3. Algorithms
This section contains algorithms for computing
Next, Rank,
and
Rank -1.
We assume that
T is represented by a pointer to a structure containing the two trees
Left(T)
and
Right(T)
as well as
Size(T).
The size is provided for efficient Implementation of
Rank
and
Next.
First a table of Catalan numbers should be prepared according to the recurrence cited
by
Knott:
6B.-1
Bn = 4Bn-1
-
n+l"
Next(T)
attempts to transform T to its successor in the natural ordenng. It falls tf T is
(a) (b)
FIG 1 The first and last trees with n nodes
A Note on Enumerating Bmary Trees 5
the last tree of size n. Otherwise, an algorithm follows directly from the definition of the
natural order: The successor of T may be formed by attempting first to transform T's right
subtree to its successor. (This subproblem is solved recursively ) If T's right subtree has no
successor, then that tree is reset to the first tree of its size, and the left subtree is transformed
to its successor (This subproblem is also solved recursively.) If both the subtrees are the
last trees of their sizes, then one node is transferred from the right to the left and both
subtrees are remitialized.
The computation time for a call to
Next,
exclusive of calls to
First
and recurslve calls to
Next,
is bounded by a constant.
First(n)
may be calculated m time proportional to n.
Finally, a call of
Next(T)
gives rise to at most one recursive call for each node in T.
Therefore,
Next(T)
is calculated in time
O(n),
where n =
Size(T).
Rank-l(i, n)
may be calculated by essentially the same counting argument as the one
used by Knott. Let Gkn denote the number of trees w~th n nodes whose left subtree has k
nodes. As Knott points out, Gkn =
Bk-lBn-k.
Then the size of the left subtree of T, is the
largest integer I such that Sin = ~k_~t Gkn < i. As Rotem and Varol point out, this value may
be calculated quickly by precomputmg some values of Sin and using binary search to find
the largest
Sin
less than i. The complexity of finding the size of both subtrees is then
O(log l) _< O(log n). The left and right subtrees of T, may be calculated recursively as
Rank-I([i/BrJ, l)
and
Rank-l(t
mod
Br,
r),
respectwely, where r = n - l - 1 is the size of
the right subtree.
Exclusive of recurswe invocations,
Rank-l(i, n)
is calculated in time O(log n). Since each
recursive call of
Rank-1
generates one node of the resulting tree, the total time is bounded
by
O(n
log n).
Rank(T)
may be computed by counting the number of trees preceding T in the natural
ordering. They may be divided into three classes: those T' for which
Size(Left(T')) <
Size(Left(T)),
those for which
Stze( Left( T') ) = Size(Right(T))
but
Left( T') < Left(T),
and
those for which
Left(T') = Left(T)
but
Right(T') < Right(T).
Let n =
Size(T),
and let l
and r be the sizes and j and k the ranks of the left and right subtrees of T. Then the first
class contains St~ trees, the second class contains (j - l)Br trees, and the third class
contains k - 1 trees. The rank of the entire tree is one greater than the sum of these three
numbers.
The calculation of
Rank(T)
requires constant time to find the sizes of its subtrees (since
we are assuming the informaton is stored in their roots) m addmon to one recursive call
for each node of T. Thus the running Ume of
Rank Is O(n).
4. Conclusions
We have presented straightforward and efficient algorithms for computing the rank of a
tree m the natural ordering of binary trees of a given size and for constructing the tree with
a given rank. We have also presented a new linear algorithm that transforms a tree to its
successor in the natural ordenng.
REFERENCES
1 KNOTT, G D A numbering system for binary trees
Comm ACM 20,
2 (Feb 1977), 113-115
2 ROTEM, D, AND VAROL, Y L Generation of binary trees from ballot sequences. J.
ACM 25,
3 (July 1978),
396-404
3 KNUTH, D E
TheArt of Computer Programming, Vol 1 FundamentalAlgorlthms
Addison-Wesley, Reading,
Mass, 1973
RECEIVED AUGUST 1978, REVISED APRIL 1979
Journal of the Assocmuon for Computing Machinery, Vol 27, No I, January 1980