restricted rotation distance between binary trees

6
Information Processing Letters 84 (2002) 333–338 www.elsevier.com/locate/ipl Restricted rotation distance between binary trees Sean Cleary 1 Department of Mathematics, City College of CUNY, New York, NY 10031, USA Received 18 December 2001; received in revised form 4 April 2002 Communicated by L.A. Hemaspaandra Abstract Restricted rotation distance between pairs of rooted binary trees measures differences in tree shape and is related to rotation distance. In restricted rotation distance, the rotations used to transform the trees are allowed to be only of two types. Restricted rotation distance is larger than rotation distance, since there are only two permissible locations to rotate, but is much easier to compute and estimate. We obtain linear upper and lower bounds for restricted rotation distance in terms of the number of interior nodes in the trees. Further, we describe a linear-time algorithm for estimating the restricted rotation distance between two trees and for finding a sequence of rotations which realizes that estimate. The methods use the metric properties of the abstract group known as Thompson’s group F . 2002 Published by Elsevier Science B.V. Keywords: Algorithms and data structures; Binary trees; Rotation distance 1. Introduction There are a number of ways of measuring the difference in shape between two rooted binary trees with the same number of leaves. In the particular case of rooted binary trees with a left-to-right ordering on the leaves (such as binary search trees), the difference of shape is of particular interest in balancing of trees. Rotations are a small change in the shape of a binary tree commonly used as primitive steps in tree balancing (see Knuth [11]). Rotation distance measures this difference by counting the minimal number of fundamental rotations, which can take place at any node, to transform one tree into the other. E-mail address: [email protected] (S. Cleary). URL address: http://www.sci.ccny.cuny.edu/~cleary. 1 Supported by PSC-CUNY grant #63438-0032. Work by Culik and Wood [7], Pallo [14,15] and Makinen [13] has clarified the properties of rotation distance. Sleator, Tarjan and Thurston [17], by using geometric methods, obtained an upper bound of 2n 6 rotations needed to transform one rooted binary tree with n interior nodes into any other. Furthermore, they showed that the 2n 6 bound is achieved for large values of n and thus is the best possible upper bound. Later work by Luccio and Pagli [12], Pallo [15], Hanke, Ottmann, and Schuierer [10] and Rogers [16] has shown bounds using methods which do not rely on hyperbolic geometry. There is no known polynomial-time algorithm for computing rotation distance, but work of Pallo [15] and Rogers [16] has given polynomial-time algorithms which estimate rotation distance. We consider ordered, rooted binary trees with n interior nodes and where each interior node has 2 0020-0190/02/$ – see front matter 2002 Published by Elsevier Science B.V. PII:S0020-0190(02)00315-0

Upload: sean-cleary

Post on 02-Jul-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Information Processing Letters 84 (2002) 333–338

www.elsevier.com/locate/ipl

Restricted rotation distance between binary trees

Sean Cleary1

Department of Mathematics, City College of CUNY, New York, NY 10031, USA

Received 18 December 2001; received in revised form 4 April 2002

Communicated by L.A. Hemaspaandra

Abstract

Restricted rotation distance between pairs of rooted binary trees measures differences in tree shape and is related to rotationdistance. In restricted rotation distance, the rotations used to transform the trees are allowed to be only of two types. Restrictedrotation distance is larger than rotation distance, since there are only two permissible locations to rotate, but is much easierto compute and estimate. We obtain linear upper and lower bounds for restricted rotation distance in terms of the number ofinterior nodes in the trees. Further, we describe a linear-time algorithm for estimating the restricted rotation distance betweentwo trees and for finding a sequence of rotations which realizes that estimate. The methods use the metric properties of theabstract group known as Thompson’s groupF . 2002 Published by Elsevier Science B.V.

Keywords: Algorithms and data structures; Binary trees; Rotation distance

1. Introduction

There are a number of ways of measuring thedifference in shape between two rooted binary treeswith the same number of leaves. In the particular caseof rooted binary trees with a left-to-right ordering onthe leaves (such as binary search trees), the differenceof shape is of particular interest in balancing oftrees. Rotations are a small change in the shape ofa binary tree commonly used as primitive steps intree balancing (see Knuth [11]). Rotation distancemeasures this difference by counting the minimalnumber of fundamental rotations, which can takeplace at any node, to transform one tree into the other.

