Transfer from http://blog.csdn.net/gabriel1026/article/details/6311339
Balanced binary trees can be unbalanced when inserting operations, the AVL tree is a self-balancing two-fork tree that re-balances the two-fork tree by rotating an unbalanced node, and the Find, insert, and delete operations have an O (log n) in the average and worst case
There are four scenarios in which the AVL tree rotates, noting that all rotations revolve around the first node that makes the binary tree unbalanced.
1. LL type
Balanced binary tree A node in the left child's left subtree is inserted into a new node so that the node is no longer balanced. At this time only need to rotate the tree to the right once,, the original a left child B into the parent node, a to its right child, and the right sub-tree of the original B into a left subtree, note that after the rotation of BRH is a Zuozi (pictured in a in the brh between the line)
2. RR type
A new node is inserted into the right child tree of a node in a balanced binary tree, making the node no longer balanced. At this point only need to rotate the tree to the left once,, the original a right child B into a parent node, a to its left child, and the original B's left subtree BLH will become a right subtree.
3. LR type
Balanced binary tree A node in the left child's right subtree is inserted into a new node so that the node is no longer balanced. At this point, two rotations are required, and only one rotation is not able to balance the two-fork tree again. , the binary tree is still not balanced on the a node after the B node rotates to the left after the RR type, and then it needs to rotate to the right again.
4. RL Type
Balanced binary tree A node in the right child's left subtree is inserted into a new node so that the node is no longer balanced. Similarly, it is required to rotate two times and the direction of rotation is exactly the same as the LR type.
The rotation of AVL