rotationavl-tree

AVL Rotation - Which node to rotate


I have read many sources about AVL trees, but did not find anyone addressing this issue: When AVL tree gets unbalanced, which node should be rotated first?

Assuming I have the tree:

    10
   /  \
   5   25
       / 
      20

and I'm trying to add 15, both the root and its child 25 will be unbalanced.

    10
   /  \
   5   25
       / 
      20
      /
     15

I could do a RR rotation (or single rotation) of 25, resulting in the following tree:

     10
    /  \
   5    20
        /\
      15  25

or a RL rotation (double rotation) about the root, creating the following tree:

    20
   /  \
  10   25
 /  \     
5   15   

I am confused about which rotation is the most suitable here and in similar cases.


Solution

  • The RR rotation is correct here. The rotation should be done as soon (as low) as the rule is broken. Which is for 25 here.

    The higher rotations first don't necessarily break the rule and secondly would become too complex although it doesn't seem so here at the first sight.