AVL Tree of Data Structure

Source: Internet
Author: User

Note: This article is only for learning and communication. For more information, see the source!

In the previous blog, we have introduced the knowledge about the Binary Search Tree of the data structure. The binary search is proposed mainly to improve the efficiency of data search. The same element set can correspond to different binary search tree BST. The Binary Search Tree Structure depends on the element insertion sequence. At the same time, we also know that if we insert an ordered data set into the second search tree in sequence, the binary search tree degrades to a linear table, and the time complexity of the query is O (n ). To prevent this problem, the existence value of a balanced binary tree exists. The purpose of the balanced binary tree is to prevent the emergence of oblique Binary Trees, further improve the element search efficiency, and ensure that the time complexity of element search is O (logn). Obviously, A balanced binary tree is a responsible form of a binary search tree, because it is fair and equal to left and right.

Description: The balanced binary tree is a large range, not only indicates the AVL Tree, because the balance of the tree is not an absolute measurement standard. The general significance of "balance" is that no node is too deep (the depth is too large), and different balancing conditions correspond to different tree types, which also shows different efficiency. There are many types of balanced binary trees, such as AVL-tree, Rb-tree, and AA-tree.

Basic Definition

Difference between height and depth

Node depth(Depth) refers to the number of entries from the root to the N path of the node., Node heightHeight refers to the number of edges in the longest path of a leaf node from node n down.Tree depthIt refers to the maximum value in the depth of all leaf nodes,Tree HeightThe height of the root node. As shown in the preceding definition,Depth is accumulated layer by layer from the root node, while height is accumulated from the base to the top of the leaf node..

AVL Tree (a balanced binary tree) ----> proposed by Adelson-velskii and landias in 1962.

AVL is either an empty BST or a BST of the following nature:

(1) The depth difference between the left and right subtree of the root node cannot exceed 1;

(2) The left and right subtree of the root node are also AVL trees.

FAQ: Assume that the heel node of the tree is 0th layers. How many nodes are there for an AVL Tree with N layers?

If F (n) is set to N, aVL contains at least the number of nodes.F (n) = f (n-1) + f (n-2), WhereF (0) = 1, F (1) = 2, You can see:

F (0) = 1

F (1) = 2

F (2) = 1 + 2 + 1 = 4

F (3) = 2 + 4 + 1 = 7

F (4) = 4 + 7 + 1 = 12

F (5) = 7 + 12 + 1 = 20

...

Balance factor

Balance factor of nodes in the AVL TreeThe depth of the Left subtree of the node minus the depth of the right subtree, that is, depth (left)-depth (right ).

AVL basic implementation process

When constructing an AVL Tree, whenever a knot is inserted, first check whether the balance of the tree is damaged due to insertion. If yes, find the minimum imbalance subtree (This is the unbalanced subtree closest to the insertion point.), While saving the AVL Tree, adjust the direct connection relationship between each node and rotate it accordingly to call it the new AVL subtree.

Summary: The unbalanced node may only occur in the path from the newly inserted point to the root node.

Single rotation and double Rotation

When a new node is inserted in aVL, You need to perform single or double rotation on some nodes in aVL according to the actual situation. A single rotation indicates a clockwise or counterclockwise rotation operation, in double rotation, two single rotation operations are performed (first clockwise and then counterclockwise, or first counterclockwise and then clockwise ),Single RotationOccurs in ll-type insert and RR-type insert, whileDouble RotationThis occurs in LR-type insertion and RL-type insertion. The following describes four types of insert:

The followingOut of balanceAll referClosest to the insertion pointTheOut of balance.

Ll type: The insertion point is located in the left Tree of the left child who loses the Balance Point;

Rr type: The insertion point is located on the right tree of the right child who has lost the equilibrium point;

LR type: The insertion point is located on the right tree of the left child who loses the equilibrium point;

Rr type: The insertion point is located in the left Tree of the right child who has lost the Balance Point.

Adjustment Method for ll-type insert:Raise the left child who loses the balance point, and the right child who loses the balance point to the improvement point. The original right child of the improvement Point serves as the left child who loses the balance point,As shown in:


Adjustment Method for RR insert: Raise the right child who loses the balance point, and the left child who loses the balance point to the improvement point. The original left child of the improvement Point serves as the right child who loses the balance point,As shown in:


Adjustment Method for LR insertion: First adjust the RR type, and then make the LL type adjustment, in the following two steps:

(1) first, raise the right child of the left child who loses the equilibrium point to the left child who loses the equilibrium point, use the original left child of the elevation point as the right child of the descent point (a collision occurred at this time ).

(2) Use the loss point as the new drop point and the drop point as the right child of the improvement point, the original right child of the elevation point is used as the left child of the descent point (a collision occurred at this time ).As shown in:


Adjustment Method for RL insertion:

(1) first, raise the left child of the right child who has lost the balance point. The right child who has lost the balance point is the right child of the improvement point, use the original right child of the lifting point as the left child of the descent point (a collision occurred at this time ).

(2) Use the loss point as the new drop point, and the drop point as the left child of the improvement point, use the original left child of the elevation point as the right child of the descent point (a collision occurred at this time ).As shown in:


Example

The values 2, 5, 7, 1, 4, 3, and 6 are inserted into AVL sequentially. The specific process is as follows:



References:

[1] data structure (C ++) Wang Hongmei

[2] data structure and algorithm analysis-Second edition of the original book in C Language

[3] STL source code analysis Hou Jie

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.