Balanced binary tree (note LR and RL)

One: the concept of balanced binary tree

Balanced binary tree, also known as AVL tree, is a special binary sort tree, and the absolute value of the difference between the height of the left and right subtrees does not exceed 1.

Definition: a balanced binary tree or an empty tree, or a binary sort tree with the following properties:

(1) The absolute value of the difference between the depth of the left and right subtrees does not exceed 1;

(2) The left and right subtrees are still balanced binary trees.

Balance factor BF = left subtree depth-right subtree depth.

The balance factor of each node of the balanced binary tree can only be 1, 0, -1. If its absolute value exceeds 1, the binary sort tree is unbalanced.

 

Two, balance the binary tree adjustment

  If a new node is inserted into the balanced binary tree, the balance of the balanced binary tree is destroyed. First, find the smallest subtree that loses balance after inserting a new node, and then adjust this subtree to make it a new balanced subtree. When the smallest subtree that is out of balance is adjusted to a balanced subtree, there is no need to adjust all other unbalanced subtrees, and the entire binary sort tree becomes a balanced binary tree again.

  The smallest subtree out of balance refers to the subtree whose root is the node closest to the inserted node and whose absolute value of the balance factor is greater than 1. Assuming that A is used to represent the root node of the smallest subtree that is out of balance, the operation of adjusting the subtree can be summarized into the following four situations.

(1) LL type balance rotation method

  Since node F is inserted into the left subtree of A's left child B, the balance factor of A is increased from 1 to 2, and the balance is lost. Therefore, a clockwise rotation operation is required. That is, A's left child B rotates to the upper right instead of A as the root node, and A rotates to the lower right to become the root node of B's ​​right subtree. The original right subtree of B becomes the left subtree of A.

 

(2) RR type balance rotation method

  Since the node F is inserted into the right subtree of A's right child C, the balance factor of A is reduced from -1 to -2 and the balance is lost. Therefore, a counterclockwise rotation operation is required. That is, A's right child C rotates to the upper left instead of A as the root node, and A rotates to the lower left to become the root node of the left subtree of C. The original left subtree of C becomes the right subtree of A.

 

(3) LR type balance rotation method

  Since the node F is inserted into the right child of A's left child B, the balance factor of A is increased from 1 to 2, and the balance is lost. Therefore, two rotation operations are required (first counterclockwise, then clockwise). That is, first rotate the root node D of the left child of node A to the right subtree of node B to the position of node B, and then rotate the node D to the position of node A. That is, make it LL type first, and then process it as LL type.

 

As shown in the figure, first adjust the circle part to a balanced tree, and then connect it to the left subtree of A with the root node. At this time, it becomes the LL type, and then the LL type is processed into a balanced type.

(4) RL type balance rotation method

  Since the node F is inserted into the left subtree of A's right child C, the balance factor of A is reduced from -1 to -2 and the balance is lost. Therefore, it needs to perform two rotation operations (first clockwise, then counterclockwise), that is, first rotate the root node D of the left subtree of the right child of A node to the position of the C node, and then The D node rotates to the upper left and rises to the position of the A node. That is, make it the RR type first, and then treat it as the RR type.

 

As shown in the figure, first adjust the circle part to a balanced tree, and then connect it to the left subtree of A with the root node. At this time, it becomes the RR type, and then the RR type is processed into the balanced type.

 

Memory: LL is clockwise rotation, RR is counterclockwise, LR is counterclockwise first and then clockwise, RL is first clockwise and then counterclockwise (remember L is clockwise, R is counterclockwise, and double rotation is from back to front read)

Three: How to determine whether to perform a single rotation or a double rotation (when do you need a single rotation, and when do you need more rotation?)

1.0) The height imbalance requires that the height difference between the two subtrees at point α is 2, so the available height imbalance may occur in the following four situations:
① Insert the left subtree of the left son of α once.
② Perform an insertion into the right subtree of the left son of α.
③ Perform an insertion into the left subtree of the right son of α.
④ Perform an insertion into the right subtree of the right son of α.
Example (the yellow dot is the α point):

1.1) Single rotation: The insertion point is not between the root of the tree that does not meet the AVL condition and the corresponding child node of the tree root; (cases ①, ③ namely left-left, right-right)
1.2 ) Double rotation: the insertion point is between the tree root that does not meet the AVL condition and the corresponding child node of the tree root; (case ②, ④ namely left-right, right-left)

Guess you like

Origin blog.csdn.net/qie_wei/article/details/103479713