5

In the book Introduction To Algorithms - A Creative Approach, Question 4.24:

Let T1, and T2 be two arbitrary trees, each having n nodes. Prove that it is sufficient to apply at most 2n rotations to T1, so that it becomes equal to T2.

For binary search trees, I figured out an algorithm:

  1. Find the element which is equal to T2's root, let's call it target-root.

  2. Using AVL rotation strategy, rotate target-root to make it T1's new root.
    During this process, multiple rotations may be performed.

  3. For the left sub tree of T1 and T2, process them as above recursively.
    For the right sub tree of T1 and T2, process them as above recursively.

This algorithm, when in worst-case, runs in O(N^2).

I don't quite understand the phrase “arbitrary trees”, and I can't figure out how to make T1 equal to T2.

Could anyone help on this question?

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
Guocheng
  • 543
  • 3
  • 15
  • 1
    I don't know is there any simple answer. I think this paper is a good starting point. Daniel Sleator, Robert Tarjan and William Thurston showed that the rotation distance between any two n-node trees (for n ≥ 11) is at most 2n − 6, and that infinitely many pairs of trees are this far apart. http://www.ams.org/journals/jams/1988-01-03/S0894-0347-1988-0928904-4/home.html – amald Nov 24 '13 at 14:23
  • @amald thanks, I will read the paper – Guocheng Nov 25 '13 at 09:44

1 Answers1

1

From whatever i have got i can propose an algorithm that can solve this problem in O(N) rotations but could not get the exact upper bound but think you can build on this:-

Here is pseudo code for algorithm:-

 //Method to make T1 equivalent to T2

    alignTree(T1,T2) {

     if(length(T1)==1)
        return

     else {

        Node k = FindRoot(T1,T2)
        rotateAbout(k)
        align(T1.left,T2.left)
        align(T1.right,T2.right)
     }


    }

Suppose FindRoot finds the node of T1 which is considered to be root of new Tree which is equivalent tree. Suppose rotateAbout(K) makes appropriate rotation to root to get equivalent nodes on left and right subtrees of new tree. Then we can recursively solve for smaller sub-problems on smaller subtrees.

No of Rotations: As you can see in pseudo code the no of rotation is equivalent to pre-order traversal of tree which is O(N)

Note: You can always extend the above pseudo code for general tree and still get same complexity.

Vikram Bhat
  • 6,106
  • 3
  • 20
  • 19
  • I don't think rotateAbout(K) takes constant time. suppose T1 is a tree which is generated by inserting 10,9,8,7,6,5,4,3,2,1, and T2 is a tree which is generated by inserting 1,2,3,4,5,6,7,8,9,10. Then first we should rotatedAbout(1), it takes 9 steps if we use single rotation(AVL single rotation). – Guocheng Nov 25 '13 at 05:55
  • 1
    @Guocheng Here while aligning the two trees we do not match the keys of tree but only the structure of the two trees must be same so the root will always be the node which has same nodes on left and right as the second tree. Moreover we donot calculate the time complexity of algorithm here but only the no of rotations needed to align the tree. – Vikram Bhat Nov 25 '13 at 07:11
  • so how do we decide the new root node of T1 and how to rotate to make sure that sub trees have equal number nodes? – Guocheng Nov 25 '13 at 09:40
  • @Guocheng Thats not our problem here , u can also do it by brute force, we just need to find the amount of rotations needed to align two trees. For tight upper bound u need to find out the maximum rotations rotateAbout need to achieve that, Precisely we need to prove that it is at max 2 – Vikram Bhat Nov 25 '13 at 10:10