1. 二叉搜索树的缺陷
二叉搜索树虽可以缩短查找的效率,但如果数据有序或接近有序二叉搜索树将退化为单支树,查找元素相当于在顺序表中搜索元素,效率低下。因此,两位俄罗斯的数学家G.M.Adelson-Velskii和E.M.Landis在1962年发明了一种解决上述问题的方法:当向二叉搜索树中插入新结点后,如果能保证每个结点的左右子树高度之差的绝对值不超过1(需要对树中的结点进行调整),即可降低树的高度,从而减少平均搜索长度。
2. AVL树的概念
一棵AVL树或者是空树,或者是具有以下性质的二叉搜索树:(前提是一颗二叉搜索树)
- 它的左右子树都是AVL树
- 左右子树高度之差(简称平衡因子)的绝对值不超过1(-1 / 0 / 1)。
-
3. AVL树节点的定义
template<class T>
struct AVLTreeNode
{
AVLTreeNode(const T& data)
: _pLeft(nullptr)
, _pRight(nullptr)
, _pParent(nullptr)
, _data(data)
, _bf(0)
{}
AVLTreeNode<T>* _pLeft; // 该节点的左孩子
AVLTreeNode<T>* _pRight; // 该节点的右孩子
AVLTreeNode<T>* _pParent; // 该节点的双亲
T _data; //节点的值
int _bf; // 该节点的平衡因子
};
4. AVL 树的插入
AVL树就是在二叉搜索树的基础上引入了平衡因子,因此AVL树也可以看成是二叉搜索树。那么AVL树的插入
过程可以分为两步:
1.按照二叉搜索树的方式插入:
bool Insert(const T& data)
{
// 1. 先按照二叉搜索树的规则将节点插入到AVL树中
if (nullptr == _pRoot)
{
_pRoot = new Node(data);
return true;
}
// 按照二叉搜索树的性质找data在AVL中的插入位置
PNode pCur = _pRoot;
PNode pParent = nullptr;
while (pCur)
{
pParent = pCur;
if (data < pCur->_pLeft;
else if (data > pCur->_data)
pCur = pCur->_data;
else
return false; // 该节点在二叉搜索树中存在
}
pCur = new Node(data);
// 插入新节点:新节点一定插入在pParent的左侧或者右侧
if (data < pParent->_data)
pParent->_pLeft = pCur;
else
pParent->_pRight = pCur;
// 更新pCur的双亲节点
pCur->_pParent = pParent;
// 2. 新节点插入后,AVL树的平衡性可能会遭到破坏,
//此时就需要更新平衡因子,并检测是否破坏了AVL树的平衡性
//...
return true;
}
- 调整节点的平衡因子
由上图可以看出,如果给pParent(即节点的双亲)右边插入,只需给双亲的平衡因子+1即可,如果给双亲的左边插入,只需给双亲的平衡因子-1即可。(我们计算平衡因子的方式:右子树高度 减左子树的高度)。
双亲节点更新完成前:平衡因子可能是:-1,0,1.
双亲节点平衡因子更新后:可能是:-2,-1,0,1,2。
1. 如果pParent的平衡因子为0,说明插入之前pParent的平衡因子为正负1,插入后被调整成0,此时满足AVL树的性质,插入成功。
2.如果pParent的平衡因子为正负1,说明插入前pParent的平衡因子一定为0,插入后被更新成正负1, 此时以pParent为根的树的高度增加,需要继续向上更新。
3. 如果pParent的平衡因子为正负2,则pParent的平衡因子违反平衡树的性质,需要对其进行旋转处理。
bool Insert(const T& data) {
// 1. 先按照二叉搜索树的规则将节点插入到AVL树中
// ...
// 2. 新节点插入后,AVL树的平衡性可能会遭到破坏,
//此时就需要更新平衡因子,并检测是否破坏了AVL树
//的平衡性
/*
pCur插入后,pParent的平衡因子一定需要调整,在插
入之前,pParent的平衡因子分为三种情
况:-1,0, 1, 分以下两种情况:
1. 如果pCur插入到pParent的左侧,只需给
pParent的平衡因子-1即可
2. 如果pCur插入到pParent的右侧,只需给
pParent的平衡因子+1即可
此时:pParent的平衡因子可能有三种
情况:0,正负1, 正负2
1. 如果pParent的平衡因子为0,说明插入
之前pParent的平衡因子为正负1,插入后被
调整成0,此时满足AVL树的性质,插入成功
2. 如果pParent的平衡因子为正负1,说明
插入前pParent的平衡因子一定为0,插入
后被更新成正负1, 此时以pParent为根的树
的高度增加,需要继续向上更新
3. 如果pParent的平衡因子为正负2,
则pParent的平衡因子违反平衡树的性质,需要对
其进行旋转处理
*/
while (pParent)
{
// 更新双亲的平衡因子
if (pCur == pParent->_pLeft)
pParent->_bf--;
else
pParent->_bf++;
// 更新后检测双亲的平衡因子
if (0 == pParent->_bf)
break;
else if (1 == pParent->_bf || -1 == pParent->_bf)
{
// 插入前双亲的平衡因子是0,插入后双亲的平衡因子为
//为1 或者 -1 ,说明以双亲为根的二叉树
// 的高度增加了一层,因此需要继续向上调整
pCur = pParent;
pParent = pCur->_pParent;
}
else
{
// 双亲的平衡因子为正负2,违反了AVL树的平衡性,
//需要对以pParent为根的树进行旋转处理
if (2 == pParent->_bf)
{
// ...
}
else
{
// ...
}
}
}
return true;
}
5.AVL树的旋转
1.右单旋
旋转后的图形:
在旋转的过程中,会有一下几个问题需要注意:
void _RotateR(Node* pParent) {
// pSubL: pParent的左孩子
// pSubLR: pParent左孩子的右孩子,
Node* pSubL = pParent->_pLeft;
Node* pSubLR = pSubL->_pRight;
// 60的左孩子是50
pParent->_pLeft = pSubLR;
// 50存在,更新亲双亲
if (pSubLR)
pSubLR->_pParent = pParent;
// 60 作为 40的右孩子
pSubL->_pRight = pParent;
// 因为60可能是棵子树,因此在更新其双亲前必须先保存60的双亲
Node* pPParent = pParent->_pParent;
// 更新60的双亲
pParent->_pParent = pSubL;
// 更新40的双亲
pSubL->_pParent = pPParent;
// 如果60是根节点,根新指向根节点的指针
if (NULL == pPParent)
{
_pRoot = pSubL;
pSubL->_pParent = NULL;
}
else
{
// 如果60是子树,可能是其双亲的左子树,也可能是右子树
if (pPParent->_pLeft == pParent)
pPParent->_pLeft = pSubL;
else
pPParent->_pRight = pSubL;
}
// 根据调整后的结构更新部分节点的平衡因子
pParent->_bf = pSubL->_bf = 0;
}
2.左单旋
旋转后图形:
void RotateL(Node* pParent)
{
Node* pSubR = pParent->_pRight;
Node* pSubRL = pSubR->_pLeft;
pParent->_pRight = pSubRL;
if (pSubRL)
pSubRL->_pParent = pParent;
pSubR->_pLeft = pParent;
Node* pPParent = pParent->_pParent;
pParent->_pParent = pSubR;
pSubR->_pParent = pPParent;
if (nullptr == pPParent)
{
_pRoot = pSubR;
}
else
{
if (pParent == pPParent->_pLeft)
pPParent->_pLeft = pSubR;
else
pPParent->_pRight = pSubR;
}
pParent->_bf = pSubR->_bf = 0;
}
3.先左单旋,在右单旋
由上图可以看出,我们要插入的节点为75,如果直接以70那个节点进行旋转处理,不论怎么进行处理,都不可能满足AVL树,都有一个节点的平衡因子不满足。所以,我们先80这个节点进行左单旋,在以80这个节点进行右单旋。
注:代码:我们写代码直接调用单个旋转就行,但要注意传参时,判断是根据哪个节点旋转的。
4.先右单旋,在左单旋
由上图可以看出,我们要插入的节点为65,如果直接以60那个节点进行旋转处理,不论怎么进行处理,都不可能满足AVL树,都有一个节点的平衡因子不满足。所以,我们先60这个节点进行右单旋,在以60这个节点进行左单旋。
6.AVL树的印证
AVL树是在二叉搜索树的基础上加入了平衡性的限制,因此要验证AVL树,可以分两步:
- 验证其为二叉搜索树
如果中序遍历可得到一个有序的序列,就说明为二叉搜索树 - 验证其为平衡树
- 每个节点子树高度差的绝对值不超过1(注意节点中如果没有平衡因子)
- 节点的平衡因子是否计算正确
// 注意:此处需要验证两个内容:
// 1. 每个节点的平衡因子是否计算正确(通过节点的子树高度差来与当前节点的平衡因子比较)
// 2. 每个节点的平衡因子的绝对值是否超过1
int _Height(Node* pRoot)
{
if (nullptr == pRoot)
return 0;
// 计算pRoot左右子树的高度
int leftHeight = _Height(pRoot->_pLeft);
int rightHeight = _Height(pRoot->_pRight);
// 返回左右子树中较高的子树高度+1
return (leftHeight > rightHeight) ? (leftHeight + 1) : (rightHeight + 1);
}
bool _IsBalanceTree(Node* pRoot)
{
// 空树也是AVL树
if (nullptr == pRoot)
return true;
// 计算pRoot节点的平衡因子:即pRoot左右子树的高度差
int leftHeight = _Height(pRoot->_pLeft);
int rightHeight = _Height(pRoot->_pRight);
int diff = rightHeight - leftHeight;
// 如果计算出的平衡因子与pRoot的平衡因子不相等,或者
// pRoot平衡因子的绝对值超过1,则一定不是AVL树
if (diff != pRoot->_bf || (diff > 1 || diff < -1)) //与根的bf不等,说明插入计算错误。
return false;
// pRoot的左和右如果都是AVL树,则该树一定是AVL树
return _IsBalanceTree(pRoot->_pLeft) && _IsBalanceTree(pRoot->_pRight);
}
6.AVL树的性能
AVL树是一棵绝对平衡的二叉搜索树,其要求每个节点的左右子树高度差的绝对值都不超过1,这样可以保证查询时高效的时间复杂度,即(log N)但是如果要对AVL树做一些结构修改的操作,性能非常低下,比如:
插入时要维护其绝对平衡,旋转的次数比较多,更差的是在删除时,有可能一直要让旋转持续到根的位置。
因此:如果需要一种查询高效且有序的数据结构,而且数据的个数为静态的(即不会改变),可以考虑AVL树,但一个结构经常修改,就不太适合。
7. 代码实现
#include <iostream>
using namespace std;
template<class T>
struct AVLTreeNode
{
AVLTreeNode(const T& data = T())
: _pLeft(nullptr)
, _pRight(nullptr)
, _pParent(nullptr)
, _data(data)
, _bf(0)
{}
AVLTreeNode<T>* _pLeft;
AVLTreeNode<T>* _pRight;
AVLTreeNode<T>* _pParent;
T _data;
int _bf; // 节点的平衡因子
};
// AVL: 二叉搜索树 + 平衡因子的限制
template<class T>
class AVLTree
{
typedef AVLTreeNode<T> Node;
public:
AVLTree()
: _pRoot(nullptr)
{}
bool Insert(const T& data)
{
// 先按照二叉搜索树的方式进行插入
if (nullptr == _pRoot)
{
_pRoot = new Node(data);
return true;
}
// 按照二叉搜索树特性:找待插入节点在树中的位置
// 并保存其双亲
Node* pCur = _pRoot;
Node* pParent = nullptr;
while (pCur)
{
pParent = pCur;
if (data < pCur->_data)
pCur = pCur->_pLeft;
else if (data > pCur->_data)
pCur = pCur->_pRight;
else
return false;
}
// 插入新节点
pCur = new Node(data);
if (data < pParent->_data)
pParent->_pLeft = pCur;
else
pParent->_pRight = pCur;
pCur->_pParent = pParent;
while (pParent)
{
// 更新pParent的平衡因子
if (pCur == pParent->_pLeft)
pParent->_bf--;
else
pParent->_bf++;
if (0 == pParent->_bf)
break;
else if (-1 == pParent->_bf || 1 == pParent->_bf)
{
pCur = pParent;
pParent = pCur->_pParent;
}
else
{
// pParent->_bf: 2 || -2
// pParent的节点失去平衡
if (2 == pParent->_bf)
{
// 右子树高-->最后必须左单旋
if (1 == pCur->_bf)
RotateL(pParent);
else
RotateRL(pParent);
}
else
{
if (-1 == pCur->_bf)
RotateR(pParent);
else
RotateLR(pParent);
}
break;
}
}
return true;
}
void InOrder()
{
_InOrder(_pRoot);
}
Node* LeftMost()
{
if (nullptr == _pRoot)
return nullptr;
Node* pCur = _pRoot;
while (pCur->_pLeft)
pCur = pCur->_pLeft;
return pCur;
}
Node* RightMost()
{
if (nullptr == _pRoot)
return nullptr;
Node* pCur = _pRoot;
while (pCur->_pRight)
pCur = pCur->_pRight;
return pCur;
}
bool IsAVLTree()
{
return _IsAVLTree(_pRoot);
}
private:
bool _IsAVLTree(Node* pRoot)
{
if (nullptr == pRoot)
return true;
int leftHeight = _Height(pRoot->_pLeft);
int rightHeight = _Height(pRoot->_pRight);
if (abs(rightHeight - leftHeight) > 1 ||
rightHeight - leftHeight != pRoot->_bf)
return false;
return _IsAVLTree(pRoot->_pLeft) && _IsAVLTree(pRoot->_pRight);
}
size_t _Height(Node* pRoot)
{
if (nullptr == pRoot)
return 0;
size_t leftHeight = _Height(pRoot->_pLeft);
size_t rightHeight = _Height(pRoot->_pRight);
return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
}
void RotateR(Node* pParent)
{
Node* pSubL = pParent->_pLeft;
Node* pSubLR = pSubL->_pRight;
// 改变pParent和pSubL孩子的指向
pParent->_pLeft = pSubLR;
if (pSubLR)
pSubLR->_pParent = pParent;
pSubL->_pRight = pParent;
// 更新pParent和pSubL的双亲
Node* pPParent = pParent->_pParent;
pParent->_pParent = pSubL;
pSubL->_pParent = pPParent;
// 更新原pParent双亲的左||右指针域指向
if (nullptr == pPParent)
{
_pRoot = pSubL;
}
else
{
if (pParent == pPParent->_pLeft)
pPParent->_pLeft = pSubL;
else
pPParent->_pRight = pSubL;
}
pParent->_bf = pSubL->_bf = 0;
}
void RotateL(Node* pParent)
{
Node* pSubR = pParent->_pRight;
Node* pSubRL = pSubR->_pLeft;
pParent->_pRight = pSubRL;
if (pSubRL)
pSubRL->_pParent = pParent;
pSubR->_pLeft = pParent;
Node* pPParent = pParent->_pParent;
pParent->_pParent = pSubR;
pSubR->_pParent = pPParent;
if (nullptr == pPParent)
{
_pRoot = pSubR;
}
else
{
if (pParent == pPParent->_pLeft)
pPParent->_pLeft = pSubR;
else
pPParent->_pRight = pSubR;
}
pParent->_bf = pSubR->_bf = 0;
}
void RotateRL(Node* pParent)
{
Node* pSubR = pParent->_pRight;
Node* pSubRL = pSubR->_pLeft;
int bf = pSubRL->_bf;
RotateR(pParent->_pRight);
RotateL(pParent);
// 更新部分节点的平衡因子
if (bf == -1)
pSubR->_bf = 1;
else if (bf == 1)
pParent->_bf = -1;
}
void RotateLR(Node* pParent)
{
Node* pSubL = pParent->_pLeft;
Node* pSubLR = pSubL->_pRight;
int bf = pSubLR->_bf;
RotateL(pParent->_pLeft);
RotateR(pParent);
if (-1 == bf)
pParent->_bf = 1;
else if (1 == bf)
pSubL->_bf = -1;
}
void _InOrder(Node* pRoot)
{
if (pRoot)
{
_InOrder(pRoot->_pLeft);
cout << pRoot->_data << " ";
_InOrder(pRoot->_pRight);
}
}
private:
Node* _pRoot;
};