AVL Rotation Tree

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.