目录
1、前言
AVL树是数据结构的发展史中一个过度的数据结构,他解决了搜索二叉树的一些缺陷,让数据的插入不论有多么不均衡,都能保持树的各个子树平衡,保持树的最佳搜索姿态。这篇文章是我个人在学习过程中,对自己的理解的一个总结,如果有什么错误,欢迎大家私信我,、
2、AVL树
二叉搜索树虽可以缩短查找的效率,但如果数据有序或接近有序二叉搜索树将退化为单支树,查找元素相当于在顺序表中搜索元素,效率低下。因此,两位俄罗斯的数学家G.M.Adelson-Velskii和E.M.Landis在1962年发明了一种解决上述问题的方法:当向二叉搜索树中插入新结点后,如果能保证每个结点的左右子树高度之差的绝对值不超过1(需要对树中的结点进行调整),即可降低树的高度,从而减少平均搜索长度。
一棵AVL树或者是空树,或者是具有以下性质的二叉搜索树:
它的左右子树都是AVL树
左右子树高度之差(简称平衡因子)的绝对值不超过1(-1/0/1)
如果一棵二叉搜索树是高度平衡的,它就是AVL树。如果它有n个结点,其高度可保持在O(log_2 n),搜索时间复杂度O(log_2 n)。
3、AVL树平衡过程的模拟实现:
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; // 该节点的平衡因子
};
AVL树的定义
template<class T>
class AVLTree
{
typedef AVLTreeNode<T> Node;
public:
AVLTree()
: _root(nullptr)
{}
// 在AVL树中插入值为data的节点
bool Insert(const T& data);
// AVL树的验证
bool IsAVLTree()
{
return _IsAVLTree(_root);
}
private:
// 根据AVL树的概念验证pRoot是否为有效的AVL树
bool _IsAVLTree(Node* pRoot) {
if (!pRoot) return true;
size_t leftHeight = _Height(pRoot->_pLeft);
size_t rightHeight = _Height(pRoot->_pRight);
int diff = rightHeight - leftHeight;
if (diff != pRoot->_bf || (diff < -1 || diff > 1))
return false;
return _IsAVLTree(pRoot->_pLeft) && _IsAVLTree(pRoot->_pRight);
}
size_t _Height(Node* pRoot) {
if (!pRoot) {
return 0;
}
size_t left = _Height(pRoot->_pLeft);
size_t right = _Height(pRoot->_pRight);
return 1 + (left >= right ? left : right);
}
// 右单旋
void RotateR(Node* pSub);
// 左单旋
void RotateL(Node* pSub);
// 右左双旋
void RotateRL(Node* pSub);
// 左右双旋
void RotateLR(Node* pSub);
private:
Node* _root;
};
AVL树的插入:
AVL树的插入可以分为两步:
1. 按照二叉搜索树的方式插入新节点
2. 调整节点的平衡因子
bool Insert(const T& data) {
Node* node = new Node(data);
if (_root == nullptr) {
_root = node;
node->_bf = 0;
return true;
}
Node* cur = _root;
while (1) {
if (node->_data == cur->_data)
return false;
if (node->_data < cur->_data) {
if (cur->_pLeft == nullptr) {
cur->_pLeft = node;
node->_pParent = cur;
break;
}
else {
cur = cur->_pLeft;
}
}
else if (node->_data > cur->_data) {
if (cur->_pRight == nullptr) {
cur->_pRight = node;
node->_pParent = cur;
break;
}
else {
cur = cur->_pRight;
}
}
}
Node* child = node;
Node* parent = cur;
while (parent) {
if (child == parent->_pLeft)
parent->_bf--;
else
parent->_bf++;
//检测平衡因子
if (parent->_bf == 0) {
//符合平衡二叉树的要求,插入前和插入后数的高度没有发生变化,插入完成
break;
}
else if (parent->_bf == 1 || parent->_bf == -1) {
//这个时候说明,插入新节点之后,双亲为根的树的高度发生了变化,要继续往上更新
child = parent;
parent = parent->_pParent;
}
else if (parent->_bf == 2 || parent->_bf == -2) {
//说明在新节点插入后,parent这棵子树已经不平衡了,需要旋转来平衡
if (parent->_bf == 2 && child->_bf == 1) {
//左单旋
RotateL(child);
}
else if (parent->_bf == -2 && child->_bf == -1) {
//右单旋
RotateR(child);
}
else if (parent->_bf == 2 && child->_bf == -1) {
//双旋:先右单旋,在左单旋
RotateRL(child);
}
else if (parent->_bf == -2 && child->_bf == 1) {
//双旋:先左单旋,在右单旋
RotateLR(child);
}
else {
assert(false);
}
break;
}
}
}
如果在插入的时候,破坏了AVL的平衡,那么我们需要对AVL进行翻转调整
(1)、左单旋
// 左单旋
void RotateL(Node* pSub) {
Node* subParent = pSub->_pParent;
Node* subChildL = pSub->_pLeft;
Node* subChildR = pSub->_pRight;
Node* pParent = nullptr;
subParent->_pRight = subChildL;
if (subChildL) {
subChildL->_pParent = subParent;
}
if (subParent == _root) {
_root = pSub;
}
else {
pParent = subParent->_pParent;
}
pSub->_pLeft = subParent;
pSub->_pParent = pParent;
subParent->_pParent = pSub;
if (pParent) {
if (pParent->_pLeft == subParent)
pParent->_pLeft = pSub;
else if (pParent->_pRight == subParent)
pParent->_pRight = pSub;
}
//更新节点的平衡因子
pSub->_bf = subParent->_bf = 0;
}
(2)、右单旋
// 右单旋
void RotateR(Node* pSub) {
Node* subParent = pSub->_pParent;
Node* subChildL = pSub->_pLeft;
Node* subChildR = pSub->_pRight;
Node* pParent = nullptr;
//Node* pParent = subParent->_pParent;
subParent->_pLeft = subChildR;
if (subChildR) {
subChildR->_pParent = subParent;
}
if (subParent == _root) {
_root = pSub;
}
else {
//保存父节点的父节点,用于链接
pParent = subParent->_pParent;
}
pSub->_pRight = subParent;
pSub->_pParent = pParent;
subParent->_pParent = pSub;
if (pParent) {
if (pParent->_pLeft == subParent)
pParent->_pLeft = pSub;
else if (pParent->_pRight == subParent)
pParent->_pRight = pSub;
}
//更新节点的平衡因子
pSub->_bf = subParent->_bf = 0;
}
(3)、左右双旋
// 左右双旋
void RotateLR(Node* pSub) {
Node* subChildR = pSub->_pRight;
Node* subParent = pSub->_pLeft;
int bf = subChildR->_bf;
RotateL(subChildR);
RotateR(subChildR);
if (bf == 1) {
pSub->_bf = -1;
}
else if (bf == -1) {
subParent->_bf = 1;
}
}
(4)、右左双旋
// 右左双旋
void RotateRL(Node* pSub) {
Node* subChildL = pSub->_pLeft;
Node* subParent = pSub->_pParent;
int bf = subChildL->_bf;
RotateR(subChildL);
RotateL(subChildL);
//更新平衡因子
if (bf == 1) {
subParent->_bf = -1;
}
else if (bf == -1) {
subChildL->_bf = 1;
}
}
4、结语
欢迎各位读者指出我在学习中的错误