AVL Tree rotation,
The balanced binary tree may be unbalanced during the insert operation. AVL is a self-balanced binary tree, which re-balances the binary tree by rotating unbalanced nodes, in addition, the average and worst case time complexity of search, insert, and delete operations is O (log n)
There are four types of AVL Tree rotation. Note that all rotation conditions are centered around the first node that makes the binary tree unbalanced.
1. LL type
A new node is inserted in the left subtree of a left child of a balanced binary tree, so that the node is not balanced. At this time, you only need to rotate the tree once to the right. The original Child B of A changes to the parent node, A changes to its right child, and the original Child B changes to the left child tree of, note that after the rotation, Brh is the left subtree of A (the image is not marked with A line between Brh)
2. RR type
A new node is inserted into the right child tree of a right child of a balanced binary tree, so that the node is not balanced. At this time, you only need to rotate the tree once to the left. The original A right Child B becomes the parent node, and A changes to its left child, the left subtree Blh of the original B will be changed to the right subtree of.
3. LR type
A new node is inserted in the right subtree of a left child of a balanced binary tree, so that the node is not balanced. At this time, it is required to rotate twice. Only one rotation can not balance the binary tree again ., After Node B rotates to the left according to the RR type, the binary tree still cannot maintain A balance in node A. At this time, it needs to rotate to the right again.
4. RL type
A new node is inserted in the left subtree of the right child of a balanced binary tree, so that the node is not balanced. At this time, you need to rotate twice, and the rotation direction is exactly the same as that of the LR type.