二叉搜索树虽然可以加快查找效率,但当数据有序或接近有序时,二叉搜索树将退化为单支树,查找元素相当于在顺序表中搜索元素,效率低下。因此,当向二叉搜索树中插入新结点后,如果能保证每个结点的左右子树高度之差的绝对值不超过1(需要对树中的结点进行调整),即可降低树的高度,从而减少平均搜索长度。
一颗AVL树或是空树,或是具有以下性质的二叉搜索树:
- 它的左右子树都是AVL数
- 它的左右子树高度之差(简称平衡因子)的绝对值不超过1可以取-1、0、1
如果一棵二叉搜索树是高度平衡的,那么它就是AVL树。如果它有N个结点,其高度可保持在O(logN),搜索树的时间复杂度为O(logN)。
AVL数的插入
AVL树是在二叉搜索树的基础上引入了平衡因子,因此AVL树也可以看成是二叉搜索树。AVL树的插入过程可以分两步:
1.按照二叉搜索树的方式插入新结点。
1)如果树的根为空,新创建一个结点作为树的根,返回true。
2)如果树不为空,从根结点开始比较将要插入结点的值key和当前结点值cur->_key的大小。如果key<cur->_key,到当前结点的左子树去找;如果key>cur->_key,到当前结点的右子树去找;如果key==cur->_key,说明该树中有和新结点值相同的结点,就不插入了,返回false。按照此思路循环查找,直到当前结点为空,结束循环。此时parent为叶子结点。
3)插入新结点:新结点一定插入在parent的左侧或右侧。
2.调节结点的平衡因子。
新结点插入后,AVL树的平衡性可能遭到破坏,此时就需要更新平衡因子,并检查是否破坏了AVL树的平衡性。
当新结点cur插入后,parent的平衡因子一定需要调整。在插入结点之前,parent的平衡因子分为-1,0,1三种情况。
调节平衡因子
1)如果cur插入到parent的左侧,给parent的平衡因子-1;
2)如果cur插入到parent的右侧,给parent的平衡因子+1
此时parent的平衡因子可能是:0、正负1、正负2
如果parent的平衡因子是0,说明插入之前parent的平衡因子为正负1,插入后被调整成0,此时满足AVL数的性质,插入成功。
如果parent的平衡因子是正负1,说明插入前parent的平衡因子一定为0,插入后更新成正负1,此时以parent为根的树的高度增加,需要继续向上更新。
如果parent的平衡因子为正负2,则parent的平衡因子违反了平衡树的性质,需要对其进行旋转处理,以降低高度。
AVL树的旋转
1.新结点插入较高左子树的左侧--左左:右单旋
2.新结点插入较高右子树的右侧--右右:左单旋
3.新结点插入较高左子树的右侧--左右:先左单旋再右单旋
4.新结点插入较高右子树的左侧--右左:先右单旋再左单旋
AVL树的插入完整代码:
bool Insert(const K& key,const V& value)
{
//如果树为空,插入新结点
if (_root == nullptr)
{
_root = new Node(key,value);
return true;
}
//按照二叉搜索树的性质找到要插入的位置
Node* cur = _root;
Node* parent = nullptr;
while (cur)
{
if (key < cur->_key)
{
parent = cur;
cur = cur->_left;
}
else if (key > cur->_key)
{
parent = cur;
cur = cur->_right;
}
else
{
return false;
}
}
//插入新结点
cur = new Node(key,value);
if (key < parent->_key)
{
parent->_left = cur;
}
else
{
parent->_right = cur;
}
while (parent)
{
//更新父亲的平衡因子
if (cur == parent->_left)
{
parent->_bf--;
}
else
{
parent->_bf++;
}
//更新后检查父亲的平衡
if (parent->_bf == 0)
{
break;
}
else if (abs(parent->_bf) == 1)
{
//向上更新
cur = parent;
parent = parent->_parent;
}
else if (abs(parent->_bf) == 2)
{
//插入新结点后,AVL数的平衡性遭到破坏,需要对以parent为根的树进行旋转处理
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;
}
else
{
assert(false);
}
}
return true;
}
void RotateLR(Node* parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
int bf = subLR->_bf;
RotateL(parent->_left);
RotateR(parent);
if (bf == -1)
{
parent->_bf = 1;
subL->_bf = 0;
}
else if (bf == 1)
{
parent->_bf = 0;
subL->_bf = -1;
}
else
{
parent->_bf = subL->_bf = 0;
}
subLR->_bf = 0;
}
void RotateRL(Node* parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
int bf = subRL->_bf;
RotateR(parent->_right);
RotateL(parent);
if (bf == -1)
{
parent->_bf = 0;
subR->_bf = 1;
}
else if (bf == 1)
{
parent->_bf = -1;
subR->_bf = 0;
}
else
{
parent->_bf = subR->_bf = 0;
}
subRL->_bf = 0;
}
void RotateL(Node* parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
parent->_right = subRL;
if (subRL)
{
subRL->_parent = parent;
}
subR->_left = parent;
Node* pparent = parent->_parent;
parent->_parent = subR;
if (parent == _root)
{
_root = subR;
}
else
{
if (pparent->_left == parent)
{
pparent->_left = subR;
}
else
{
pparent->_right = subR;
}
}
subR->_parent = pparent;
subR->_bf = parent->_bf = 0;
}
void RotateR(Node* parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
parent->_left = subLR;
if (subLR)
{
subLR->_parent = parent;
}
subL->_right = parent;
Node* pparent = parent->_parent;
parent->_parent = subL;
if (parent == _root)
{
_root = subL;
}
else
{
if (pparent->_left = parent)
{
pparent->_left = subL;
}
else
{
pparent->_right = subL;
}
}
subL->_parent = pparent;
subL->_bf = parent->_bf = 0;
}
AVL树的验证
1.验证其为二叉搜索树
如果中序遍历可以得到一个有序的序列,说明其为二叉搜索树。
2.验证其为平衡树
- 每个节点子树高度差的绝对值不超过1(注意节点中如果没有平衡因子) 。
- 节点的平衡因子是否计算正确。
AVL树从构建到验证的完整代码
#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
template<class K,class V>
struct AVLTreeNode
{
AVLTreeNode<K, V>* _left;
AVLTreeNode<K, V>* _right;
AVLTreeNode<K, V>* _parent;
K _key;
V _value;
int _bf;
AVLTreeNode(const K& key,const V& value)
: _left(nullptr)
, _right(nullptr)
, _parent(nullptr)
, _key(key)
, _value(value)
, _bf(0)
{}
};
template<class K,class V>
class AVLTree
{
typedef AVLTreeNode<K,V> Node;
public:
AVLTree()
: _root(nullptr)
{}
~AVLTree()
{
Destroy(_root);
_root = nullptr;
}
bool Insert(const K& key,const V& value)
{
//如果树为空,插入新结点
if (_root == nullptr)
{
_root = new Node(key,value);
return true;
}
//按照二叉搜索树的性质找到要插入的位置
Node* cur = _root;
Node* parent = nullptr;
while (cur)
{
if (key < cur->_key)
{
parent = cur;
cur = cur->_left;
}
else if (key > cur->_key)
{
parent = cur;
cur = cur->_right;
}
else
{
return false;
}
}
//插入新结点
cur = new Node(key,value);
if (key < parent->_key)
{
parent->_left = cur;
}
else
{
parent->_right = cur;
}
//更新cur的父亲结点
cur->_parent = parent;
//插入新结点后,AVL树的平衡性可能遭到破坏,此时需要更新平衡因子,并检查是否破坏了AVL树的平衡性
while (parent)
{
//更新父亲的平衡因子
if (cur == parent->_left)
{
parent->_bf--;
}
else
{
parent->_bf++;
}
//更新后检查父亲的平衡
if (parent->_bf == 0)
{
break;
}
else if (abs(parent->_bf) == 1)
{
//向上更新
cur = parent;
parent = parent->_parent;
}
else if (abs(parent->_bf) == 2)
{
//插入新结点后,AVL数的平衡性遭到破坏,需要对以parent为根的树进行旋转处理
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;
}
else
{
assert(false);
}
}
return true;
}
void RotateLR(Node* parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
int bf = subLR->_bf;
RotateL(parent->_left);
RotateR(parent);
if (bf == -1)
{
parent->_bf = 1;
subL->_bf = 0;
}
else if (bf == 1)
{
parent->_bf = 0;
subL->_bf = -1;
}
else
{
parent->_bf = subL->_bf = 0;
}
subLR->_bf = 0;
}
void RotateRL(Node* parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
int bf = subRL->_bf;
RotateR(parent->_right);
RotateL(parent);
if (bf == -1)
{
parent->_bf = 0;
subR->_bf = 1;
}
else if (bf == 1)
{
parent->_bf = -1;
subR->_bf = 0;
}
else
{
parent->_bf = subR->_bf = 0;
}
subRL->_bf = 0;
}
void RotateL(Node* parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
parent->_right = subRL;
if (subRL)
{
subRL->_parent = parent;
}
subR->_left = parent;
Node* pparent = parent->_parent;
parent->_parent = subR;
if (parent == _root)
{
_root = subR;
}
else
{
if (pparent->_left == parent)
{
pparent->_left = subR;
}
else
{
pparent->_right = subR;
}
}
subR->_parent = pparent;
subR->_bf = parent->_bf = 0;
}
void RotateR(Node* parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
parent->_left = subLR;
if (subLR)
{
subLR->_parent = parent;
}
subL->_right = parent;
Node* pparent = parent->_parent;
parent->_parent = subL;
if (parent == _root)
{
_root = subL;
}
else
{
if (pparent->_left = parent)
{
pparent->_left = subL;
}
else
{
pparent->_right = subL;
}
}
subL->_parent = pparent;
subL->_bf = parent->_bf = 0;
}
void Inorder()
{
_Inorder(_root);
cout << endl;
}
void Height()
{
cout << _Height(_root) << endl;
}
bool IsBalanceTree()
{
return _IsBalanceTree(_root);
}
int _Height(Node* root)
{
if (root == nullptr)
return 0;
int leftHeight = _Height(root->_left);
int rightHeight = _Height(root->_right);
return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
}
bool _IsBalanceTree(Node* root)
{
if (_root == nullptr)
return true;
int leftHeight = _Height(root->_left);
int rightHeight = _Height(root->_right);
if (abs(leftHeight - rightHeight) > 2 || (leftHeight - rightHeight) != root->_bf)
{
cout << " 平衡因子错误" << endl;
return false;
}
return _IsBalanceTree(root->_left) && _IsBalanceTree(root->_right);
}
private:
void Destroy(Node* root)
{
if (root)
{
Destroy(root->_left);
Destroy(root->_right);
delete root;
}
}
void _Inorder(Node* root)
{
if (root == nullptr)
return;
_Inorder(root->_left);
cout << root->_key << " ";
_Inorder(root->_right);
}
Node* _root;
};
void TestAVLTree()
{
AVLTree<int,int> tree;
int arr[] = { 26, 18, 14, 5, 3 };
for (auto& e : arr)
{
tree.Insert(e,e);
}
tree.Inorder();
tree.Height();
tree.IsBalanceTree();
}
AVL树的性能
AVL树是一棵绝对平衡的二叉搜索树,其要求每个结点的左右子树高度差的绝对值都不超过1,这样可以保证查询时高效的时间复杂度,即O(logN)。但是如果要对AVL树做一些结构修改的操作,性能就非常低下了。如:插入时要维护其绝对平衡,旋转的次数比较多,更差的是在删除时,有可能一直要让旋转持续到根的位置。因此,如果需要一种查询高效且有序的数据结构,而且数据的个数为静态的(不会改变),可以考虑AVL树,但如果一个结构经常修改,就不太适合用AVL树。