The rotation of AVL

Source: Internet
Author: User

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

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.