目录
AVL树的概念:
前文提到的二叉搜索树虽然可以提高搜索效率,但是可能会形成单支树,又由于二叉树的查找效率是高度次,所以但支树的搜索二叉树会导致查找效率低下。G.M.Adelson-Velskii和E.M.Landis发明了平衡二叉树(AVL)来解决上述问题,向二叉树插入新节点后,保证每个节点的左右子树之差的绝对值不超过1,即可降低二叉树高度,提高搜索效率。
AVL树节点的定义:
template <class T>
struct AVLTreeNode
{
AVLTreeNode(const T&data)
:_data(data)
,_left(nullptr)
,_right(nullptr)
,_parnet(nullptr)
,__pf(0)
{}
T _data;
AVLTreeNode<T>* _left; //该节点的左孩子
AVLTreeNode<T>* _right; //该节点的右孩子
AVLTreeNode<T>* _parent; //该节点的双亲
int _pf; //定义平衡因子
}
AVL树的插入:
按照二叉搜索树的形式插入新节点:
查找要插入的节点的位置:
二叉搜索树默认是当前节点的左节点的数值小于当前节点的数值,当前节点的右节点的数值大于当前节点的数值,如果要插入的值小于当前节点的数值,就往左走,大于当前节点的数值,就往右走。又因为AVL树是没有重复数值的,所以当出现要插入的数值跟树的某个节点的数值相同时,则退出函数,不进行插入操作。
Node* parent = nullptr;
Node* pcur = _root;
while (pcur)
{
if (pcur->_kv.first < kv.first)
{
parent = pcur;
pcur = pcur->_right;
}
else if (pcur->_kv.first > kv.first)
{
parent = pcur;
pcur = pcur->_left;
}
else
{
return false;
}
}
插入节点:
pcur = new Node(kv);
if (parent->_kv.first > kv.first)
{
parent->_left = pcur;
}
else if (parent->_kv.first < kv.first)
{
parent->_right = pcur;
}
调整节点的平衡因子:
平衡因子默认是右减左:
pCur插入后,pParent的平衡因子一定需要调整,在插入之前,pParent的平衡因子分为三种情况:-1,0, 1, 分以下两种情况:
- 如果pCur插入到pParent的左侧,只需给pParent的平衡因子-1即可
- 如果pCur插入到pParent的右侧,只需给pParent的平衡因子+1即可
此时:pParent的平衡因子可能有三种情况:
0
如果pParent的平衡因子为0,说明插入之前pParent的平衡因子为正负1,插入后被调整成0,此时满足AVL树的性质,插入成功。
if (parent->_bf == 0)
break;
正负1
如果pParent的平衡因子为正负1,说明插入前pParent的平衡因子一定为0,插入后被更新成正负1,此时以pParent为根的树的高度增加,需要继续向上更新。
else if (parent->_bf == 1 || parent->_bf == -1)
{
pcur = parent;
parent = parent->_parent;
}
正负2
如果pParent的平衡因子为正负2,则pParent的平衡因子违反平衡树的性质,需要对其进行旋转处理。
else if (parent->_bf == 2 || parent->_bf == -2)
{
if (parent->_bf == 2 && pcur->_bf == 1)
{
RotateL(parent);
}
else if (parent->_bf == -2 && parent->_bf == -1)
{
RotateR(parent);
}
else if (parent->_bf == 2 && parent->_bf == -1)
{
RotateRL(parent);
}
else
{
RotateLR(parent);
}
}
AVL树的旋转:
如果在一个原本平衡的AVL树中插入一个新的节点,可能造成不平和,这时就必须使用旋转调整树的结构,使其再次平衡。
左旋:
void RotateL(Node* parent)
{
Node* SubR = parent->_left;
Node* SubRL = SubR->_right;
Node* Parentparent = parent->_parent;
parent->_left = SubRL;
if (SubRL)
SubRL->_parent = parent;
SubR->_right = parent;
parent->_parent = SubR;
if (Parentparent == nullptr)
{
SubR->_parent = nullptr;
_root = SubR;
}
else
{
if (parent == Parentparent->_left)
Parentparent->_left = SubR;
else if (parent == Parentparent->_right)
Parentparent->_right = SubR;
SubR->_parent = Parentparent;
}
parent->_bf = SubR->_bf = 0;
}
右旋:
void RotateR(Node* parent)
{
Node* SubL = parent->_left;
Node* SubLR = SubL->_right;
Node* Parentparent = parent->_parent;
parent->_left = SubLR;
if (SubLR)
SubLR->_parent = parent;
SubL->_right = parent;
parent->_parent = SubL;
if (Parentparent == nullptr)
{
SubL->_parent = nullptr;
_root = SubL;
}
else
{
if (parent == Parentparent->_left)
Parentparent->_left = SubL;
else if (parent == Parentparent->_right)
Parentparent->_right = SubL;
SubL->_parent = Parentparent;
}
parent->_bf = SubL->_bf = 0;
}
左右双旋:
void RotateLR(Node* parent)
{
Node* SubL = parent->_left;
Node* SubLR = SubL->_right;
int bf = SubLR->_bf;
RotateL(SubL);
RotateR(parent);
if (bf == 0)
{
parent->_bf = 0;
SubL->_bf = 0;
SubLR = 0;
}
else if (bf == -1)
{
parent->_bf = 1;
SubLR->_bf = 0;
SubL->_bf = 0;
}
else if (bf == 1)
{
SubLR->_bf = 0;
parent->_bf = 0;
SubL->_bf = -1;
}
else
{
assert(false);
}
}
右左双旋:
void RotateRL(Node* parent)
{
Node* SubR = parent->_right;
Node* SubRL = SubR->_left;
int bf = SubRL->_bf;
RotateR(parent->_right);
RotateL(parent);
if (bf == 0)
{
SubR->_bf = 0;
SubRL->_bf = 0;
parent->_bf = 0;
}
else if (bf == 1)
{
SubR->_bf = 0;
SubRL->_bf = 0;
parent->_bf = -1;
}
else if (bf == -1)
{
SubR->_bf = 1;
SubRL->_bf = 0;
parent->_bf = 0;
}
else
{
assert(false);
}
}
AVL树的验证:
验证其为二叉搜索树:
中序遍历是升序
验证其为平衡树:
每个节点子树高度差的绝对值不超过1(注意节点中如果没有平衡因子)节点的平衡因子是否计算正确
AVL树源码:
#pragma once
#include <assert.h>
#include <iostream>
using namespace std;
template<class K,class V>
struct AVLNode
{
pair<K, V> _kv;
AVLNode<K, V>* _left;
AVLNode<K, V>* _right;
AVLNode<K, V>* _parent;
int _bf;
AVLNode(const pair<K,V>&kv)
:_kv(kv)
,_left(nullptr)
,_right(nullptr)
,_parent(nullptr)
,_bf(0)
{}
};
template<class K,class V>
class AVLTree
{
typedef AVLNode<K, V> Node;
public:
AVLTree()
:_root(nullptr)
{}
AVLTree(const AVLTree<K, V>& r)
{
_root = Copy(r._root);
}
AVLTree<K,V>& operator=(AVLTree<K,V> t)
{
std::swap(_root, t._root);
return *this;
}
~AVLTree()
{
Destory(_root);
_root = nullptr;
}
bool insert(const pair<K, V>& kv)
{
if (_root == nullptr)
{
_root = new Node(kv);
return true;
}
Node* parent = nullptr;
Node* pcur = _root;
while (pcur)
{
if (pcur->_kv.first < kv.first)
{
parent = pcur;
pcur = pcur->_right;
}
else if (pcur->_kv.first > kv.first)
{
parent = pcur;
pcur = pcur->_left;
}
else
{
return false;
}
}
pcur = new Node(kv);
if (parent->_kv.first > kv.first)
{
parent->_left = pcur;
}
else if (parent->_kv.first < kv.first)
{
parent->_right = pcur;
}
//更改平衡因子
//默认是右减左
while (parent)
{
if (pcur == parent->_left)
parent->_bf--;
else if (pcur == parent->_right)
parent->_bf++;
if (parent->_bf == 0)
break;
else if (parent->_bf == 1 || parent->_bf == -1)
{
pcur = parent;
parent = parent->_parent;
}
else if (parent->_bf == 2 || parent->_bf == -2)
{
if (parent->_bf == 2 && pcur->_bf == 1)
{
RotateL(parent);
}
else if (parent->_bf == -2 && parent->_bf == -1)
{
RotateR(parent);
}
else if (parent->_bf == 2 && parent->_bf == -1)
{
RotateRL(parent);
}
else
{
RotateLR(parent);
}
}
else
{
assert(false);
}
}
return true;
}
bool Find(const pair<K, V>& kv)
{
Node* pcur = _root;
while (pcur)
{
if (kv.first < pcur->_kv.first)
{
pcur = pcur->_left;
}
else if (kv.first > pcur->_kv.first)
{
pcur = pcur->_right;
}
else
{
return true;
}
}
return false;
}
bool isbalance()
{
return _isbalance(_root);
}
private:
Node* Copy(Node* root)
{
if (root == nullptr)
{
return nullptr;
}
Node* newroot = new Node(root->_kv);
newroot->_left = Copy(root->_left);
newroot->_right = Copy(root->_right);
return newroot;
}
void Destory(Node* root)
{
if (root == nullptr)
{
return;
}
Destory(root->_left);
Destory(root->_right);
delete root;
}
void RotateL(Node* parent)
{
Node* SubR = parent->_left;
Node* SubRL = SubR->_right;
Node* Parentparent = parent->_parent;
parent->_left = SubRL;
if (SubRL)
SubRL->_parent = parent;
SubR->_right = parent;
parent->_parent = SubR;
if (Parentparent == nullptr)
{
SubR->_parent = nullptr;
_root = SubR;
}
else
{
if (parent == Parentparent->_left)
Parentparent->_left = SubR;
else if (parent == Parentparent->_right)
Parentparent->_right = SubR;
SubR->_parent = Parentparent;
}
parent->_bf = SubR->_bf = 0;
}
void RotateR(Node* parent)
{
Node* SubL = parent->_left;
Node* SubLR = SubL->_right;
Node* Parentparent = parent->_parent;
parent->_left = SubLR;
if (SubLR)
SubLR->_parent = parent;
SubL->_right = parent;
parent->_parent = SubL;
if (Parentparent == nullptr)
{
SubL->_parent = nullptr;
_root = SubL;
}
else
{
if (parent == Parentparent->_left)
Parentparent->_left = SubL;
else if (parent == Parentparent->_right)
Parentparent->_right = SubL;
SubL->_parent = Parentparent;
}
parent->_bf = SubL->_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 == 0)
{
SubR->_bf = 0;
SubRL->_bf = 0;
parent->_bf = 0;
}
else if (bf == 1)
{
SubR->_bf = 0;
SubRL->_bf = 0;
parent->_bf = -1;
}
else if (bf == -1)
{
SubR->_bf = 1;
SubRL->_bf = 0;
parent->_bf = 0;
}
else
{
assert(false);
}
}
void RotateLR(Node* parent)
{
Node* SubL = parent->_left;
Node* SubLR = SubL->_right;
int bf = SubLR->_bf;
RotateL(SubL);
RotateR(parent);
if (bf == 0)
{
parent->_bf = 0;
SubL->_bf = 0;
SubLR = 0;
}
else if (bf == -1)
{
parent->_bf = 1;
SubLR->_bf = 0;
SubL->_bf = 0;
}
else if (bf == 1)
{
SubLR->_bf = 0;
parent->_bf = 0;
SubL->_bf = -1;
}
else
{
assert(false);
}
}
int Hight(Node* root)
{
if (root == nullptr)
{
return 0;
}
int left = Hight(root->_left);
int right = Hight(root->_right);
return left > right ? left + 1 : right + 1;
}
bool _isbalance(Node* root)
{
if (root == nullptr)
{
return true;
}
int left = Hight(root->_left);
int right = Hight(root->_right);
if (abs(left - right) >= 2 || abs(left - right) != root->_bf)
{
return false;
}
return _isbalance(root->_left) && _isbalance(root->_right);
}
Node* _root = nullptr;
};