Data structure-Self-balancing binary search tree (AVL) detailed

Source: Internet
Author: User

Introduced:

In computer science, AVL trees are the first to invent self-balancing binary search trees.

The height of the two subtrees of any node in the AVL tree is the maximum difference of one, so it is also called a height-balanced tree.

Find, insert, and delete are O (log n) in both the average and worst case scenarios. Additions and deletions may require one or more tree rotations to rebalance the tree. The AVL tree was named after its inventor, g.m Adelson-velsky and E.M Landis, who published it in the 1962 paper "An algorithm for the organization of Information".

Features: The AVL tree is essentially a binary search tree, characterized by:

1. itself is first a binary search tree.

2. With equilibrium condition: the absolute value (balance factor) of the difference of the height of the left and right sub-tree of each node is up to 1.

In other words, the AVL tree is essentially a two-fork lookup tree with a balanced function (binary sort tree, binary search tree). Node count: AVL tree with height h, node N up to 2^h? 1; N (h) =n (H. 1) +n (H. 2) + 1.

The minimum number of nodes n if the Fibonacci sequence can be used to prove the mathematical inductive method:
That
N (0) = 0 (indicates the total number of nodes with an AVL Tree height of 0)
N (1) = 1 (indicates the total number of nodes with an AVL Tree height of 1)
N (2) = 2 (indicates the total number of nodes with an AVL Tree height of 2)
N (h) =n (H. 1) +n (H. 2) + 1 (total number of nodes that indicate the AVL Tree height is h)

The balance factor of a node is the height of its left subtree minus the height of its right sub-tree. Nodes with a balance factor of 1, 0, or-1 are considered to be balanced. Nodes with a balance factor of 2 or 2 are considered unbalanced and need to rebalance the tree. The balance factor can be stored directly in each node or calculated from the height of the subtree that may be stored in the node.

Operation:

Suppose that the pointer to the root node of the least-balanced tree is a (that is, a is the ancestor node closest to the insertion point and the absolute balance factor is more than 1) because the node is inserted on the binary sort trees.

After the loss of balance after the conduct of the law can be summed up in the following four cases: one-way right-balance processing LL:

Due to the insertion of nodes in the left subtree of the Zogen node of the *a, the balance factor of *a is increased from 1 to 2, resulting in the loss of balance of the subtree rooted in the *a, and a right rotation operation is required;

Unidirectional left-hand balance processing RR:

Because the node is inserted on the right subtree of the right subtree node of *a, the balance factor of *a is changed from 1 to 2, resulting in the loss of balance of the subtree with the root of the *a, and a left rotation operation is required;

Bidirectional rotation (first left rear right) balances processing LR:

Due to the insertion of nodes in the right subtree of the Zogen node of the *a, the balance factor of the *a is increased from 1 to 2, resulting in the loss of balance of the subtree with the root of the *a, two rotations (first left-handed and right-handed) operations are required.

Bidirectional rotation (first right rear left) balanced processing RL:

Due to the insertion of a node in the left subtree of the right subtree node of the *a, the balance factor of the *a is changed from 1 to 2, resulting in the loss of balance of the subtree with the root of the *a, two rotations (first right and left left) are required.

Illustration of Binary tree operation: 1. Left-handed

2. Right-handed:

The insert operation for AVL: (Evil Face) 1. Single direction:

Right-click on the base of node 5:

Summarize:

For this One direction, only one operation is required, this is the case where the child is on the left, for the child is on the right side, (5,8,10), to 5 as the benchmark, a left-handed

2. The parties to:

Not a direction, do two operations, first converted to 1 case, again rotation, showing 3 as the benchmark, left, 5 as the benchmark right-handed, first to the right

Summarize:

For this not a direction of the situation, requires two operations, first converted to 1 cases, the symmetry is the first right after the left.

Delete operation:

Deleting from the AVL tree can be done by rotating the node to be deleted into a leaf node and then pruning the leaf node directly. Because a maximum of log n nodes are rotated during the rotation of a leaf node, and each AVL rotation consumes a constant amount of time, the removal process consumes an O (log n) time overall.

Find:

Looking in the AVL tree is exactly the same as in general BST, so it consumes O (log n) time because the AVL tree is always balanced. No special preparation is required, and the structure of the tree is not changed by the query. (This is in contrast to the extension tree lookup, which changes the tree structure because of the lookup.) )

Time Complexity Analysis:

In tree, binary tree, binary search tree, a two-fork tree with n nodes, with a minimum depth of log (n) and a maximum depth of n. such as the following two two fork trees:

Two-fork tree with a depth of n

Two-tree with a depth of log (n)

Analysis:

The smaller the depth of the binary search tree, the less time is required for the search to operate. A two-fork search tree with a depth of log (n), and the time complexity of the search algorithm is also log (n). However, the insert and delete operations that we have implemented in the binary search tree do not allow the depth of log (n) to be maintained. If we insert the node in the order of 8,7,6,5,4,3,2,1, it is a two-fork tree with a depth of N. Then, the time complexity of the search algorithm is n.

My QR code is as follows, welcome to exchange discussion

You are welcome to pay attention to the "It question summary" subscription number. Every day to push the classic face test and interview tips, are dry! The QR code of the subscription number is as follows:

Reference

Http://baike.baidu.com/view/671745.htm
Http://www.cnblogs.com/vamei/archive/2013/03/21/2964092.html

Data structure-Self-balancing binary search tree (AVL) detailed

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.