E-mail address: [email protected] (S. Cleary).URL address: http://www.sci.ccny.cuny.edu/~cleary.

1 Supported by PSC-CUNY grant #63438-0032.

Work by Culik and Wood [7], Pallo [14,15] andMakinen [13] has clarified the properties of rotationdistance. Sleator, Tarjan and Thurston [17], by usinggeometric methods, obtained an upper bound of 2n−6rotations needed to transform one rooted binary treewith n interior nodes into any other. Furthermore,they showed that the 2n − 6 bound is achieved forlarge values ofn and thus is the best possible upperbound. Later work by Luccio and Pagli [12], Pallo[15], Hanke, Ottmann, and Schuierer [10] and Rogers[16] has shown bounds using methods which do notrely on hyperbolic geometry. There is no knownpolynomial-time algorithm for computing rotationdistance, but work of Pallo [15] and Rogers [16]has given polynomial-time algorithms which estimaterotation distance.

We consider ordered, rooted binary trees withn

interior nodes and where each interior node has 2

0020-0190/02/$ – see front matter 2002 Published by Elsevier Science B.V.PII: S0020-0190(02)00315-0

334 S. Cleary / Information Processing Letters 84 (2002) 333–338

children. Such trees are commonly calledextendedbinary trees [11] or 0–2trees. In the following, ‘binarytree’ refers to such a tree, ‘node’ refers to an interiornode, and ‘leaf’ refers to a non-interior node. Ourtrees will haven + 1 leaves numbered in left-to-rightorder from 0 ton. With the ordinary rotation distance,a rotation can take place at any of then nodes inthe rooted binary tree withn + 1 leaves. We definerestricted rotation distance, where we consider thesame rotation operation, but we only allow rotation atone of two nodes—either the root or the right childof the root. (Note this restricted rotation distance isdifferent than in [1], where the rotations are restrictedto allow rotations only at nodes with an external edge.)With this minimal sufficient set of allowed rotations,we find an upper bound of 12n for the restrictedrotation distance between any two rooted trees withn nodes, and a lower bound of(n − 1)/3 for theminimum number of such rotations needed to changeone tree to another if the trees satisfy a reductioncondition. We describe a linear-time algorithm to findthe rotations to transform the tree. These estimates andalgorithm are obtained by considering the word metricon Thompson’s groupF , an abstract group with manyremarkable properties, which has been extensivelystudied in the fields of combinatorial group theory,measure theory and logic. Cannon, Floyd and Parry[6] have written an excellent introduction toF . Themetric properties ofF have been studied by Burillo[4] and by Burillo, Cleary and Stein [5], leading tothis understanding of restricted rotation distance in thecontext of rooted binary trees.

2. Restricted rotation distance

Rotation at a node of a rooted binary tree is definedas as a simple change toT and is illustrated in Fig. 1as taking the left-hand tree to the right-hand one. Leftrotation at a node is the inverse operation, and takesthe right hand tree to the left-hand one. Therotationdistance dR(T1, T2) between two rooted binary treesT1 and T2 with the same number of leaves is theminimum number of rotations needed to transformT1 to T2, where the rotations can be performed atany node. In the following,T1 andT2 are trees withthe same number of leaves. Allowing rotations at anynode gives a sufficient set of fundamental moves—

Fig. 1. Right rotation at nodeN .

any treeT1 can be transformed to any other treeT2by a sequence of such rotations. We can considersmaller sets of allowed rotations, occurring at only asubset of all nodes. Culik and Wood [7] showed thatallowing rotations only to take place at nodes on theright “spine” is sufficient to transform anyT1 to T2,and takes no more than 2n−2 steps. Their allowed setof rotations grows with the size of the trees. We canrestrict further to finite sets of nodes where rotationis allowed. Allowing rotation at only a single node isinsufficient to transform anyT1 to T2 but we can geta minimal sufficient set of fundamental rotations if wechoose the allowed two nodes to be the root and theright child of the root. It is not immediately clear thatit is possible to transform anyT1 to T2 by a sequenceof rotations taking place at only those two nodes, butthat will be shown as Lemma 1 below. With respect tothis minimal set of fundamental moves, we define therestricted rotation distance dRR(T1, T2) between tworooted binary treesT1 andT2 with the same numberof leaves as the minimum number of rotations at theroot or the right child of the root needed to transformT1 to T2. We can think of this restricted setting ofrotation distance as analogous to the restriction ofarray operations to stack operations. With ordinaryrotation distance, we are able to randomly access anynode and perform a rotation there, but with restrictedrotation distance, we are limited to access only theroot and right child of the root. With this restriction,transformations take more steps, just as using stackoperations instead of array operations to accomplishtasks takes more steps. Surprisingly, the number ofsteps required to transformT1 to T2 is still linear inthe size of the tree, despite the fact that it may take

