Introduction to algorithms-a thorough understanding of the balanced binary tree (AVL Tree)

Source: Internet
Author: User

I. Introduction to balanced Binary Trees
A balanced binary tree is called a balanced binary tree. The height of the left and right Subtrees of each node is at most 1, which is strictly defined:
An empty tree is a balanced binary tree. If T is a non-empty Binary Tree, its left and right subtree are TL and TR, make HL and HR respectively the depth of left and right subtree. When and only when
1) TL and TR are balanced binary trees;
2) | HL-HR | ≤ 1;

Of course, the binary sorting tree has its own nature. Review:
1) Any node X in the binary sorting tree, and any node y (if any) in the left (right) subtree must have a keyword smaller (larger) than that of X.
2) In the binary sorting tree, each node keyword is unique.
3) The central sequence obtained by traversing the tree in the central order is an ascending order sequence.
See the following example:

Ii. Tree Rotation
When we insert or delete an AVL Tree, we modify it, which may violate the nature of the AVL Tree.

To maintain the nature of the AVL Tree, we can rotate the tree by modifying the corresponding node pointer structure in the tree to insert or delete nodes in the AVL Tree, the AVL tree can still maintain its unique nature.
Adelson-velskii and Landis propose a method to dynamically maintain the balance of the binary sorting tree. The basic idea is:
During the construction of a binary sorting tree, whenever a knot is inserted, first check whether the balance of the tree is damaged due to insertion. If the balance of the tree is damaged due to the insertion of nodes, the minimum imbalance subtree is found, and the connection relationship between nodes in the smallest imbalance subtree is adjusted to reach a new balance while the ordering tree is maintained.

The adjustment mentioned above is rotation.
Assume that the balanced node is a when the binary sorting tree is inserted, there may be four imbalance situations:
1) insert the left subtree of the Left son of a once.
2) insert the right subtree of the Left son of a once.
3) insert the left subtree of the right son of a once.
4) Insert the right subtree of the right son of a once.

Where 1 and 4 are symmetric about a, and 2 and 3 are also symmetric about.
In the case of 1 and 4 (left or right), you only need to perform a single rotation on the tree to complete the adjustment. In the case of 2 and 3 (left or right) you need to perform a double rotation to complete the process.

The following is illustrated in a diagram.

1) single rotation

The right is used as an example. For example, if the right subtree of the right son of K1 is inserted once, K1 is an unbalanced node.

Solution: K1's right son K2 is an axial left rotation. If y is not empty, connect Y to the right subtree of K1.


 
Example:
 
Code in introduction to algorithms:
Left-rotate (t, x)
1 y = x. Right // set Y
2 x. Right = Y. Left // turn y's left subtree into X's right subtree
3 if y. Left! = T. Nil
4 Y. Left. P = x
5 Y. P = x. P // link X's parent to Y
6 if X. P = T. Nil
7 T. Root = y
8 elseif x = x. P. Left
9 x. P. Left = y
10 else X. P. Right = y
11 Y. Left = x // put X on Y's left
12 x. P = y

2) Double Rotation
The right left is used as an example. 1 is an unbalanced node. Turning left directly cannot solve the problem.
Practice: The left subtree of K3 rotates right around K2, and the tree changes to right. It can be rotated left around K2 once.


Example:
 

3. AVL Tree Insertion

Insert in two steps:
1) Insert the new node into the tree position based on the insertion method of the binary sorting tree.
2) Search for the possible imbalance X. If the imbalance X is found, adjust the balance. Adjust the balance once at most.

Iv. AVL tree deletion
1) Delete the corresponding node according to the deletion method of the binary sorting tree.
2) Adjust along the parent node all the way up. Until root.

For the insertion and deletion methods of the binary sorting tree, see:
Introduction to algorithms-binary sorting tree

For example, set Keywords of a set of records to be inserted in the following order: 4, 5, 7, 2, 1, 3, 6, the process of generating and adjusting the binary balancing tree is shown in the figure (the number in the brackets is the node balancing factor ).
 


References:

Introduction to algorithms (Third edition)

Data Structure and algorithm analysis

Http://blog.csdn.net/sagadean/article/details/7081625

Http://www.cnblogs.com/fornever/archive/2011/11/15/2249492.html

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.