"Dahua Data Structure" balance binary tree, the adjustment process of LeftBalance balance factor

Record the understanding of LeftBalance, the rotation operation function of the balanced binary tree during the process of learning "Dahua Data Structure".
Insert picture description here
The specific situation is as follows:
Insert picture description here
1. case LH:
Check the height of the left subtree L of T, and find that when the left subtree L is LH, because it is a recursive backtracking process, the LH of the left subtree L is due to the newly inserted node being inserted into Caused on the left subtree of L. But after inserting a new node, L is still balanced. The specific process is as follows: Insert picture description here
Therefore, after rotating, adjust the balance factor as
(*T)->bf = L->bf = EH
and then rotate T to the right.

2.case RH:
Check the height of the left subtree L of T and find that when the left subtree L is RH, because it is a recursive backtracking process, the RH of the left subtree L is due to the newly inserted node being inserted into the right child of L Caused on the tree. But after inserting a new node, L is still balanced. The specific process is as follows:
Insert picture description here
In order to adjust the balance factor, the height of the right subtree Lr of L must be classified and discussed at this time, that is, the 3 case
2.1 case LH in the second switch statement
inserts a new node to cause the height of the left subtree of Lr Greater than the height of the right subtree, but Lr is still balanced. The insertion process is as follows:
Insert picture description here
After rotating, the changes are as follows:
Insert picture description here
Therefore, the balance factor is modified as:
(*T)->bf = RH;
L->bf = EH;
Lr->bf = EH;

2.2
Inserting a new node in case EH causes the height of the left subtree of Lr to be equal to the height of the right subtree. In this case, the right subtree of L is originally empty, that is, Lr is the newly inserted node.
Assuming that the right subtree of L is not empty, that is, n is not equal to 0, the situation is as follows:
Insert picture description here
At this time, Lr must have a subtree with a height of n, because the balance factor of Lr must be guaranteed to be 0, so T is unbalanced before the new node is inserted Therefore, it contradicts the imbalance caused by the insertion of new nodes. Therefore, when the balance factor of Lr is EH, the right subtree of L should be empty, that is, Lr==A. The insertion process at this time is as follows:
![Insert picture description here](https://img-blog.csdnimg.cn/20200807111938834.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_
Therefore, the balance after the balance rotation is adjusted to:
(*T)->bf = L->bf = EH;
Lr->bf = EH;

2.3 case RH:
Inserting a new node causes the height of the right subtree of Lr to be greater than the height of the left subtree. The insertion process is as follows:
Insert picture description here
After the rotation, the changes are as follows:
Insert picture description here
Therefore, the balance factor is modified as:
(*T)->bf = EH;
L->bf = LH;
Lr->bf = EH;

Guess you like

Origin blog.csdn.net/weixin_40315481/article/details/107856484