S. Cleary / Information Processing Letters 84 (2002) 333–338 335

many rotations even to move a particular node to oneof the two places where rotations are allowed.

Clearly, the restricted rotation distance betweentwo trees is bounded below by (ordinary) rotationdistance. In general, restricted rotation distance ismuch greater as it may take many rotations at thetwo distinguished nodes to accomplish the equivalentoperation of a single rotation at a single node at agreat distance from the root. There is a trade-off;though the number of steps required to perform thetransformation is higher, the individual steps are verysimple and only of two types. To understand restrictedrotation distance, we study Thompson’s groupF .

3. Thompson’s groupF and tree pair diagrams

Thompson’s groupF can be defined in three equiv-alent ways, all of which are useful in understandingthe structure of the group. Analytically, we can defineF as a group of piecewise-linear homeomorphisms ofthe unit interval. Combinatorially, we can defineFin terms of generators and relations. Geometrically,we can regardF as equivalence classes of pairs ofrooted binary trees. The remarkable equivalence ofthese three characterizations ofF is described in [6].

Analytically, we defineF as the group of orien-tation-preserving piecewise-linear homeomorphismsfrom [0,1] to [0,1] where each homeomorphism hasonly finitely many singularities of slope, all suchsingularities lie in the dyadic rationalsZ[1

2], and, awayfrom the singularities, the slopes are powers of 2.

Combinatorially,F has infinite presentation:⟨x0, x1, . . . |x−1

i xnxi = xn+1,∀i < n⟩.

There is a set of normal forms for elements ofF givenby

xr1i1

xr2i2

. . . xrkik

x−sljl

. . . x−s2j2

x−s1j1

with ri, si > 0, i1 < i2 · · · < ik and j1 < j2 · · · < jl .This normal form is unique if we further require areduction condition that when bothxi andx−1

i occur,so doesxi+1 or x−1

i+1 as discussed in [2]. The relationsprovide a quick and efficient manner to rewrite aword into normal form, and form a complete rewritingsystem, as described in [3].

There is also a finite presentation forF ; sincex0 conjugatesx1 to x2 and similarly higher-index

generators are also conjugates ofx1 by x0, the twogeneratorsx0 and x1 suffice to generate the wholegroup. Furthermore, all of the infinitely many relationsare consequences of two relations of length 10 and 18(see [2]).

The geometric description ofF is in terms of treepair diagrams. Atree pair diagram is a pair of rootedbinary trees with the same number of leaves, as de-scribed in [6]. Essentially, a rooted binary tree can beregarded as a procedure for constructing a subdivisionof the unit interval by successive halving of subin-tervals. A pair of such trees(S,T ) gives an elementof F from the analytical perspective by consideringthe elementf which is the piecewise-linear homeo-morphism which realizes the interpolation of subdivi-sions described by the source treeS and the target treeT . The equivalence between the rooted tree pair di-agram perspective and the combinatorial perspectivesdescribed above by the infinite and finite presentationsis described in [6], and the tree pair diagrams associ-ated to generatorsx0 andx1 are pictured in Fig. 2. Notethat the generatorx0 performs a right rotation at theroot and that the generatorx1 performs a right rotationat the right child of the root. The inversesx−1

0 andx−11

are left rotations at their respective nodes. The (op-tional) generators in the infinite generating set definedasxn = x−n+1

0 x1xn−10 are right rotations at the noden

