Performing an insert operation can be unbalanced when balancing a binary tree. AVL This tree is a self-balancing binary tree that balances the two forks once again. And the Find, insert, and delete operations are O (log n) in the average and worst case time complexity
There are four scenarios in which the AVL tree rotates in a common way. Note that all rotations are carried out 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. You just need to rotate the tree to the right one time and you can see it. The left child B of the original a becomes the parent node, a becomes its right child, and the right sub-tree of the original B becomes the left subtree of a, note that the BRH is a Zuozi after rotation (forget to mark the line between a and brh in the picture)
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. You just need to rotate the tree to the left once. As seen, the original a right child B becomes the parent node. A becomes its left child. The BLH of the left subtree of the original B will become the right subtree of a.
3. LR type
A new node is inserted on the right subtree of the left child of a certain node of the balanced binary tree. Makes the node no longer balanced. At this point, you need to rotate two times, only one rotation is not to make the two fork tree again balance.
As seen, after the B-node rotates to the left once in accordance with the RR type, the binary tree still cannot maintain a balance in the A-node, 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.
Same. This requires a rotation of two times. The direction of rotation is just the opposite of the LR type.
Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.
AVL Rotation Tree