AVL Tree rotation,

Source: Internet
Author: User

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.

 




Related Article

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.