levels down on the right side of the tree. The group op-eration in terms of the tree pair diagram representationis composition; it may be necessary to expand trees (bychanging a leaves to an interior nodes and adding newleaves) to perform composition (see [6]). Given anypair of trees(T1, T2) with the same number of leaves,we can consider that as representing an element ofF .

Fig. 2. Elementsx0 andx1 of F .

336 S. Cleary / Information Processing Letters 84 (2002) 333–338

And given any elementf of F , we can find(T1, T2)

representingf , which will be unique if we further re-quire that the tree pair(T1, T2) is reduced, as describedbelow. Thus, there is a one-to-one correspondence be-tween reduced tree pair diagrams and elements ofF .Now we are in a position to prove the lemma:

Lemma 1.A rooted binary tree T1 can be transformedinto any other tree T2 with the same number of leavesby a sequence of rotations at the root and right childof the root.

Proof. The tree pair diagram(T1, T2) represents anelement ofF . Sincex0 and x1 generateF , we canexpress the rotations needed to changeT1 to T2 as asequence of right and left rotations at the root (x±1

0 )

and right child of the root (x±11 ). Thus we have the

lemma. ✷We can also prove the lemma by noting that we

can express a rotation at any node on the right sideof the tree n levels down as the conjugatexn =x−n+1

0 x1xn−10 . Since Culik and Wood [7] showed that

rotations at nodes on the right side of the tree sufficeto change anyT1 to T2, thusx0 andx1 suffice.

4. The metric onF and restricted rotationdistance

Given a groupG in terms of a finite presentation,we define thelength of a wordg in G as the numberof generators counted with unsigned multiplicity in aminimal-length representative ofg as a word in termsof the generators. This length is precisely the distancefrom the identity in the Cayley graph for the groupG

with respect to that generating set, where each edgein the Cayley graph is declared to have length 1. Thefield of geometric group theory has concerned itselfwith understanding the metric properties of groups andthe consequences of metric hypotheses on groups; seeGromov [9] and Epstein et al. [8] for an introduction.

Burillo [4] and Burillo, Cleary and Stein [5] ana-lyzed the word metric onF . Given a wordx in F ,there are many representatives ofx in terms of the fi-nite generating set{x0, x1}. The length|x| with respectto that finite generating set is the shortest such repre-sentative. The relevant result from [5] is

Theorem 1. Let x ∈ F have normal form xr1i1

xr2i2

. . .

xrnin

x−smjm

. . . x−s2j2

x−s1j1

, and let D(x) = r1 + r2 + · · · +rn + s1 + s2 + · · · + sm + in + jm. Then we have

D(x)

3� |x| � 3D(x).

We can defineN(x) to be the number of interiornodes in either tree of the reduced tree pair diagramrepresentingx. We showed in [5] that

Theorem 2. Let x ∈ F be as above, then D(x)/4 �N(x) � D(x) + 1. Thus we have

N(x) − 1

3� |x| � 12N(x).

These results give us an expression for the max-imum rotation distance in terms of the number ofnodes:

Theorem 3. Given two rooted binary trees T1 andT2 each with n interior nodes, the restricted rotationdistance dRR(T1, T2) � 12n.

Proof. Consider the elementx of F given by the treepair diagram(T1, T2). We know thatx has length lessthan or equal to 12N(x) with respect to the generatingset{x0, x1} and can be thus represented by a string ofno more than 12N(x) x0’s andx1’s and their inverses.Since these generators correspond to left and rightrotations at the root and right child of the root, we havethe result. ✷

We also can apply the metric on Thompson’s groupto get a lower bound on the number of rotations at theroot and right child of the root needed to transformT1to T2 by understanding the notion of a reduced treepair diagram.

Definition 4.1. A tree pair diagram(T1, T2) is unre-duced if there is ani such that theith and(i + 1)thleaves ofT1 are both children of the same node andthe correspondingith and(i + 1)th leaves ofT2 arealso both children of the same node. A tree pair dia-gram which is not unreduced isreduced.

In the reduction illustrated in Fig. 3, the leavesnumbered 1 and 2 ofT1 are both children of the same

S. Cleary / Information Processing Letters 84 (2002) 333–338 337

Fig. 3. Example of reduction and relabeling.

