我们知道map/multimap/set/multiset,这几个容器有个
共同点是:其底层都是按照二叉搜索树来实现的,但是二叉搜索树有其自身的缺陷,假如往树中插入的元素有序或者接近有序,二叉搜索树就会退化成单支树,时间复杂度会退化成O(N),因此map、set等关联式容器的底层结构是对二叉树进行了平衡处理,即采用平衡树来实现。
原因:
方案:
一棵AVL树或者是空树,或者是具有以下性质的二叉搜索树:

如果一棵二叉搜索树是高度平衡的,它就是AVL树。如果它有n个结点,其高度可保持在 O ( l o g 2 n ) O(log_2 n) O(log2n),搜索时间复杂度O( l o g 2 n log_2 n log2n)。(也就是这个树的高度h)
template<class K, class V>
struct AVLTreeNode
{
AVLTreeNode<K, V>* _left;// 该节点的左孩子
AVLTreeNode<K, V>* _right;// 该节点的右孩子
AVLTreeNode<K, V>* _parent;// 该节点的双亲
pair<K, V> _kv;
int _bf; //该节点的平衡因子
AVLTreeNode(const pair<K, V>& kv)//初始化列表初始化
:_left(nullptr)
, _right(nullptr)
, _parent(nullptr)
, _kv(kv)
, _bf(0)
{}
};
AVL树的插入过程分为两步:
实现的模板如下,旋转下面会具体讲解:
bool Insert(const pair<K, V>& kv)
{
if (_root == nullptr)
{
_root = new Node(kv);
return true;
}
//找find
Node* parent = nullptr;
Node* cur = _root;
while (cur)
{
if (cur->_kv.first < kv.first)
{
parent = cur;
cur = cur->_right;
}
else if (cur->_kv.first > kv.first)
{
parent = cur;
cur = cur->_left;
}
else
{
//重复的
return false;
}
}
//插入
cur = new Node(kv);
if (parent->_kv.first < kv.first)
{
parent->_right = cur;
}
else
{
parent->_left = cur;
}
cur->_parent = parent;
//更新平衡因子
while (parent) // 从下往上更新,最坏情况全部更新完
{
if (cur == parent->_right)
{
parent->_bf++;
}
else //cur==paren->_left
{
parent->_bf--;
}
//是否继续更新?
if (parent->_bf == 0)
{
//高度不变,更新结束
break;
}
else if (parent->_bf == 1 || parent->_bf == -1) //-1 、1
{
//子树高度变了,继续更新祖先
cur = parent;
parent = parent->_parent;
}
else if (parent->_bf == 2 || parent->_bf == -2) //平衡旋转
{
// 双亲的平衡因子为正负2,违反了AVL树的平衡性,需要对以pParent;为根的树进行旋转处理
//旋转:左、右、左右、右左
if (parent->_bf == 2 && cur->_bf == 1)
{
RotateL(parent);
}
else if (parent->_bf == -2 && cur->_bf == -1)
{
RotateR(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;
}
注意:
下面讲解旋转实现的方法。
抽象图:

实例化图:
下面5这个结点不为nulltptr的情况
下面5这个结点为nulltptr的情况

代码实现如下:(注意原根的parent,它可能不为空即可能为一个树中的子树)
void RotateL(Node* parent) //左旋
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
parent->_right = subRL;
if (subRL)
{
subRL->_parent = parent;
}
Node* ppNode = parent->_parent;
subR->_left = parent;
parent->_parent = subR;
if (parent == _root)
{
_root = subR;
_root->_parent = nullptr;
}
else
{
if (ppNode->_left == parent)
{
ppNode->_left = subR;
}
else //ppNode->_right == parent
{
ppNode->_right = subR;
}
subR->_parent = ppNode;
}
//更新平衡因子
parent->_bf = 0;
subR->_bf = 0;
}
抽象图:

实例化图:
下面5这个结点不为nulltptr的情况
下面5这个结点为nulltptr的情况

代码实现如下:
void RotateR(Node* parent) //右旋
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
parent->_left = subLR;
if (subLR)
{
subLR->_parent = parent;
}
Node* ppNode = parent->_parent;
subL->_right = parent;
parent->_parent = subL;
if (_root == parent)
{
_root = subL;
_root->_parent = nullptr;
}
else
{
if (ppNode->_left == parent)
{
ppNode->_left = subL;
}
else
{
ppNode->_right = subL;
}
subL->_parent = ppNode;
}
//更新平衡因子
parent->_bf = 0;
subL->_bf = 0;
}
通过左右旋转的方法,我们知道了旋转是如何做到的。
其实就是对左、右旋转的复用。


void RotateLR(Node* parent) //左右旋转
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
int bf = subLR->_bf;
RotateL(parent->_left);
RotateR(parent);
//更新平衡因子
if (bf == 0)
{
subLR->_bf = 0;
subL->_bf = 0;
parent->_bf = 0;
}
else if (bf == 1)
{
subLR->_bf = 0;
subL->_bf = -1;
parent->_bf = 0;
}
else if (bf == -1)
{
subLR->_bf = 0;
subL->_bf = 0;
parent->_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)
{
subRL->_bf = 0;
subR->_bf = 0;
parent->_bf = 0;
}
else if (bf == 1)
{
subRL->_bf = 0;
subR->_bf = 0;
parent->_bf = -1;
}
else if (bf == -1)
{
subRL->_bf = 0;
subR->_bf = 1;
parent->_bf = 0;
}
else
{
//旋转前就有问题
assert(false);
}
}
总结:
假如以parent为根的子树不平衡,即parent的平衡因子为2或者-2,分以下情况考虑
旋转完成后,原pParent为根的子树个高度降低,已经平衡,不需要再向上更新。
AVL树是在二叉搜索树的基础上加入了平衡性的限制,因此要验证AVL树,可以分两步:
验证其为二叉搜索树
如果中序遍历可得到一个有序的序列,就说明为二叉搜索树。
验证其为平衡树
// AVL树的验证
bool IsAVLTree()
{
return _IsAVLTree(_root);
}
void InOrder()
{
_InOrder(_root);
cout << endl;
}
具体实现如下:
验证其为平衡树思路:计算每棵树的高度差,再与平衡因子比较即可。
int _Height(Node* root)
{
if (root == nullptr)
{
return 0;
}
return max(_Height(root->_left), _Height(root->_right)) + 1;
}
bool _IsAVLTree(Node* root)
{
//空数也是AVL树
if (nullptr == root)
{
return true;
}
//计算_root结点的平衡因子
int leftHeight = _Height(root->_left);
int rightHeight = _Height(root->_right);
int diff = rightHeight - leftHeight;
//判断
if (abs(diff) >= 2)
{
cout << root->_kv.first << "结点平衡因子异常" << endl;
return false;
}
else if (diff != root->_bf)
{
cout << root->_kv.first << "结点平衡因子异常" << endl;
return false;
}
return _IsAVLTree(root->_left) && _IsAVLTree(root->_right);
}
void _InOrder(Node* root)
{
if (root == nullptr)
{
return;
}
_InOrder(root->_left);
cout << root->_kv.second << " ";
_InOrder(root->_right);
}
#pragma once
#include
#include
#include
#include
#include
using namespace std;
template<class K, class V>
struct AVLTreeNode
{
AVLTreeNode<K, V>* _left;
AVLTreeNode<K, V>* _right;
AVLTreeNode<K, V>* _parent;
pair<K, V> _kv;
int _bf; //平衡因子
AVLTreeNode(const pair<K, V>& kv)
:_left(nullptr)
, _right(nullptr)
, _parent(nullptr)
, _kv(kv)
, _bf(0)
{}
};
template<class K, class V>
class AVLTree
{
public:
typedef AVLTreeNode<K, V> Node;
AVLTree()
{}
bool Insert(const pair<K, V>& kv)
{
if (_root == nullptr)
{
_root = new Node(kv);
return true;
}
//找find
Node* parent = nullptr;
Node* cur = _root;
while (cur)
{
if (cur->_kv.first < kv.first)
{
parent = cur;
cur = cur->_right;
}
else if (cur->_kv.first > kv.first)
{
parent = cur;
cur = cur->_left;
}
else
{
//重复的
return false;
}
}
//插入
cur = new Node(kv);
if (parent->_kv.first < kv.first)
{
parent->_right = cur;
}
else
{
parent->_left = cur;
}
cur->_parent = parent;
//更新平衡因子
while (parent) // 从下往上更新,最坏情况全部更新完
{
if (cur == parent->_right)
{
parent->_bf++;
}
else //cur==paren->_left
{
parent->_bf--;
}
//是否继续更新?
if (parent->_bf == 0)
{
//高度不变,更新结束
break;
}
else if (parent->_bf == 1 || parent->_bf == -1) //-1 、1
{
//子树高度变了,继续更新祖先
cur = parent;
parent = parent->_parent;
}
else if (parent->_bf == 2 || parent->_bf == -2) //平衡旋转
{
//旋转:左、右、左右、右左
if (parent->_bf == 2 && cur->_bf == 1)
{
RotateL(parent);
}
else if (parent->_bf == -2 && cur->_bf == -1)
{
RotateR(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;
}
// AVL树的验证
bool IsAVLTree()
{
return _IsAVLTree(_root);
}
void InOrder()
{
_InOrder(_root);
cout << endl;
}
private:
void _InOrder(Node* root)
{
if (root == nullptr)
{
return;
}
_InOrder(root->_left);
cout << root->_kv.second << " ";
_InOrder(root->_right);
}
int _Height(Node* root)
{
if (root == nullptr)
{
return 0;
}
return max(_Height(root->_left), _Height(root->_right)) + 1;
}
bool _IsAVLTree(Node* root)
{
//空数也是AVL树
if (nullptr == root)
{
return true;
}
//计算_root结点的平衡因子
int leftHeight = _Height(root->_left);
int rightHeight = _Height(root->_right);
int diff = rightHeight - leftHeight;
//判断
if (abs(diff) >= 2)
{
cout << root->_kv.first << "结点平衡因子异常" << endl;
return false;
}
else if (diff != root->_bf)
{
cout << root->_kv.first << "结点平衡因子异常" << endl;
return false;
}
return _IsAVLTree(root->_left) && _IsAVLTree(root->_right);
}
void RotateRL(Node* parent) //右左旋转
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
int bf = subRL->_bf;
RotateR(parent->_right);
RotateL(parent);
//更新平衡因子
if (bf == 0)
{
subRL->_bf = 0;
subR->_bf = 0;
parent->_bf = 0;
}
else if (bf == 1)
{
subRL->_bf = 0;
subR->_bf = 0;
parent->_bf = -1;
}
else if (bf == -1)
{
subRL->_bf = 0;
subR->_bf = 1;
parent->_bf = 0;
}
else
{
//旋转前就有问题
assert(false);
}
}
void RotateLR(Node* parent) //左右旋转
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
int bf = subLR->_bf;
RotateL(parent->_left);
RotateR(parent);
//更新平衡因子
if (bf == 0)
{
subLR->_bf = 0;
subL->_bf = 0;
parent->_bf = 0;
}
else if (bf == 1)
{
subLR->_bf = 0;
subL->_bf = -1;
parent->_bf = 0;
}
else if (bf == -1)
{
subLR->_bf = 0;
subL->_bf = 0;
parent->_bf = 1;
}
else
{
//旋转前就有问题
assert(false);
}
}
void RotateR(Node* parent) //右旋
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
parent->_left = subLR;
if (subLR)
{
subLR->_parent = parent;
}
Node* ppNode = parent->_parent;
subL->_right = parent;
parent->_parent = subL;
if (_root == parent)
{
_root = subL;
_root->_parent = nullptr;
}
else
{
if (ppNode->_left == parent)
{
ppNode->_left = subL;
}
else
{
ppNode->_right = subL;
}
subL->_parent = ppNode;
}
//更新平衡因子
parent->_bf = 0;
subL->_bf = 0;
}
void RotateL(Node* parent) //左旋
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
parent->_right = subRL;
if (subRL)
{
subRL->_parent = parent;
}
Node* ppNode = parent->_parent;
subR->_left = parent;
parent->_parent = subR;
if (parent == _root)
{
_root = subR;
_root->_parent = nullptr;
}
else
{
if (ppNode->_left == parent)
{
ppNode->_left = subR;
}
else //ppNode->_right == parent
{
ppNode->_right = subR;
}
subR->_parent = ppNode;
}
//更新平衡因子
parent->_bf = 0;
subR->_bf = 0;
}
Node* _root = nullptr;
};
void TectAVLTree1()
{
int a[] = { 16, 3, 7, 11, 9, 26, 18, 14, 15 };
//int a[] = { 4, 2, 6, 1, 3, 5, 15, 7, 16, 14 };
//int a[] = { 1,2,3,4,5,6,7,8,9 };
AVLTree<int, int> t;
for (auto e : a)
{
t.Insert(make_pair(e, e));
}
t.InOrder();
if (t.IsAVLTree())
cout << "ture" << endl;
else
cout << "false" << endl;
}
