AVL树实际上一个引入了平衡因子的二叉搜索树,该平衡因子保证了每个节点的左右子树高度之差的绝对值不超过1,这样就可以降低树的高度,减少平均搜索长度。
一棵AVL树或者是空树,或者是具有以下性质的二叉搜索树:
1.它的左右子树都是AVL树
2.左右子树高度之差(平衡因子)的绝对值不超过1(-1/0/1)
如果一棵二叉搜索树是高度平衡的,他就是AVL树,如果它有n个结点,那么它的高度就是log2n,搜索时间复杂度O(log2n).
1.按照二叉搜索树的方式插入新节点
2.调整节点的平衡因子
pCur插入后,pParent的平衡因子需要调整,插入之前,pParent的平衡因子分为三种情况-1 0 1
1.如果pCur插入到pParent的左侧,只需要给pParent的平衡因子-1
2.如果pCur插入到pParent的右侧,只需要给pParent的平衡因子+1插入后的平衡因子会有下面三种情况 0 正负1 正负2
1.如果平衡因子为0,说明插入前平衡因子为正负1,插入后满足性质
2.如果平衡因子为正负,说明插入前平衡因子为0,插入后以pParent为根的高度增加,需要继续向上更新
3.如果平衡因子为正负2,违反了AVL树的性质,需要进行旋转处理
当插入节点后造成了不平衡也就是平衡因子为正负2,此时就需要进行旋转。
旋转分为4种情况:
1.新节点插入较高左子树的左侧 --左左:右单旋
2.新节点插入较高右子树的右侧 --右右:左单旋
参考第一种情况
3.新节点插入较高左子树的右侧 --左右:先左单旋再右单旋
4.新节点插入较高右子树的左侧 --右左:先右单旋再左单旋
参考第三种情况图形
1.验证为二叉搜索树
看中序遍历可以得到一个有序的序列,就说明为二叉搜索树
2.验证为平衡树
(1)每个节点子树高度差的绝对值不超过1(这是没有引入平衡因子)
(2)节点的平衡因子是否正确
AVLTree.h
#pragma once
#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; // balance factor
AVLTreeNode(const pair<K, V>& kv)
:_left(nullptr)
, _right(nullptr)
, _parent(nullptr)
, _kv(kv)
, _bf(0)
{}
};
template<class K, class V>
struct AVLTree
{
typedef AVLTreeNode<K, V> Node;
public:
bool Insert(const pair<K, V>& kv)
{
if (_root == nullptr)
{
_root = new Node(kv);
return true;
}
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;
// 控制平衡
// 1、更新平衡因子
while (parent)
{
if (cur == parent->_right)
{
parent->_bf++;
}
else
{
parent->_bf--;
}
if (parent->_bf == 0)
{
break;
}
else if (abs(parent->_bf) == 1)
{
parent = parent->_parent;
cur = cur->_parent;
}
else if (abs(parent->_bf) == 2)
{
// 说明parent所在子树已经不平衡了,需要旋转处理
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);
}
else
{
assert(false);
}
break;
}
else
{
assert(false);
}
}
return true;
}
void InOrder()
{
_InOrder(_root);
cout << endl;
}
bool IsBalance()
{
return _IsBalance(_root);
}
private:
bool _IsBalance(Node* root)
{
if (root == nullptr)
{
return true;
}
int leftHT = Height(root->_left);
int rightHT = Height(root->_right);
int diff = rightHT - leftHT;
if (diff != root->_bf)
{
cout << root->_kv.first << "平衡因子异常" << endl;
return false;
}
return abs(diff) < 2
&& _IsBalance(root->_left)
&& _IsBalance(root->_right);
}
int Height(Node* root)
{
if (root == nullptr)
return 0;
return max(Height(root->_left), Height(root->_right)) + 1;
}
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 (_root == parent)
{
_root = subR;
subR->_parent = nullptr;
}
else
{
if (ppNode->_left == parent)
{
ppNode->_left = subR;
}
else
{
ppNode->_right = subR;
}
subR->_parent = ppNode;
}
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;
}
Node* ppNode = parent->_parent;
subL->_right = parent;
parent->_parent = subL;
if (_root == parent)
{
_root = subL;
subL->_parent = nullptr;
}
else
{
if (ppNode->_left == parent)
{
ppNode->_left = subL;
}
else
{
ppNode->_right = subL;
}
subL->_parent = ppNode;
}
subL->_bf = parent->_bf = 0;
}
void RotateLR(Node* parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
int bf = subLR->_bf;
RotateL(parent->_left);
RotateR(parent);
subLR->_bf = 0;
if (bf == 1)
{
parent->_bf = 0;
subL->_bf = -1;
}
else if (bf == -1)
{
// 错的
/*parent->_bf = 0;
subL->_bf = 1;*/
parent->_bf = 1;
subL->_bf = 0;
}
else if (bf == 0)
{
parent->_bf = 0;
subL->_bf = 0;
}
else
{
assert(false);
}
}
void RotateRL(Node* parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
int bf = subRL->_bf;
RotateR(parent->_right);
RotateL(parent);
subRL->_bf = 0;
if (bf == 1)
{
subR->_bf = 0;
parent->_bf = -1;
}
else if (bf == -1)
{
subR->_bf = 1;
parent->_bf = 0;
}
else if (bf == 0)
{
parent->_bf = 0;
subR->_bf = 0;
}
else
{
assert(false);
}
}
void _InOrder(Node* root)
{
if (root == nullptr)
{
return;
}
_InOrder(root->_left);
cout << root->_kv.first << ":" << root->_kv.second << endl;
_InOrder(root->_right);
}
private:
Node* _root = nullptr;
};
test.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include"AVLTree.h"
void TestAVLTree1()
{
int a[] = { 4, 2, 6, 1, 3, 5, 15, 7, 16, 14 }; // 测试双旋平衡因子调节
//int a[] = { 16, 3, 7, 11, 9, 26, 18, 14, 15 };
AVLTree<int, int> t1;
for (auto e : a)
{
t1.Insert(make_pair(e, e));
}
t1.InOrder();
cout << "IsBalance:" << t1.IsBalance() << endl;
}
//验证AVLTree
void TestAVLTree2()
{
size_t N = 10000;
srand(time(0));
AVLTree<int, int> t1;
for (size_t i = 0; i < N; ++i)
{
int x = rand();
t1.Insert(make_pair(x, i));
/* bool ret = t1.IsBalance();
if (ret == false)
{
int u = 1;
}
else
{
cout << "Insert:" << x << " IsBalance:" <
}
cout << "IsBalance:" << t1.IsBalance() << endl;
}
int main()
{
//TestAVLTree1();
TestAVLTree2();
return 0;
}
这就是AVL树的一些介绍,但并没有写上AVL树的删除是因为AVL树删除一个结点将要更新平衡因子到根节点,效率不高,不常见。
AVL树是一棵绝对平衡的二叉搜索树,它的每个结点的左右子树高度差的绝对值都不超过1,这就保证了它的时间复杂度时log(N)。
但是对AVL树的结构修改效率不高,因为要保证平衡而进行旋转。