node, as are the corresponding leaves 1 and 2 inT2,so there is a possible reduction there and also at leaves5 and 6. After reduction, there are two fewer leavesand the leaves can be renumbered from left to right.So after reduction and relabeling, the tree pair diagram(T1, T2) becomes(T ′

1, T′2). Note that the rotations that

transform T1 to T2 are exactly the same rotationsneeded to transformT ′

1 to T ′2, as the reducible pieces of

T1 andT2 are carried along unchanged. Those reducedpieces hang from leaves inT ′

1 andT ′2 which, though

they may be moved around by the transformation,are not descended into and thus the reducible piecesare carried around unchanged. Applying the lowerinequality of Theorem 2, we have the following, whichgives a lower bound on the number of rotations at theroot and right child of the root needed to transform onetree to another:

Theorem 4. If T1 and T2 are rooted binary treeswith n nodes each which form a reduced tree pairdiagram (T1, T2), then the restricted rotation distancedRR(T1, T2) � (n − 1)/3.

5. Algorithm for computing restricted rotationdistance

We now describe an efficient algorithm for givinga sequence of rotations to transformT1 to T2, rootedbinary trees with the same number of leavesn. Ourrotations are restricted to those at the root and rightchild of the root. GivenT1 and T2, we consider theelementx in F whose tree pair diagram is(T1, T2).We construct the normal form inF by the followingprocess, described in [6]: number the leaves ofT1 andT2 from left to right beginning at 0. For theith leaf,

we count the maximal length path of left edges begin-ning at the leaf which does not reach the right side ofthe tree. That is, we consider the set of ancestors of theith leaf, and count the number of ancestors which areconnected to leafi by a path consisting entirely of leftedges, subtracting one if the most distant such ances-tor is on the right side of the tree. Call these lengthsri for T1 and si for T2; many of these lengths maybe zero. The wordxr0

0 xr11 . . . x

rnn x

−snn . . . x

−s11 x

−s00 rep-

resentsx in the normal form of the infinite generatingset forF . Many of the exponents are possibly zero andintroduce nothing into the word. Then, we use the rela-tionshipxk = x−k+1

0 x1xk−10 to express the generators

xk for k � 2 in terms of the two generatorsx0 andx1.We then perform the obvious cancellations, cancelingx0x

−10 andx−1

0 x0 which can be quickly eliminated in asingle pass though the word. (The potential cascadingof cancellations such as that encountered in a subwordof the formx2

0x−11 x0x

−10 x1x

−20 cannot occur because

of the original form of the wordx is in a standard formfor the infinite presentation—guaranteeing that all thex1’s precede all thex−1

1 ’s and that there is a net non-zero exponent forx0 between the lastx1 and the firstx−1

1 .) This givesx expressed as a product ofx0’s andx1’s which will transform the treeT2 to the treeT1. Ifwe want to transformT1 to T2, we take the inversex−1

which gives a sequence of rotations transformingT1 toT2. This may not be the optimal representation ofx re-alizing dRR(T1, T2), but a consequence of Theorem 1is that this estimate is within a factor of three of the op-timum. The steps required to find this expression of ro-tations between two trees each withn leaves involves:a single traversal of each tree to compute the expo-nents for each leaf and thus construct the wordx, asingle pass through the wordx to express it in terms ofx0’s andx1’s, a single pass through the resulting wordto cancel occurrences ofx0x

−10 andx−1

0 x0, and a sin-gle pass through the resulting word to reverse it. Sinceeach pass requires at most a linear number of steps inn, the algorithm is linear in the size of the given trees.

As an example, given the treesT1 andT2 in Fig. 4,we estimate the reduced rotation distance and find thesequence of rotations realizing that estimate. First, wefind the exponents forT1. In T1, leaf 0 has no path ofleft edges which do not reach the right-hand side of thetree (there is a single left edge beginning at leaf 0, butit reaches the right side of the tree). Leaf 1 inT1 has asingle left edge not reaching the right side of the tree,

338 S. Cleary / Information Processing Letters 84 (2002) 333–338

Fig. 4. Sequence of rotations to changeT1 to T2.

so its exponent is 1. Continuing, we get exponents forT1 being(0,1,0,0,0,0). ForT2, we get exponents ofthe leaves as(1,0,1,1,0,0). Thus, the wordx in F

representing(T1, T2) is

