在上一篇文章中,我们对map/multimap/set/multiset进行了简单的认识,这几个容器有个共同点是:其底层都是按照二叉搜索树来实现的,但是二叉搜索树有其自身的缺陷,假如往树中插入的元素有序或者接近有序,二叉搜索树就会退化成单支树,时间复杂度会退化成O(N),因此map、set等关联式容器的底层结构是对二叉树进行了平衡处理,即采用平衡树来实现。
一、AVL树的概念
二叉搜索树虽可以缩短查找的效率,但如果数据有序或接近有序二叉搜索树将退化为单支树,查找元素相当于在顺序表中搜索元素,效率低下。因此,两位俄罗斯的数学家G.M.Adelson-Velskii和E.M.Landis在1962年发明了一种解决上述问题的方法:当向二叉搜索树中插入新结点后,如果能保证每个结点的左右子树高度之差的绝对值不超过1(需要对树中的结点进行调整),即可降低树的高度,从而减少平均搜索长度。
一棵AVL树或者是空树,或者是具有以下性质的二叉搜索树:
(1)它的左右子树都是AVL树
(2)左右子树高度之差(简称平衡因子)的绝对值不超过1(-1/0/1)
如果一棵二叉搜索树是高度平衡的,它就是AVL树。如果它有n个结点,其高度可保持在log2 n,搜索时间复杂度O(log_2 n)。
二、AVL树节点的定义
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树的插入
AVL树就是在二叉搜索树的基础上引入了平衡因子,因此AVL树也可以看成是二叉搜索树。那么AVL树的插入过程可以分为两步:
(1)按照二叉搜索树的方式插入新节点
(2)调整节点的平衡因子
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的平衡因子违反平衡树的性质,需要对其进行旋转处理
// 在AVL树中插入值为data的节点
bool Insert(const T& data)
{
//1. 先按照二叉搜索树的规则将节点插入到AVL树中
if (_pRoot == nullptr)
{
_pRoot = new Node(data);
return true;
}
Node* parent = nullptr;
Node* cur = _pRoot;
while (cur)
{
if (cur->_data < data)
{
parent = cur;
cur = cur->_pRight;
}
else if (cur->_data > data)
{
parent = cur;
cur = cur->_pLeft;
}
else
{
return false;
}
}
cur = new Node(data);
if (parent->_data < data)
{
parent->_pRight = cur;
}
else
{
parent->_pLeft = cur;
}
cur->_pParent = parent;
//更新平衡因子
while (parent)
{
if (cur == parent->_pLeft)
{
parent->_bf--;
}
else
{
parent->_bf++;
}
if (parent->_bf == 0)
{
break;
}
else if (parent->_bf == 1 || parent->_bf == -1)
{
cur = parent;
parent = parent->_pParent;
}
else if (parent->_bf == 2 || parent->_bf == -2)//出问题了,旋转
{
if (parent->_bf == -2 && cur->_bf == -1)
{
//右单旋
RotateR(parent);
}
else if (parent->_bf == 2 && cur->_bf == 1)
{
//左单旋
RotateL(parent);
}
else if (parent->_bf == -2 && cur->_bf == 1)
{
RotateLR(parent);
}
else if (parent->_bf == 2 && cur->_bf == -1)
{
RotateRL(parent);
}
break;
}
}
return true;
}
四、AVL树的旋转
如果在一棵原本是平衡的AVL树中插入一个新节点,可能造成不平衡,此时必须调整树的结构,使之平衡化。根据节点插入位置的不同,AVL树的旋转分为四种:
1. 新节点插入较高左子树的左侧---左左:右单旋
上图在插入前,AVL树是平衡的,新节点插入到30的左子树(注意:此处不是左孩子)中,30左子树增加了一层,导致以60为根的二叉树不平衡,要让60平衡,只能将60左子树的高度减少一层,右子树增加一层,即将左子树往上提,这样60转下来,因为60比30大,只能将其放在30的右子树,而如果30有右子树,右子树根的值一定大于30,小于60,只能将其放在60的左子树,旋转完成后,更新节点的平衡因子即可。在旋转过程中,有以下几种情况需要考虑:
(1)30节点的右孩子可能存在,也可能不存在
(2)60可能是根节点,也可能是子树
如果是根节点,旋转完成后,要更新根节点
如果是子树,可能是某个节点的左子树,也可能是右子树
// 右单旋
void RotateR(Node* pParent)
{
Node* subl = pParent->_pLeft;
Node* sublr = subl->_pRight;
pParent->_pLeft = sublr;
if (sublr)
{
sublr->_pParent = pParent;
}
subl->_pRight = pParent;
Node* ppNode = pParent->_pParent;
pParent->_pParent = subl;
if (pParent == _pRoot)
{
_pRoot = subl;
subl->_pParent = nullptr;
}
else
{
if (ppNode->_pLeft == pParent)
{
ppNode->_pLeft = subl;
}
else
{
ppNode->_pRight = subl;
}
subl->_pParent = ppNode;
}
pParent->_bf = subl->_bf = 0;
}
2. 新节点插入较高右子树的右侧---右右:左单旋
与右单旋相似
// 左单旋
void RotateL(Node* pParent)
{
Node* subr = pParent->_pRight;
Node* subrl = subr->_pLeft;
pParent->_pRight = subrl;
if (subrl)
{
subrl->_pParent = pParent;
}
subr->_pLeft = pParent;
Node* ppNode = pParent->_pParent;
pParent->_pParent = subr;
if (pParent == _pRoot)
{
_pRoot = subr;
subr->_pParent = nullptr;
}
else
{
if (ppNode->_pRight == pParent)
{
ppNode->_pRight = subr;
}
else
{
ppNode->_pLeft = subr;
}
subr->_pParent = ppNode;
}
pParent->_bf = subr->_bf = 0;
}
3. 新节点插入较高左子树的右侧---左右:先左单旋再右单旋
将双旋变成单旋后再旋转,即:先对30进行左单旋,然后再对90进行右单旋,旋转完成后再考虑平衡因子的更新。
// 左右双旋
void RotateLR(Node* pParent)
{
Node* subl = pParent->_pLeft;
Node* sublr = subl->_pRight;
int bf = sublr->_bf;
RotateL(subl);
RotateR(pParent);
sublr->_bf = 0;
if (bf == -1)
{
subl->_bf = 0;
pParent->_bf = 1;
}
else if (bf == 1)
{
subl->_bf = -1;
pParent->_bf = 0;
}
else
{
subl->_bf = 0;
pParent->_bf = 0;
}
}
4. 新节点插入较高右子树的左侧---右左:先右单旋再左单旋
参考右左双旋
// 右左双旋
void RotateRL(Node* pParent)
{
Node* subr = pParent->_pRight;
Node* subrl = subr->_pLeft;
int bf = subrl->_bf;
RotateR(subr);
RotateL(pParent);
subrl->_bf = 0;
if (bf == 1)
{
subr->_bf = 0;
pParent->_bf = -1;
}
else if (bf == -1)
{
pParent->_bf = 0;
subr->_bf = 1;
}
else
{
pParent->_bf = 0;
subr->_bf = 0;
}
}
5.总结
假如以pParent为根的子树不平衡,即pParent的平衡因子为2或者-2,分以下情况考虑
(1)pParent的平衡因子为2,说明pParent的右子树高,设pParent的右子树的根为pSubR
当pSubR的平衡因子为1时,执行左单旋
当pSubR的平衡因子为-1时,执行右左双旋
(2)pParent的平衡因子为-2,说明pParent的左子树高,设pParent的左子树的根为pSubL
当pSubL的平衡因子为-1是,执行右单旋
当pSubL的平衡因子为1时,执行左右双旋
旋转完成后,原pParent为根的子树个高度降低,已经平衡,不需要再向上更新。
五、AVL树的验证
AVL树是在二叉搜索树的基础上加入了平衡性的限制,因此要验证AVL树,可以分两步:
(1)验证其为二叉搜索树
如果中序遍历可得到一个有序的序列,就说明为二叉搜索树
(2)验证其为平衡树
每个节点子树高度差的绝对值不超过1(注意节点中如果没有平衡因子)
节点的平衡因子是否计算正确
public:
// AVL树的验证
bool IsAVLTree()
{
return _IsAVLTree(_pRoot);
}
private:
// 根据AVL树的概念验证pRoot是否为有效的AVL树
bool _IsAVLTree(Node* pRoot)
{
if (pRoot == nullptr)
{
return true;
}
int left = _Height(pRoot->_pLeft);
int right = _Height(pRoot->_pRight);
return (abs(right - left) < 2) && _IsAVLTree(pRoot->_pLeft) && _IsAVLTree(pRoot->_pRight);
}
size_t _Height(Node* pRoot)
{
if (pRoot == nullptr)
{
return 0;
}
int left = _Height(pRoot->_pLeft) + 1;
int right = _Height(pRoot->_pRight) + 1;
return left > right ? left : right;
}
六、中序遍历
public:
void Inorder()
{
_Inorder(_pRoot);
cout << endl;
}
private:
void _Inorder(Node* pRoot)
{
if (pRoot == nullptr)
{
return;
}
_Inorder(pRoot->_pLeft);
cout << pRoot->_data << " ";
_Inorder(pRoot->_pRight);
}
七、全部代码
#pragma once
#include <assert.h>
#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)
{}
// 在AVL树中插入值为data的节点
bool Insert(const T& data)
{
if (_pRoot == nullptr)
{
_pRoot = new Node(data);
return true;
}
Node* parent = nullptr;
Node* cur = _pRoot;
while (cur)
{
if (cur->_data < data)
{
parent = cur;
cur = cur->_pRight;
}
else if (cur->_data > data)
{
parent = cur;
cur = cur->_pLeft;
}
else
{
return false;
}
}
cur = new Node(data);
if (parent->_data < data)
{
parent->_pRight = cur;
}
else
{
parent->_pLeft = cur;
}
cur->_pParent = parent;
//更新平衡因子
while (parent)
{
if (cur == parent->_pLeft)
{
parent->_bf--;
}
else
{
parent->_bf++;
}
if (parent->_bf == 0)
{
break;
}
else if (parent->_bf == 1 || parent->_bf == -1)
{
cur = parent;
parent = parent->_pParent;
}
else if (parent->_bf == 2 || parent->_bf == -2)//出问题了,旋转
{
if (parent->_bf == -2 && cur->_bf == -1)
{
//右单旋
RotateR(parent);
}
else if (parent->_bf == 2 && cur->_bf == 1)
{
//左单旋
RotateL(parent);
}
else if (parent->_bf == -2 && cur->_bf == 1)
{
RotateLR(parent);
}
else if (parent->_bf == 2 && cur->_bf == -1)
{
RotateRL(parent);
}
break;
}
}
return true;
}
// AVL树的验证
bool IsAVLTree()
{
return _IsAVLTree(_pRoot);
}
void Inorder()
{
_Inorder(_pRoot);
cout << endl;
}
private:
// 根据AVL树的概念验证pRoot是否为有效的AVL树
bool _IsAVLTree(Node* pRoot)
{
if (pRoot == nullptr)
{
return true;
}
int left = _Height(pRoot->_pLeft);
int right = _Height(pRoot->_pRight);
return (abs(right - left) < 2) && _IsAVLTree(pRoot->_pLeft) && _IsAVLTree(pRoot->_pRight);
}
size_t _Height(Node* pRoot)
{
if (pRoot == nullptr)
{
return 0;
}
int left = _Height(pRoot->_pLeft) + 1;
int right = _Height(pRoot->_pRight) + 1;
return left > right ? left : right;
}
void _Inorder(Node* pRoot)
{
if (pRoot == nullptr)
{
return;
}
_Inorder(pRoot->_pLeft);
cout << pRoot->_data << " ";
_Inorder(pRoot->_pRight);
}
// 右单旋
void RotateR(Node* pParent)
{
Node* subl = pParent->_pLeft;
Node* sublr = subl->_pRight;
pParent->_pLeft = sublr;
if (sublr)
{
sublr->_pParent = pParent;
}
subl->_pRight = pParent;
Node* ppNode = pParent->_pParent;
pParent->_pParent = subl;
if (pParent == _pRoot)
{
_pRoot = subl;
subl->_pParent = nullptr;
}
else
{
if (ppNode->_pLeft == pParent)
{
ppNode->_pLeft = subl;
}
else
{
ppNode->_pRight = subl;
}
subl->_pParent = ppNode;
}
pParent->_bf = subl->_bf = 0;
}
// 左单旋
void RotateL(Node* pParent)
{
Node* subr = pParent->_pRight;
Node* subrl = subr->_pLeft;
pParent->_pRight = subrl;
if (subrl)
{
subrl->_pParent = pParent;
}
subr->_pLeft = pParent;
Node* ppNode = pParent->_pParent;
pParent->_pParent = subr;
if (pParent == _pRoot)
{
_pRoot = subr;
subr->_pParent = nullptr;
}
else
{
if (ppNode->_pRight == pParent)
{
ppNode->_pRight = subr;
}
else
{
ppNode->_pLeft = subr;
}
subr->_pParent = ppNode;
}
pParent->_bf = subr->_bf = 0;
}
// 右左双旋
void RotateRL(Node* pParent)
{
Node* subr = pParent->_pRight;
Node* subrl = subr->_pLeft;
int bf = subrl->_bf;
RotateR(subr);
RotateL(pParent);
subrl->_bf = 0;
if (bf == 1)
{
subr->_bf = 0;
pParent->_bf = -1;
}
else if (bf == -1)
{
pParent->_bf = 0;
subr->_bf = 1;
}
else
{
pParent->_bf = 0;
subr->_bf = 0;
}
}
// 左右双旋
void RotateLR(Node* pParent)
{
Node* subl = pParent->_pLeft;
Node* sublr = subl->_pRight;
int bf = sublr->_bf;
RotateL(subl);
RotateR(pParent);
sublr->_bf = 0;
if (bf == -1)
{
subl->_bf = 0;
pParent->_bf = 1;
}
else if (bf == 1)
{
subl->_bf = -1;
pParent->_bf = 0;
}
else
{
subl->_bf = 0;
pParent->_bf = 0;
}
}
private:
Node* _pRoot;
};
八、测试
#include"AVLtree.h"
#include <vector>
void TestAVLTree()
{
int a1[] = { 8, 3, 1, 10, 6, 4, 7, 14, 13 };
int a2[] = { 4, 2, 6, 1, 3, 5, 15, 7, 16, 14 };
cout << "=================test1==================" << endl;
AVLTree<int> t1;
for (auto e : a1)
{
t1.Insert({ e});
cout << "Insert:" << e << "->" << t1.IsAVLTree() << endl;
}
t1.Inorder();
cout << t1.IsAVLTree() << endl;
cout << "=================test1==================" << endl;
AVLTree<int> t2;
for (auto e : a2)
{
t1.Insert({ e });
cout << "Insert:" << e << "->" << t1.IsAVLTree() << endl;
}
t2.Inorder();
cout << t2.IsAVLTree() << endl;
}
int main()
{
TestAVLTree();
return 0;
}
九、AVL树的性能
AVL树是一棵绝对平衡的二叉搜索树,其要求每个节点的左右子树高度差的绝对值都不超过1,这样可以保证查询时高效的时间复杂度,即log2 N。但是如果要对AVL树做一些结构修改的操作,性能非常低下,比如:插入时要维护其绝对平衡,旋转的次数比较多,更差的是在删除时,有可能一直要让旋转持续到根的位置。因此:如果需要一种查询高效且有序的数据结构,而且数据的个数为静态的(即不会改变),可以考虑AVL树,但一个结构经常修改,就不太适合。