# For AVL trees, how do we know if a RL or a LR rotation is needed?

Suppose I am trying to construct a simple AVL tree:

Upon inserting 'B' the tree becomes imbalanced. How do I exactly know that I need a LR or RL rotation without making any guesses?

From what I heard is that if you insert at right side of tree and it becomes imbalanced then you need a LR rotation and vice-versa. I want to make sure that it is true.

• What have you tried? Where did you get stuck? Have you seen en.wikipedia.org/wiki/Tree_rotation ? All cases are there.
– Evil
Feb 16, 2017 at 0:06
• You can refer 'cise.ufl.edu/~nemo/cop3530/AVL-Tree-Rotations.pdf' just see to it that the traversal of the tree before and after the rotation remains same. Feb 16, 2017 at 5:00
• There's no LR and RL cases there. Just tell me how do I know if the tree is right-left or left-right imbalanced. Thanks. Feb 16, 2017 at 7:50

In this case the tree is right-left unbalanced. That's because when you insert $B$ it causes the root node to become unbalanced. You can tell its right-left unbalanced from the path that goes from the newly inserted node to the root node i.e. $B$ is the right child of $A$ and $A$ is the left child of $C$. Therefore, it needs a left-right rotation.