x10x0

1x12x1

3x04x0

5x−05 x−0

4 x−03 x−0

2 x−11 x−0

0

which, after omitting terms with exponent 0, isx0x2x3x

−11 . We act on the relationship(T1, T2) =

x0x2x3x−11 to get (T1x1x

−13 x−1

2 x−10 , T2) = id, a rep-

resentative of the identity transformation and thus twocopies of the same tree. Then we use the relationshipsx−1

2 = x−10 x−1

1 x0 andx−13 = x−2

0 x−11 x2

0 to rewrite thisas a sequence of rotations:x1x

−20 x−1

1 x20x−1

0 x1x0x−10 .

We cancel out the occurrences ofx0x−10 to get

x1x−20 x−1

1 x0x−11 . That sequence of 6 rotations trans-

formsT1 to T2 as shown. For this small example, thatturns out to be the unique minimal sequence of rota-tions anddRR(T1, T2) = 6, but in general, there maybe many such minimal sequences of rotations and thealgorithm may find a longer sequence than one of theminimal ones. A better understanding of the metricproperties of the groupF might lead to algorithms tobetter compute the restricted rotation distance.

Acknowledgements

The author would like to thank Joel Hass andKatherine St. John for helpful conversations and theanonymous referees for helpful suggestions.

References

[1] A. Bonnin, J. Pallo, A shortest path metric on unlabeled binarytrees, Pattern Recognition Lett. 13 (1992) 411–415.

[2] K.S. Brown, R. Geoghegan, An infinite-dimensional torsion-freeFP∞ group, Invent. Math. 77 (1984) 367–381.

[3] K.S. Brown, The geometry of rewriting systems: a proofof the Anick–Groves–Squier theorem, in: Algorithms andClassification in Combinatorial Group Theory (Berkeley, CA,1989), Springer, New York, 1992, pp. 137–163.

[4] J. Burillo, Quasi-isometrically embedded subgroups ofThompson’s groupF , J. Algebra 212 (1) (1999) 65–78.

[5] J. Burillo, S. Cleary, M.I. Stein, Metrics and embeddings ofgeneralizations of Thompson’s groupF , Trans. Amer. Math.Soc. 353 (4) (2001) 1677–1689.

[6] J.W. Cannon, W.J. Floyd, W.R. Parry, Introductory notes onRichard Thompson’s groups, Enseign. Math. 42 (1996) 215–256.

[7] K. Culik II, D. Wood, A note on some tree similarity measures,Inform. Process. Lett. 15 (1) (1982) 39–42.

[8] D.B. Epstein, J.W. Cannon, D.F. Holt, S. Levy, M.S. Paterson,W.P. Thurston, Word Processing in Groups, Jones and Bartlett,1992.

[9] M. Gromov, Hyperbolic groups, in: S.M. Gersten (Ed.), Essaysin Group Theory, Springer, Berlin, 1987.

[10] S. Hanke, T. Ottmann, S. Schuierer, The edge-flipping distanceof triangulations, J. UCS 2 (8) (1996) 570–579.

[11] D.E. Knuth, Sorting and Searching, in: The Art of ComputerProgramming, Vol. 3, Addison-Wesley, Reading, MA, 1973.

[12] F. Luccio, L. Pagli, On the upper bound on the rotation distanceof binary trees, Inform. Process. Lett. 31 (2) (1989) 57–60.

[13] E. Mäkinen, On the rotation distance of binary trees, Inform.Process. Lett. 26 (5) (1988) 271–272.

[14] J. Pallo, On the rotation distance in the lattice of binary trees,Inform. Process. Lett. 25 (6) (1987) 369–373.

[15] J. Pallo, An efficient upper bound of the rotation distance ofbinary trees, Inform. Process. Lett. 73 (3–4) (2000) 87–92.

[16] R. Rogers, On finding shortest paths in the rotation graphof binary trees, in: Proc. Southeastern Internat. Conf. onCombinatorics, in: Graph Theory, and Computing, Vol. 137,1999, pp. 77–95.

[17] D.D. Sleator, R.E. Tarjan, W.P. Thurston, Rotation distance,triangulations, and hyperbolic geometry, J. Amer. Math.Soc. 1 (3) (1988) 647–681.