Information Processing North-Holland
LISTING
Teruo
Letters
AND
17 (1983) 225-229
COUNTING
8 November
SUBTREES
OF EQUAL
SIZE OF A BINARY
1983
TREE
HIKITA
Departmenr
of Mathematics,
Tokyo Metropolitan
University, Fukazawa,
Setagaya,
Tokyo 158, Japan
Communicated by M. Paul Received March 1982
Keywords:
Binary
tree generation,
subtree
counting,
subtree
listing, inorder-preorder
sequences
1. Introduction
Our objects of study are rooted ordered binary trees and their subtrees. In particular, we only treat subtrees which share the root with their parent tree. A tree or a subtree with exactly k nodes are simply called a k-tree or a k-subtree, respectively. The purpose of this paper is to present an efficient algorithm which, given a tree and a value k, lists and enumerates all k-subtrees of the tree. There have been given many algorithms which generate all k-trees in some order [1,3,4,6-111. Ruskey [5] gave an algorithm which, given a tree, lists all subtrees of the tree in an order independent of the numbers of nodes of the subtrees. The strategy of our algorithm is simple, and can be summarized, as follows. (1) Generate all k-trees in some order, for each of them test whether it can be embedded in the given tree, and list the k-trees which have passed this test. (2) In the above, do not test the embeddability after each k-tree is completely produced, but rather do the production and test in parallel. Thus one knows the impossibility of embedding as early as possible. (3) Represent all the trees by ‘inorder-preorder sequences’. These sequences are certain permutations of the sequence 1, 2,. . . , k. The set of inorder-preorder sequences exactly corresponds to that of ordered binary trees. We do not treat here transformation algorithms between inorder-preorder sequences and other representations of trees. In this paper, first a naive algorithm is presented which lexicographically generates all these sequences of length k in average constant time per sequence. Then using it an algorithm is presented which lists and enumerates all k-noded subtrees of a given rooted ordered binary tree. As an application of the algorithm, the numbers of k-noded binary trees of height h, 0 < h < k - 1, were calculated with the aid of an computer in the range of 1 < k < 13.
2. Generation
of inorder-preorder
sequences
For a k-tree, an inorder-preorder sequence (see Fig. 1) corresponding procedure. (1) Label the nodes of the tree by 1, 2,. . . , k in inorder. 0020-0190/83/$3.00
0 1983, Elsevier Science Publishers
B.V. (North-Holland)
to the tree is given by the following
225
Volume
17, Number
4
INFORMATION
PROCESSING
LETTERS
8 November
1983
5,2,1,4,3,6,7
t------,
Fig. 1. A tree and its inorder-preorder
sequence.
(2) Read out the labels of the tree in preorder. The following properties of inorder-preorder sequences
are well-known.
of the sequence 1, 2,. . . , k. Then it is an inorder-preorder Property 1. Let a,, a,, . . . , ak be a permutation sequence if and only if there exist no indices i, j and m, i < j < m, such that a,,, < a, < a,. Property 2. A sequence is an inorder-preorder sequence 1, 2, . . . , k by using a single stack. Property 3. The set of all inorder-preorder ordered binary trees on k nodes.
sequence
sequences
if and only if it can be transformed
of length k corresponds
one-to-one
into
the
to the set of all
Inorder-preorder sequences are called tree permutations in Knott [l]. They are also called stack-sortable permutations [4] because of Property 2. ‘Permutations obtainable from a stack’ in Knuth [2, Section 2.2.1, Exercises 2-5, pp. 238-239, and Section 2.3.1, Exercise 6, p. 3291 are inverse permutations of our inorder-preorder sequences, so that they share very similar properties. There have been given some algorithms for generating inorder-preorder sequences (or similars) [1,7-91. Our Algorithm 1 (given below) is rather simple among them, and basically it is backtracking. Algorithm 1. Inorder-preorder sequence generation order in an array a. Two This generates all inorder-preorder sequences of length k in lexicographical auxiliary arrays p and q of length k are used, where p[i] and q[i] together designate the range of values a[i] can currently take. Step 1 (Initialize)
i+O; p[l] Step 2 (Loop.
+- 1; q[l] Proceed
+ k;
one position
forward)
i+i+l; a[il + ptil; Step 3 (If reached to the right end, output the current candidate still exists, and set this candidate) if i = k then output (a) ; 226
content
of a, go back to the position
where another
Volume
17, Number
INFORMATION
4
PROCESSING
LETTERS
8 November
1983
repeatici-l;ifi=Othenstop;endif;untila[i]
+
l] + p[i];
q[i + l] + a[i] - 1;
endif; Step 5 (Set p and q for the right son of a[;], if it exists) if a[i] < q[i] then p[i + a[i] - p[i] + l] + a[i] + 1; q[i + a[i] - p[i] + l] + q[i]; endif; Step 6 (Repeat
loop)
got0 Step 2; In the above algorithm note the following two facts. (1) In an inorder-preorder sequence, the ‘left son’ of one node a[i] is located immediately after the node, that is a[i + 11. (2) But the ‘right son’ is usually located in a distant position from a[i], that is a[i + a[i] - p[i] + 11. The computation time for each inorder-preorder sequence is bounded by a constant in average, as the algorithms in [7,9]. Its proof is easy and is omitted here. 3. Listing and enumerating k-subtrees Our Algorithm 2 (see below) is obtained by expanding Section 1. The algorithm is almost self-explanatory.
Algorithm
1, following
the strategy
stated
in
Algorithm 2. Listing all k-noded subtrees of a binary tree This lists all k-noded subtrees of a given rooted ordered binary tree on n nodes. The subtrees are assumed to share the root with the given tree. Input: A value k, and representations of a tree in arrays b and r of length n. A tree should be given as an inorder-preorder sequence in b. And also an array r should be given, where rli] is the index in b of the right son of btj] if it exists and 0 otherwise. Subtrees are generated in lexicographical order in an array a of length k as inorder-preorder sequences. Auxiliary arrays f, p and q of length k are used in the algorithm, where f[i] is the index in a of the father node of a[i] (used only when a[i] is the right son), and p and q are as in Algorithm 1. An array m of length k is also used, where m[i] indicates the index in b at which a[i] currently matches. Step 1 (Initialize) i +- 0; p[ll+l;
Step 2 (Loop.
Proceed
q[ll+k;
one position
forward)
i+-+l; a[il +-- p[il; 227
Volume
17, Number
4
INFORMATION
PROCESSING
LETTERS
Step 3 (Examine whether a[i] matches to some node in b, and return the result in a Boolean If successful, the position of the matched node in b is in m[i].) (Case 1. a[i] is the root) if i = 1 then match + true; m[l] + 1; (Case 2. a[i] is the left son of a[i - 11) elseif a[i - l] > a[i] then if m[i - 11= n then match + false; elseif b[m[i - l]] < b[m[i - l] + 11 then match +-false; else match + true; m[i] + m[i - l] + 1; endif; (Case 3. a[i] is the right son of a[f[i]]) else if r[m[f[i]]] = 0 then match + false; else match +--true; m[i] + r[m[f[i]]]; endif; endif; Step 4 (If no node in b matches
to a[i], find another
candidate)
if not match then repeat i + i - 1; if i = 0 then stop; endif; until a[i] < q[i]; a[i] + a[i] + 1; goto Step 6; endif; Step 5 (If reached
to the right end, output
a, and find another
candidate)
if i = k then output(a); repeat i + i - 1; if i = 0 then stop; endif; until a[i] + q[i]; a[i] + a[i] + 1; endif Step 6 (Set p and q for the left son of a[i], if it exists) if p[i] + a[i] then p[i + l] + p[i]; q[i + l] + a[i] - 1; endif; Step 7 (Set f, p and q for the right son of a[i], if it exists) if a[i] < q[i] then f[i + a[i] - p[i] + l] 4- i; p[i + a[i] - p[i] + l] + a[i] + 1; q[i + a[i] - p[i] + l] + q[i]; endif; Step 8 (Repeat
loop)
got0 Step 2; 228
8 November
variable
1983
match.
Volume
17, Number
Table 1 The numbers
4
of rooted
INFORMATION
ordered
kh0123 1 2 3 4 5 6 7 8 9 10 11 12 13
binary 4
1 0 0 0 0 0 0 0 0 0 0 0 0
2 1 0 0 0 0 0 0 0 0 0 0
4 6 6 4 1 0 0 0 0 0 0
8 20 40 68 94 114 116 94 60 28
16 56 152 316 844 1744 3340 5976 10040
PROCESSING
LETTERS
8 November
1983
trees on k nodes of height h, for 1 < k < 13 5
32 144 480 1440 4056 10856 27672 67616
6
64 352 1376 4736 15248 47104 140640
7
128 832 3712 14272 50784 172640
8
256 1920 9600 40576 156864
9
512 4352 24064 110592
10
1024 9728 58880
11
2048 21504
12
total
4096
1 2 5 14 42 132 429 1430 4862 16796 58786 208012 742900
In Algorithm 2 the following information is needed. (i) The positions of the right sons of each node of the given tree. (ii) The positions of the father nodes of each node (actually right son) of the subtree. Information (i) can be preprocessed from b and given as an input to the algorithm.
4. Enumeration of k-noded binary trees of height h Applying Algorithm 2 to the complete binary tree of height h (n = 2h+’ - l), we obtain the exact number of k-noded binary trees of height less than or equal to h. Hence we can obtain the number of k-noded binary trees of height exactly h, for each 0 < h < k - 1. These numbers have been calculated with the aid of a computer in the range 1 < k < 13, and the result is shown in Table 1. The total number of k-trees of several heights is the kth Catalan number, that is equal to ( tk)/(k + 1).
Noted added in proof Recently, in a paper by Flajolet and Odlyzko [12] a part of our Table 1 was listed for k G 7, computed by using a recurrence method.
References VI G.D. Knott, A numbering
system for binary trees, Comm. ACM 20 (1977) 113-115. PI D.E. Knuth, The Art of Computer Programming, Vol. 1, Fundamental Algorithms (Addison-Wesley, Reading, MA, 1968, 2nd ed., 1973). On the generation of binary trees, J. [31 A. Proskurowski, ACM 27 (1980) l-2. of binary trees from [41 D. Rotem and Y.L. Varol, Generation ballot sequences, J. ACM 25 (1978) 396-404.
[5] F. Ruskey, Listing and counting subtrees of a tree, SIAM J. Comput. 10 (1981) 141-150. [6] F. Ruskey and T.C. Hu, Generating binary trees lexicographically, SIAM J. Comput. 6 (1977) 745-758. [7] I. Semba, Generation of stack sequences in lexicographical order, J. Inform. Process. 15 (1982) 17-20. [S] M. Solomon and R.A. Finkel, A note on enumerating binary trees, J. ACM 27 (1980) 3-5. [9] A.E. Trojanowski, Ranking and listing algorithms for k-ary trees, SIAM J. Comput. 7 (1978) 492-509. [IO] S. Zaks, Lexicographic generation of ordered trees, Theoret. Comput. Sci. 10 (1980) 63-82. [II] S. Zaks and D. Richards, Generating trees and other combinatorial objects lexicographically, SIAM J. Comput. 8 (1979) 73-81. [12] P. Flajolet and A. Odlyzko, The average height of binary trees and other simple trees, J. Comput. Sci. 25 (1982) 171-213.
229