定义:
- 左子树与右子树的高度之差的绝对值小于等于1
- 左右子树也是平衡二叉树
a.平衡因子定义: 节点的左子树与右子树的深度之差**
例如:
平衡树的平衡方法:
(1)LL型
B = A->lchild;
A->lchild = B->rchild;
B->rchild = A;
A->bf = 0;
B->bf = 0;
if ( FA == NULL )
{
*root = B;
}
else if (A == FA->lchild )
{
FA->lchild = B;
}
else
{
FA->rchild = B;
}
(2)LR型
B = A->lchild;
PAVLNode C = B->rchild;
B->lchild = C->lchild;
A->lchild = C->rchild;
C->lchild = B;
C->rchild = A;
if (key > C->key)
{
A->bf = 0;
B->bf = 1;
C->bf = 0;
}
if (key < C->key)
{
A->bf = -1;
B->bf = 0;
C->bf = 0;
}
if (FA == NULL)
{
*root = C;
}
else if (FA->lchild == A)
{
FA->lchild = C;
}