5
Information Processing Letters 68 (1998) 113-l 17 On the loopless generation of binary tree sequences Vincent Vajnovszki l LE21, Universite’ de Bourgogne, B.I1400,21011 Dijon Cedex, France Received 1 February 1998; received in revised form 1 September 1998 Communicated by S.G. Akl Abstract Weight sequences were introduced by Pal10 in 1986 for coding binary trees and he presented a constant amortized time algorithm for their generation in lexicographic order. A year later, Roelants van Baronaigien and Ruskey developed a recursive constant amortized time algorithm for generating Gray code for binary trees in Pallo’s representation. It is common practice to find a loopless generating algorithm for a combinatorial object when enunciating a Gray code for this object. In this paper we regard weight sequences as variations and apply a Williamson algorithm in order to obtain a loopless generating algorithm for the Roelants van Baronaigien and Ruskey’s Gray code for weight sequences. 0 1998 Elsevier Science B.V. All rights reserved. Keywords: Loopless generating algorithms; Binary tree sequences; Combinatorial problems; Design of algorithms 1. Introduction ‘ho loopless generating algorithms for binary trees have been published in the literature. The first is that of Roelants van Baronaigien [8], where trees are rep- resented by codewords, introduced by Zerling [ 141 and Lucas [3]. The drawback of this representation is that there is no method for determining the tree without actually performing the rotations given by its code- word. Note that the Gray code order given by this generating algorithm yields lexicographic order; to our knowledge it is the only nontrivial combinatorial object having this remarkable property. Roelants van Baronaigien’s loopless algorithm has been generalized for k-ary trees by Korsh [2], and in [S] the author sets a challenge to the reader to develop a loopless algorithm for listing all well-formed parenthesis strings. Recall that a well-formed parenthesis string may be regarded ’ E-mail: vvajnov@u-bourgogxfr. as a binary tree by replacing left parentheses by inter- nal nodes and right parentheses by leaves, traversing parenthesis strings left to right and trees in preorder. This challenge has been resolved recently through the independent work of Mikawa and Takaoka [5], Walsh [12] andvajnovszki [lo]. The second loopless generating algorithm for binary trees is that of Lucas, Roelants van Baronaigien and Ruskey [4] where a pointer representation for trees is used, and is thus appropriate to run on a machine for which pointer operations are implemented in an efficient manner. Another representation for binary trees is achieved by weight sequences (w-sequences in short), intro- duced by Pallo [6], where a constant amortized time algorithm for their generation in lexicographic order is developed. Note that w-sequences may represent well- formed parenthesis strings (see [ 1 l] for instance). In [7] Roelants van Baronaigien and Ruskey showed a Gray code for the set of w-sequences and developed a 0020-0190/98/$ - see front matter 0 1998 Elsevier Science B.V. All rights reserved. PII: SOO20-0190(98)00155-O

On the loopless generation of binary tree sequences

Embed Size (px)

Citation preview

Page 1: On the loopless generation of binary tree sequences

Information Processing Letters 68 (1998) 113-l 17

On the loopless generation of binary tree sequences

Vincent Vajnovszki l

LE21, Universite’ de Bourgogne, B.I1400,21011 Dijon Cedex, France

Received 1 February 1998; received in revised form 1 September 1998

Communicated by S.G. Akl

Abstract

Weight sequences were introduced by Pal10 in 1986 for coding binary trees and he presented a constant amortized time algorithm for their generation in lexicographic order. A year later, Roelants van Baronaigien and Ruskey developed a recursive constant amortized time algorithm for generating Gray code for binary trees in Pallo’s representation. It is common practice to find a loopless generating algorithm for a combinatorial object when enunciating a Gray code for this object. In this paper we regard weight sequences as variations and apply a Williamson algorithm in order to obtain a loopless generating algorithm for the Roelants van Baronaigien and Ruskey’s Gray code for weight sequences. 0 1998 Elsevier Science B.V. All rights reserved.

Keywords: Loopless generating algorithms; Binary tree sequences; Combinatorial problems; Design of algorithms

1. Introduction

‘ho loopless generating algorithms for binary trees have been published in the literature. The first is that of Roelants van Baronaigien [8], where trees are rep- resented by codewords, introduced by Zerling [ 141 and Lucas [3]. The drawback of this representation is that

there is no method for determining the tree without actually performing the rotations given by its code-

word. Note that the Gray code order given by this generating algorithm yields lexicographic order; to our knowledge it is the only nontrivial combinatorial object having this remarkable property. Roelants van Baronaigien’s loopless algorithm has been generalized

for k-ary trees by Korsh [2], and in [S] the author sets a challenge to the reader to develop a loopless algorithm for listing all well-formed parenthesis strings. Recall that a well-formed parenthesis string may be regarded

’ E-mail: vvajnov@u-bourgogxfr.

as a binary tree by replacing left parentheses by inter-

nal nodes and right parentheses by leaves, traversing

parenthesis strings left to right and trees in preorder.

This challenge has been resolved recently through the

independent work of Mikawa and Takaoka [5], Walsh

[12] andvajnovszki [lo].

The second loopless generating algorithm for binary

trees is that of Lucas, Roelants van Baronaigien and

Ruskey [4] where a pointer representation for trees

is used, and is thus appropriate to run on a machine

for which pointer operations are implemented in an efficient manner.

Another representation for binary trees is achieved

by weight sequences (w-sequences in short), intro-

duced by Pallo [6], where a constant amortized time

algorithm for their generation in lexicographic order is developed. Note that w-sequences may represent well-

formed parenthesis strings (see [ 1 l] for instance). In

[7] Roelants van Baronaigien and Ruskey showed a

Gray code for the set of w-sequences and developed a

0020-0190/98/$ - see front matter 0 1998 Elsevier Science B.V. All rights reserved.

PII: SOO20-0190(98)00155-O

Page 2: On the loopless generation of binary tree sequences

114 V Vajnovszki /Information Processing Letters 68 (1998) 113-117

Fig. 1. A binary tree with the w-sequence 11241131.

recursive constant amortized time algorithm for listing this Gray code.

In the present paper we modify the Williamson [ 13, p. 1121 algorithm for generating variations in order to obtain a loopless generating algorithm for the Roelants van Baronaigien and Ruskey’s Gray code for w-sequences.

2. Definitions

The inorder (or symmetric order) of the nodes of

a tree is obtained by the following recursive traverse procedure: traverse first the left subtree in inorder, then visit the root, and then traverse the right subtree in inorder. Hereafter, in a binary tree, node i means its ith node in an inorder traversal.

The binary tree weight sequence was introduced by Pal10 in [6] where a constant time (on average) algorithm for their generation is presented; the weight sequence of an n-node binary tree (w-sequence in

short) is the integer sequence w = w 1 w2 . . . wn where each wi is the number of leaves in the left subtree of node i. For example, the tree in Fig. 1 has the weight sequence 11241131. In [6] Pal10 also shows a one-to- one correspondence between w-sequences and binary trees and gives the following theorem.

Theorem. An n-length integer sequence WI w2 . . . w, is the w-sequence for a binary tree ifSfor all i, 1 6 i 6

n, (i) 1 < wi < i, and

(ii) if j E [i -wt+l,i]theni-wi < j-wj.

A rotation is a transformation over trees defined as follows. If node k is the right child of node j then a left rotation makes j the new left child of k, the old

Fig. 2. The rotation of a binary tree.

left child of k becomes the new right child of j, and the old parent of j (if any) becomes the new parent of k. A right rotation transforms the tree obtained back to the original, see Fig. 2.

Roelants van Baronaigien and Ruskey [7] observe that the rotation of a tree at a particular node produces minimal change in the tree’s w-sequence. They state the following lemma.

Lemma. Let be a binary tree with weight sequence

ww2 . . . ?_I.&-_IV&Wk+l . . . w,, with node k the right child of its parent. Then the tree obtained by peeorm-

ing a leji rotation on node k has the weight sequence

WIWZ... Wk-l(wk + wj)wk+l . . . Wnv

if the parent of node k would be enumerated j in an inorder traversal, and we have j = k - Wk.

Conversely, the tree obtained by performing a right

rotation on node k has the weight sequence

WIWZ... Wk-1 (wk - wjbk+I . . . WnT

where j is the left child of node k.

The search in O(1) time of the left child of a given node is one of the difficulties of the generating algorithm; we get it over by employing a double representation for trees, as in [7]: a linked and a w-sequence representation. For example, a linked representation may consist of three arrays 1, r , p where for each i, 1 < i < n, lt , ri , pi are, respectively, the left child, the right child and the parent of node i, if any, and 0 otherwise. Obviously, the linked representation of a binary tree also has a minimal change property, in the sense that, for updating it, after each rotation of a tree we have just to modify the three thick-lined links in Fig. 2; see procedures inc and dec at the end of the next section for an algorithmic implementation.

Page 3: On the loopless generation of binary tree sequences

V Vujnovszki /Information Processing Letters 68 (1998) 113-I I7 115

3. The generating algorithm

This section begins with a discussion of William- son’s [ 13, p. 1121 loopless algorithm for generating variations in Gray code order, i.e., eIements of the product space

S=St x&x...xS,, with (1)

si =(O,l,..., ri-l] fori=1,2 ,..., n,

such that two consecutive generated sequences differ in a single position. Then, we modify it in order to

make it able to generate binary tree w-sequences. Let L: be the list of all sequences u1 v2 _ . , u, in Gray

code order, with vi belonging to a set Si as above (1 f i 6 n), and v a particular sequence in .C. We denote by .C(v, i) the contiguous sublist of C which contains u and with a fixed value (equal to vi) of the items in position i; and byJirst(Qv, i)) its first element. In other words, first(L(v, i)) is the first sequence which precedes TV in the list L: such that there is no transition of the item in position i between it and u. We say that item Vi is last changingfrom left to right in a sequence u if either i = 1 or, for all j, 1 6 j < i,first(l(v, j))

precedesJirst(,C(v, i)) in the list ,C. A subsequence v’ of v is called lef to right extremal (h-e in short) if, for all items Vi in u’, (1) ui is last changing from left to right in U, (2) vj is an extremal value in Si (i.e., vi = 0 or Vi =

ri - l), (3) 21’ is maximal relatively to inclusion, that is, u’ is

not a proper subsequence of another subsequence of v satisfying properties 1 and 2 above.

Note that Zre subsequences are pairwise disjoint and not adjacent, see Table 1 for an example. An item in v is called free if it does not belong to any ire

subsequence. In order to keep track of free items in

v, the algorithm employs an additional (n + I)-length arraye=etez... e,e,,+t where, for each i, ei is equal to i - 1 minus the length of the Ere subsequence ending with vi _ 1. More formally

I

i - 1, if t~i _ 1 is not the right-most item in an Ire subsequence of v,

ei= j<i-1, ifUj+tVj+2...Vi_t isanlre

subsequence of v.

As we shall see below, array e is used to find the right-most free item in a sequence v without employing loop statement. Also, we associate with

Table 1 Variations in the product space {0,1,2] x (0,1,2) x {O, 1) in Gray code order

v e U e

1 000 0123 10 111 0122

2 001 0122 11 101 0113

3 011 0123 12 100 0121

4 OlQ 0122 13 200 0023

5 020 0113 14 201 0022

6 021 0121 15 211 0023

1 121 0123 16 210 0022

8 120 0122 17 220 0103

9 110 0123 18 221 0120

In sequences u, Ire subsequences are underlined and the right-most free items are in bold face. In sequences e, items ei # i - 1 (i.e.,

“tyfl..’ vi- 1 are Ire subsequences) are in bold face.

each item vi E Si of a sequence v a direction di (up or down).

The procedure succ which computes the successor, in Gray code order, of a sequence v works as follows. Let Vk be the right-most free item in u. It is modified by making the smallest change in the appropriate direction: inC(Vk) if dk = up or dec(vk) if dk = down,

and remove the Ire subsequence beginning with V&l

(and ending with v,), if there is one, by setting e,+t to n. In addition, if Vk reaches an extreme in its direction then we change it and we either add Vk to the Ire subsequence ending with Vk_ 1 (if vk_ 1 belongs to such a subsequence), or create with Vk a single item b-e

subsequence. Initially the algorithm starts with vi = mitt($),

di =UP for i = 1,2,..., n, and ei = i - 1 for i = 1,2, . . . , n + 1, that is, v is the smallest lexicographical

sequence and all items are free. The iterative call of the procedure succ generates all sequences IJ = vt v2 . . . v, with vi belonging to Si, in Gray code order. At each computational step the next changed item (the right- most free item) is in position k = e,+t . The algorithm terminates when it tries to change the item in position zero, in other words, when the entire sequence is Ire.

procedure succ; e,+l := n; ifdk = UP

then k(Q);

Page 4: On the loopless generation of binary tree sequences

116 I? Vajnovszki /Information Processing LRtters 68 (1998) 113-l 17

else dec(vk); endif if vk = Inin or Vk = ma(&)

then dk := if dk = up then down else up

C?k+] :=ek;

ek :=k- 1; endif k := e,+l; end.

The call inc(Vk), respectively dec(vk), assigns to the item nk belonging to Sk its successor, respectively its predecessor and max(&) and mm(&) giVe, re- spectively, the largest and smallest elements of the set Sk. In WikimsOn’s dgotithm, inc( vk) is eqUiV&nt to vk := vk + 1, dec(vk) t0 Vk := Vk - 1, tin(&) iS eqUd

to 0 and max(&) is equal to rk - 1, see relation (1). The current algorithm is quite general and it is

able to generate sequences ~1~2.. . II,, where each item Vi belongs to a (finite) total ordered set Si whose cardinal@ is greater then one; it also covers the case when the set Si of all possible values of item Vi in the sequence u depends on the values of other items uj (j # i) in u. The algorithm remains loopless if the procedures inc and dec, which make the smallest change of the current item in its direction, are implemented without a loop statement. It generates binary tree w-sequences as a particular case: if we make mm($) equal to 1, mm($) equal to i, and define the procedures deC(Vk) and inc(vk) as below, then the call succ when e,+t is not 1 (since in a w-sequence the first item is always equal to one) generates the Roelants van Baronaigien and Ruskey’s [7] w-sequence Gray code. The global arrays 1, r and p are defined as at the end of Section 2, and initial values are those corresponding to the tree where each node has a leaf as left child.

procedure inC( vk) ;

Vk := Vk + Uk_uk ; {update arrays I, r, p}

j :=pk;

if pj # 0 then if l,j = j then lPj := k; else rpj := k; endif

endif pk I= Pj;

rj :=lk;plk := j; pj :=k;lk :=j;

end.

Table 2 The Slength w-sequences in Gray code order generated by the succ procedure

1 11111

2 11112

3 11113

4 11114

5 11115

6 11125

7 11124

8 11123

9 11121

10 11131

11 11134

12 11135

13 11145

14 11141

15 11241 29 12345

16 11245 30 12341

17 11235 31 12311

18 11234 32 12312

19 11231 33 12315

20 11211 34 12115

21 11212 35 12113

22 11214 36 12112

23 11215 31 12111

24 11315 38 12121

25 11312 39 12123

26 11311 40 12125

27 11341 41 12145

28 11345 42 12141

procedure dec (vk) ; vk := vk - Vlk; {update arrays 1, r, p}

j := lk;

ifpj#Othen ifl,=kthenl,,:=j;elser,,:=j;en~f

endif Pj := Pk;

lk I=rj;prj :=k;

pk:=j;rj:=k;

end.

4. Conclusions

We have presented a new loopless generating algo- rithm for binary trees based on Williamson’s [ 131 vari- ations generating algorithm. It generates Pallo’s [6] bi- nary tree w-sequences and uses the same data struc- tures and generates the same Gray code as in Roelants van Baronaigien and Ruskey’s paper [7], where rank- ing and unranking algorithms are also presented. Gen- eralizing this technique for k-q trees or applying it to other combinatorial objects are open problems.

References

[l] Y. Koda, F. Ruskey, A Gray code for the ideals of a forest pose& J. Algorithms 15 (1993) 324-340.

Page 5: On the loopless generation of binary tree sequences

c! Vajnovszki /Information Processing Letters 68 (1998) 113-l I7 117

[2] J.F. Korsh, Loopless generation of k-q tree sequences,

Inform. Process. Len. 52 (1994) 243-247. [3] J.M. Lucas, The rotations graph of binary trees is Hamiltonian,

J. Algorithms 9 (1988) 503-535. [4] J.M. Lucas, D. Roelants van Baronaigien, F. Ruskey, On

rotations and the generation of binary trees, J. Algorithms 15

(1993) 343-366. [5] K. Mikawa, T. Takaoka, Generation of parenthesis strings by

transpositions, in: Proc. CATS’97, Sydney, Australia, February

1997. [6] J.M. Pallo, Enumerating, ranking and unranking binary trees,

Comput. J. 29 (1986) 171-175. [7] D. Roelants van Baronaigien, F. Ruskey, A Hamiltonian path

in the rotation lattice of binary trees, Congr. Numer. 59 (1987)

313-318. [8] D. Roelants van Baronaigien, A loopless algorithm for gener-

ating binary tree sequences, Inform. Process. L&t. 39 (1991)

189-194.

[9] F. Ruskey, A. Proskurowski, Generating binary trees by trans-

positions, J. Algorithms 11 (1990) 68-84.

[lo] V. Vajnovszki, Loopless algorithm for generating well-formed

parenthesis strings, Research Report Department IEM, Univer-

sity of Burgundy, September 1997.

[ 1 l] V. Vajnovszki, J. Pallo, Parallel algorithms for generating well-

formed parenthesis strings, Parallel Process. Lett. 8 (1) (1998)

19-28.

[12] T.R. Walsh, A simple sequencing and ranking method that

works on almost all Gray codes, Research Report No. 243,

Department of Mathematics and Computer Science, University

of Quebec in Montreal, 1995.

[13] S.G. Williamson, Combinatorics for Computer Science, Com-

puter Science Press, Rockville, MD, 1985.

[14] D. Zerling, Generating binary trees using rotations, J. ACM 32

(1985) 694-701.