Balance of Binary Search Trees-AVL Tree and tree rotation (illustration)
The query efficiency of the Binary Search Tree is high only when the balance is maintained.
It is not easy to balance the binary search tree. However, there are still some very classic ways to do this. The best method is to implement the binary search tree as the AVL Tree.
The AVL Tree is named after its inventor G.M. Adelson-Velsky and E. M. Landis. They published it in their 1962 paper "An algorithm for the organization of information. AVL is a special type of binary tree. Each node of AVL stores information out of its share: the balance factor of the node.
Balance factor of the node = height of the Left subtree-height of the right subtree
Insert and delete operations will lead to AVL Tree self-adjustment (self-balancing), so that the balance factor of all nodes is + 1,-1 or 0.
When the balance factor of the root node of the subtree is + 1, it is left-leaning (left-heavy ).
When the balance factor of the root node of the subtree is-1, it is right-heavy ).
The balance factor of the root node of A subtree represents the balance of the subtree.
To keep almost all sub-trees in balance, the AVL tree can basically maintain a balance on the whole.
The Basic Search and insert node operations of the AVL tree are the same as those of the binary tree. However, when a node is inserted into the AVL Tree, some additional work needs to be done. First, you must calculate the change to the balance factor caused by the insert operation. Second, if any balance factor is changed to +/-2, you must rebalance the tree from this node. This rebalancing process is called rotation.
AVL Tree Rotation
The rotation operation is used to rebalance a part of the tree. By rescheduling nodes, the relationship between nodes always keeps the left child node smaller than the parent node, and the parent node smaller than the right child node. This makes the tree still a binary search tree. After rotation, the equilibrium factors of all nodes in the rotating subtree are + 1,-1, or 0.
There are four types of AVL Tree rotation: LL (left-left) rotation, LR (left-right) rotation, RR (right-right) rotate and RL (right-left) rotate.
To facilitate understanding when to execute A rotation, set x to represent the node in the inserted AVL Tree, and set A to the ancestor closest to x and the absolute value of the equilibrium factor changed to 2. The following four solutions can be summarized:
LL Rotation
As shown in, when x is on the left Tree of the Left subtree of A, perform LL rotation.
Set left to the left subtree of A. To perform LL rotation, point the left pointer of A to the right subtree of left, and the right pointer of left to, point the pointer pointing to A to left.
After rotation, change the balance factors of a and left to 0. The balance factor of all other nodes does not change.
LR Rotation
When x is located in the right tree of the Left subtree of A, LR rotation is executed.
Set left to the left subnode of A, and set grandchild to the right subnode of left.
To perform LR rotation, point the right child node of left to the left child node of grandchild, the left child node of grandchild to left, and the left child node of A to the right child node of grandchild, next, point the right child node of grandchild to A, and then point the pointer pointing to A to grandchild.
After LR rotation is performed, the adjustment of the balance factor of the node depends on the original balance factor value of the grandchild node before the rotation.
If the original balance factor of the grandchild node is + 1, set the balance factor of A to-1 and the balance factor of left to 0.
If the original equilibrium factor of the grandchild node is 0, the equilibrium factors of A and left are set to 0.
If the original balance factor of the grandchild node is-1, set the balance factor of A to 0 and the balance factor of left to + 1.
In all cases, the new equilibrium factor of grandchild is 0. The balance factor of all other nodes has not changed.
RR Rotation
When x is located in the right tree of the Left subtree of A, RR rotation is performed.
RR rotation is symmetric with LL rotation.
Set the Right subnode of a to Right. To execute the RR rotation, point the right pointer of A to the left child node of right, and the left pointer of right to A. The original pointer to A is changed to the right.
After the rotation is completed, change the balance factors of a and left to 0. The balance factor of all other nodes has not changed.
RL Rotation
When x is located in the left Tree of the right subtree of A, RL rotation is executed.
RL rotation is symmetric with LR rotation.
Set the right subnode of a to right and the left subnode of right to grandchild. To execute RL rotation, point the left child node of the right node to the right child node of grandchild, point the right child node of grandchild to right, and point the right child node of A to the left child node of grandchild, point the left child node of grandchild to A, and then point the pointer pointing to A to grandchild.
After the RL rotation is executed, the adjusted balancing factor of the node depends on the original balancing factor of the grandchild node before the rotation. There are also three situations to consider:
If the original equilibrium factor value of grandchild is + 1, update the equilibrium factor of A to 0, and the right to-1;
If the original equilibrium factor value of grandchild is 0, the equilibrium factors of A and right are updated to 0;
If the original equilibrium factor value of grandchild is-1, update the equilibrium factor of A to + 1, and the right to 0;
In all cases, the new balance factor of grandchild is set to 0. The balance factor of all other nodes does not change.
This article permanently updates link: https://www.bkjia.com/Linux/2018-03/151314.htm