1:AVLTree的概念
1:AVL树是最先发明的⾃平衡⼆叉查找树,AVL是⼀颗空树,或者具备下列性质的⼆叉搜索树:它的左右⼦树都是AVL树,且左右⼦树的⾼度差的绝对值不超过1。AVL树是⼀颗⾼度平衡搜索⼆叉树,通过控制⾼度差去控制平衡。
2: AVL树得名于它的发明者G.M.Adelson - Velsky和E.M.Landis是两个前苏联的科学家,他们在1962年的论⽂《An algorithm for the organization of information》中发表了它。
3:AVL树实现这⾥我们引⼊⼀个平衡因⼦(balance factor)的概念,每个结点都有⼀个平衡因⼦,任何结点的平衡因⼦等于右⼦树的⾼度减去左⼦树的⾼度,也就是说任何结点的平衡因⼦等于0 / 1 / -1,AVL树并不是必须要平衡因⼦,但是有了平衡因⼦可以更⽅便我们去进⾏观察和控制树是否平衡,就像⼀个风向标⼀样。
4:思考⼀下为什么AVL树是⾼度平衡搜索⼆叉树,要求⾼度差不超过1,⽽不是⾼度差是0呢?0不是更好的平衡吗?画画图分析我们发现,不是不想这样设计,⽽是有些情况是做不到⾼度差是0的。⽐如⼀棵树是2个结点,4个结点等情况下,⾼度差最好就是1,⽆法做到⾼度差是0
5:AVL树整体结点数量和分布和完全⼆叉树类似,⾼度可以控制在 ,那么增删查改的效率也可以控制在 ,相⽐⼆叉搜索树有了本质的提升
2:AVLTree的实现
1:AVLTree的节点
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;//节点的平衡因子
};
template<class T>
class AVLTree
{
typedef AVLTreeNode<T> Node;
public:
AVLTree():_pRoot(nullptr){}
//.................
private:
Node* _pRoot;
};
2:AVLTree的插入
1:插入的大概过程
1. 插⼊⼀个值按⼆叉搜索树规则进⾏插⼊。
2. 新增结点以后,只会影响祖先结点的⾼度,也就是可能会影响部分祖先结点的平衡因⼦,所以更新从新增结点->根结点路径上的平衡因⼦,实际中最坏情况下要更新到根,有些情况更新到中间就可以停⽌了,具体情况我们下⾯再详细分析。
3. 更新平衡因⼦过程中没有出现问题,则插⼊结束
4. 更新平衡因⼦过程中出现不平衡,对不平衡⼦树旋转,旋转后本质调平衡的同时,本质降低了⼦树的⾼度,不会再影响上⼀层,所以插⼊结束。
2:平衡因子的更新
更新原则:
• 平衡因⼦ = 右⼦树⾼度 - 左⼦树⾼度
• 只有⼦树⾼度变化才会影响当前结点平衡因⼦。
• 插⼊结点,会增加⾼度,所以新增结点在parent的右⼦树,parent的平衡因⼦++,新增结点在parent的左⼦树,parent平衡因⼦--
• parent所在⼦树的⾼度是否变化决定了是否会继续往上更新
更新停⽌条件:
• 更新后parent的平衡因⼦等于0,更新中parent的平衡因⼦变化为 - 1->0 或者 1->0,说明更新前parent⼦树⼀边⾼⼀边低,新增的结点插⼊在低的那边,插⼊后parent所在的⼦树⾼度不变,不会影响parent的⽗亲结点的平衡因⼦,更新结束。
• 更新后parent的平衡因⼦等于1 或 - 1,更新前更新中parent的平衡因⼦变化为0->1 或者 0-> - 1,说明更新前parent⼦树两边⼀样⾼,新增的插⼊结点后,parent所在的⼦树⼀边⾼⼀边低,parent所在的⼦树符合平衡要求,但是⾼度增加了1,会影响parent的⽗亲结点的平衡因⼦,所以要继续向上更新。
• 更新后parent的平衡因⼦等于2 或 - 2,更新前更新中parent的平衡因⼦变化为1->2 或者 - 1-> - 2,说明更新前parent⼦树⼀边⾼⼀边低,新增的插⼊结点在⾼的那边,parent所在的⼦树⾼的那边更⾼了,破坏了平衡,parent所在的⼦树不符合平衡要求,需要旋转处理,旋转的⽬标有两个:1、把parent⼦树旋转平衡。2、降低parent⼦树的⾼度,恢复到插⼊结点以前的⾼度。所以旋转后也不需要继续往上更新,插⼊结束。
• 不断更新,更新到根,跟的平衡因⼦是1或 - 1也停⽌了
3:更新平衡因子的代码实现
bool Insert(const T& data)
{
if (_pRoot == nullptr)
{
_pRoot = new Node(data);
return true;
}
Node* parent = nullptr;
Node* cur = _pRoot;
while (cur)
{
parent = cur;
if (cur->_data < data)
{
cur = cur->_pRight;
}
else if (cur->_data > data)
{
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)
{
RR(parent);
}
else if (parent->_bf == 2 && cur->_bf == 1)
{
RL(parent);
}
else if (parent->_bf == -2 && cur->_bf == 1)
{
RLR(parent);
}
else if (parent->_bf == 2 && cur->_bf == -1)
{
RRL(parent);
}
break;
}
else
{
assert(false);
}
}
}
3:AVLTree的旋转
1:旋转的原则
1. 保持搜索树的规则
2. 让旋转的树从不满⾜变平衡,其次降低旋转树的⾼度旋转总共分为四种,左单旋 / 右单旋 / 左右双旋 / 右左双旋。
说明:下⾯的图中,有些结点我们给的是具体值,如10和5等结点,这⾥是为了⽅便讲解,实际中是什么值都可以,只要⼤⼩关系符合搜索树的性质即可。
2:右单旋
//右单旋
void RR(Node* pParent)
{
Node* subL = pParent->_pLeft;
Node* subLR = subL->_pRight;
pParent->_pLeft = subLR;
if (subLR != nullptr)
{
subLR->_pParent = pParent;
}
subL->_pRight = pParent;
Node* ppnode = pParent->_pParent;
pParent->_pParent = subL;
subL->_pParent = ppnode;
if (ppnode == nullptr)
{
_pRoot = subL;
}
else
{
if (ppnode->_pLeft == pParent)
{
ppnode->_pLeft = subL;
}
else
{
ppnode->_pRight = subL;
}
}
subL->_bf = pParent->_bf = 0;
}
3:左单旋
//左单旋
void RL(Node* pParent)
{
Node* subR = pParent->_pRight;
Node* subRL = subR->_pLeft;
pParent->_pRight = subRL;
if (subRL!=nullptr)
{
subRL->_pParent = pParent;
}
subR->_pLeft = pParent;
Node* ppnode = pParent->_pParent;
pParent->_pParent = subR;
subR->_pParent = ppnode;
if (ppnode == nullptr)
{
_pRoot = subR;
}
else
{
if (ppnode->_pLeft == pParent)
{
ppnode->_pLeft = subR;
}
else
{
ppnode->_pRight = subR;
}
}
pParent->_bf = subR->_bf = 0;
}
4:右左双旋
//右左双旋转
void RRL(Node* pParent)
{
Node* subR = pParent->_pRight;
Node* subRL = subR->_pLeft;
int bf = subRL->_bf;
RR(subR);
RL(pParent);
if (bf == -1)
{
pParent->_bf = 0;
subR->_bf = 1;
subRL->_bf = 0;
}
else if (bf == 1)
{
pParent->_bf = -1;
subR->_bf = 0;
subRL->_bf = 0;
}
else if (bf == 0)
{
pParent->_bf = 0;
subR->_bf = 0;
subRL->_bf = 0;
}
else
{
assert(false);
}
}
5:左右双旋
//左右双旋转
void RLR(Node* pParent)
{
Node* subL = pParent->_pLeft;
Node* subLR = subL->_pRight;
int bf = subLR->_bf;
RL(subL);
RR(pParent);
if (bf == 1)
{
pParent->_bf = 0;
subL->_bf = -1;
subLR->_bf = 0;
}
else if (bf == -1)
{
pParent->_bf = 1;
subL->_bf = 0;
subLR->_bf = 0;
}
else if (bf == 0)
{
pParent->_bf = 0;
subL->_bf = 0;
subLR->_bf = 0;
}
else
{
assert(false);
}
}