AVL Tree non-recursive insertion and deletion ideas

Source: Internet
Author: User

AVL TreeIs the first self-balancing Binary Search Tree. The height of the two Subtrees on any node in the AVL tree is the largest difference, so it is also called the height balancing tree. Search, insert, and delete are both O (LogN). Adding or deleting a tree may require one or more tree rotations to rebalance the tree. The AVL Tree is named after its inventor g.m. Adelson-velsky and E. M. Landis. They published it in an algorithm for the organization of information, a 1962 paper.

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

The AVL number may not be balanced after the node is inserted or deleted. In this case, the so-called "AVL" rotation is required several times. The table is represented in four columns. Each row indicates the operation to be performed in this case. In the case of left and right, you only need to perform one rotation operation. In the case of left and right, you need to perform two rotation operations.

Insert

You can insert a given value to the AVL Tree as if it were an unbalanced Binary Search Tree, and then fold back from the bottom to the root node, it is completed by rotating on all nodes that become unbalanced during insertion. Because there can be a maximum of 1.44 times of log on the way back to the root node.NEach AVL rotation takes a constant time, And the insert process consumes O (LogN) Time.

Delete

To delete a node from the AVL Tree, you can rotate the node to be deleted into a leaf node, and then directly cut the leaf node. Because a maximum of logs are generated during rotation to a leaf node.NNodes are rotated, and each AVL rotation consumes a constant amount of time. The deletion process consumes O (LogN) Time.

The above are cited from Wikipedia: http://zh.wikipedia.org/wiki/AVL%E6%A0%91

Insert ideas:

Inserting data into the AVL Tree is a bit searching process. Right or left is easier to implement. However, after insertion, the balance factor of the node passing by the path may change, resulting in the tree not being balanced. At this time, the unbalanced node needs to be rotated.

The problem is how to determine the unbalanced node, that is, where is the root node of the unbalanced tree?

It is known that a node is inserted at any position in the subtree of a node with a balance factor of 0. The equilibrium factor transformation of the node is only between-and 1, the root node balance factor of the unbalanced tree is not 0. starting from the root node, the balance factor of the node passing by the position will change.

What we need to do is to balance the root subtree. You need to know the following elements:

1. Point to the root node of the unbalanced tree and point to the parent of the root node (it can be saved by the variable R and P. When the bit is searched, the node with the balance factor not 0 is updated)

2. After the node is inserted, the balance factor of each node starts from the root (you can save the array to either left or right for each bit seeking, and then add or subtract the corresponding balance factor 1)

After updating the balance factor, determine the root balance factor. If it is not positive or negative 2, return directly. If it is, rotate according to the four conditions.

Note: The balance factor is updated after rotation in the left and right directions.

 

Delete idea:

To delete a node from the AVL Tree, you can rotate the node to be deleted into a leaf node, and then directly cut the leaf node. Then

Path:

1. Update the balance factor

2. Determine whether the balance factor of parent is affected. Exit if it is not affected.

3. Rotate and perform Step 2. If there is any impact, perform step 4.

4. Perform Step 1 on the parent (do not know where to draw the flowchart ^_^)

Another expression:

The deleted node is rotated down to a leaf node, that is, a leaf node is switched to the node to be deleted.

Definition: del_node, the leaf node to be switched, swap_node

It can be inferred that swap_node must be the maximum value of the Left subtree of del_node or the maximum value of the right subtree.

Take the right subtree of the switch del_node as an example:

1. Search for del_node, save the search path node, and store the direction in pnode [K], dir [k];

2. If del_node does not have left child, delete it directly. If yes, determine whether the left child node of right child is null. If it is null, you can also directly

Delete. If it is not null, find the minimum value of swap_node in the right subtree and replace it.

3. Update the balancing factor of the pnode [k] node from K and add or subtract it based on dir [K. (Balance factor = left subtree height-right subtree height; Dir = 0, indicating left turn, 1 indicates right turn)

Because the query direction affects the balancing factor of pnode [K], you can determine the direction based on the Dir:

Dir [k] = 0 (left direction:

Then, the balance factor of pnode [k] is reduced by 1 and updated:

1. if the value is-1 after update, the pnode [k] balance factor is 0. The change of the balance factor does not affect the balance factor of the parent node, so you can exit directly.

2. if the value is 0 after the update, the balance factor of pnode [k] is 1. After the update, the height of pnode [k] is reduced, therefore, it may affect the balancing factor of its parent.

Pnode [k-1] performs update balancing factor and similar judgment operations.

3. if the value is-2 after the update, the balance factor of pnode [k] is-1. After the update, the pnode [k] subtree is unbalanced and must be rotated first, then determine whether the height is changed. The key at this time is determined by

The balance factor RC of the right subtree.

If rc = 0: After rotation, the height does not change. You can exit directly.

If rc = 1: Right left, right left can be performed to balance, at this time the height of 1, The pnode [k-1] to determine the balance factor update.

If rc =-1: Is the right situation, you can perform left-hand, make it balanced, at this time the height of 1, The pnode [k-1] to determine the balance factor update.

When dir [k] = 1, the steps are similar and will not be repeated.

Too lazy, hey! AVL's difficulty lies in the deletion of the delete statement. After reading it for a long time, I finally found the key point: will the deletion of the leaf node affect the height of the parent, which in turn affects the balance factor of the upper layer.

I want to think more about it in the future, write something, level comparison, please give me some advice.

Hi, also to reprint please note the Source: http://www.cnblogs.com/chagmf/p/3878697.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.