Balance
Obviously, the subtree after equilibrification should be balanced, and this is the principle, it is easy to know how to rotate in a variety of situations.
Private:
Void L_Balance (btNode
{
IF (P-> Right-> BF == 1) R_ROTATE (P-> Right);
L_ROTATE (P); current = P;
}
Void r_balance (btNode
{
IF (P-> LEFT-> BF == -1) l_rotate (p-> left);
R_ROTATE (P); current = P;
}
They are also symmetrical.
Modify the balance factor
This is the core of the entire AVL tree, the current textbook, is precisely because there is no truly understand how to modify the balance factor, the Switch ... case is full of flying. The variation of the balance factor occurs in the rotation - as this, the rotation can have a balanced effect - so the modified balance factor should be placed in the rotation operation instead of being placed in the balance. Let's take a look at the problem of the balance factor change brought about by possible rotation:
Left rotation (rotating P temporarily no change) right rotation (rotating P temporarily no change) Rotating P Rotation before t Rotation P Rotation before P Rotation T Rotation After P Rotate T -2 0 -1 1 2 0 1 -1 -2 -1 0 0 2 1 0 0 -2 -2 1 0 2 2 -1 0 -1 0 0 1 1 0 0 -1 -1-1 1 1 1 1 -1 -1 - 1 1 0 2 1 -1 0 -2
The initial occurrence of rotation is because BF == 2 or BF == - 2, the rotation of BF == 1 or BF == - 1 is for balanced requirements - the rotation P and T of the rotation p and T are different when the balanced number. The surface looks very messy, it seems that there is no law, it is actually.
For the left-hand-P-page right, the left subtree is turned from T, and it is apparent that the upper right subtree height of the P is reduced by at least one. The BF of T represents the height difference of the original T left and right subtots. If t-> bf <0, the height of the P's right child is also reduced | t-> bf |. The left subtree of t has a much p p on the original left subtree, obviously at least 1 of the left subtree height. After the balance factor of P, if P-> BF> 0, then the left subtree height of T is added to P-> BF.
Comprehensively, (p-> bf) - = t-> bf <0? T-> bf: 0; (t-> bf) = p-> bf> 0? P-> bf: 0 ;
For the right thread. - (p-> bf) - = t-> bf> 0? t-> bf: 0; - (t-> bf) = p-> bf <0? p-> bf: 0;
It can be seen that this is also symmetrical.
Complete AVL tree implementation
#define c_p current-> parent
#define c_root (C_P? ((c_p-> left == current)? c_p-> left: c_p-> right): root)
#include "bstree.h"
Template
Class Avltree: Public BStree
PUBLIC:
Bool Insert (Const T & DATA)
{
IF (! bstree
While (Current)
{
Current-> BF = (Current-> DATA> * P)? 1: -1;
IF (Current-> BF == -2) l_balance (c_root);
Else if (Current-> bf == 2) r_balance (c_root);
IF (! Current-> BF) Break;
P = & (current-> data); current = current-> parent;
}
Return True;
}
Bool Remove (Const T & DATA)
{
IF (! bstree
While (Current)
{
Current-> bf - = (Current-> DATA> * P)? 1: -1;
IF (Current-> BF == -2) l_balance (c_root);
Else if (Current-> bf == 2) r_balance (c_root);
IF (Current-> BF) Break;
P = & (current-> data); current = current-> parent;
}
Return True;
}
Private:
Void L_Balance (btNode
{
IF (P-> Right-> BF == 1) R_ROTATE (P-> Right);
L_ROTATE (P); current = P;
}
Void r_balance (btNode
{
IF (P-> LEFT-> BF == -1) l_rotate (p-> left);
R_ROTATE (P); current = P;
}
Void L_Rotate (btNode
{
BTNODE
T-> parent = p-> parent; p-> parent = t; p-> right = t-> left;
IF (t-> left) t-> left-> parent = p; t-> left = p;
(p-> bf) - = t-> bf <0? t-> bf: 0; (t-> bf) = p-> bf> 0? P-> bf: 0;
P = T;
}
Void R_Rotate (btNode
{
BTNODE
T-> Parent = P-> Parent; P-> Parent = T; P-> Left = T-> Right;
IF (t-> right) t-> right-> parent = p; t-> right = p; - (p-> bf) - = t-> bf> 0? t-> bf: 0; (t-> bf) = p-> bf <0? p-> bf: 0;
P = T;
}
}
Summary and revelation
The AVL tree is a balanced binary tree, which uses symmetrical rotation to maintain balance, which is also determined to be symmetrical for other operations. But because it is not perfect, the insertion and deletion of the external performance is not so symmetrical (once equilibrification is balanced at the time of inserting, the worst situation when deleting can be adjusted to Tree root O (logn), but their inherent nature should It is symmetrical, as given above - all operations are symmetrical.
Promote me carefully study the symmetry of insertion and deletion, which is for the confidue of the AVL tree operation. This reflects a person's philosophical cultivation. I don't want to talk about philosophy for a person's importance, just for those who think of Ma Zhe, Mao, and useless people.