# Reference for fundamental theorem on tree rotations

Two binary search trees are said to be linearly equivalent when they agree in their in-order traversals. The following theorem explains why tree rotations are so fundamental:

Let A and B be binary search trees. Then A and B are linearly equivalent if and only if they are connected by a sequence of tree rotations.

I noticed this result when I was first learning about data structures long ago and wanted to understand the special status of tree rotations more deeply.

The proof is simple and intuitive: Rotate the least element up to the root position along the leftward spine. By the order invariant, this rearranged tree cannot have a left subtree. Now recurse on the right subtree. The result is a normal form for testing linear equivalence.

While it's a basic theorem, I've never come across it in the literature. I would greatly appreciate a reference for the next time I need to use this result.

(Bonus brain teaser: What's the best algorithm for finding the shortest sequence of tree rotations that connect two linearly equivalent binary search trees?)

• Another place to look might be for a reference that equivalence modulo an associative operator is decidable, as this amounts to the same thing. However, all the references that I'm aware of take this fact for granted. – Rob Simmons Oct 29 '10 at 13:38

As David Eppstein points out here, even finding the shortest path for binary trees is not known to be in P. In the comments to that answer he links to the best current bounds

• I'm accepting this answer since I learned something from it. However, I'd still love to find a reference for the structure theorem if anyone knows one. – Per Vognsen Sep 26 '10 at 11:58

An early paper that made this observation explicitly -- that rotations preserve inorder traversals -- is (in Figure 2 of) Sleator and Tarjan's 1983 Self-adjusting binary search trees. The move-to-root heuristic was studied in Allen and Munro's 1978 Self-Organizing Binary Search Trees paper.

• The interesting direction in Per's equivalence is not that rotations preserve in-order, but that you can travel in-between any two trees that have the same in-order using rotations. – Radu GRIGore Sep 23 '10 at 14:39
• Yes - that's why I included the move-to root. There's also another paper by Sleator, Tarjan, & Thurston (Rotation Distance, Triangulations, and Hyperbolic Geometry) computing the distances between any two trees, which I didn't include in my answer. I don't think that Per's observation appears in any one paper as is, but I'd love to be proven wrong. – Lev Reyzin Sep 23 '10 at 14:52
• Right, the easy direction is a necessary part of the proofs of correctness for AVL trees, 2-3 trees, etc. The opposite direction is deeper. It says that you don't need any structure-preserving transformations other than tree rotations for completeness. – Per Vognsen Sep 23 '10 at 14:55

Since you are interested in the structure of tree rotations, I think you may also be interested in the following paper, which shows that the rotation graph of binary trees of a fixed number of nodes has a Hamiltonian cycle. In fact Lucas showed that the cycle can be traversed with constant delay per tree. (A rotation can be performed in $O(1)$ time of course, but it is not obvious a priori that we should be able to decide which rotation to perform along the Hamiltonian cycle in $O(1)$ time.) Naturally, you may also be interested in the references within.

Joan M. Lucas, The rotation graph of binary trees is Hamiltonian, Journal of Algorithms, Volume 8, Issue 4, December 1987, Pages 503-535, ISSN 0196-6774, DOI: 10.1016/0196-6774(87)90048-4.

A simpler proof, also constructive, of the simpler fact that a Hamiltonian path exists in the rotation graph can be found in this later paper coauthored by Lucas and her collaborators.

Lucas J. M., Vanbaronaigien D. R., Ruskey F., On Rotations and the Generation of Binary Trees, Journal of Algorithms, Volume 15, Issue 3, November 1993, Pages 343-366, ISSN 0196-6774, DOI: 10.1006/jagm.1993.1045.

A simpler proof ,also constructive ,of the simpler fact that a Hamiltonian path exits in the rotation graph can be found in this latter .

• Your answer seems incomplete? – Jeremy Jun 21 '13 at 13:34