本博客梳理AVL树基本性质与底层原理
一、AVL树的概念
1.定义
AVL树是一棵空树,或者是:左右子树都是AVL树,且左右子树高度差的绝对值不超过1
2.平衡因子
任何节点的平衡因子 = 右子树高度 - 左子树高度
3.增删查改效率:O(logN)
二、AVL树底层原理
1.AVL树的结构
template<class K, class V>
struct AVLTreeNode
{
pair<K, V> _kv;
AVLTreeNode<K, V>* _left;
AVLTreeNode<K, V>* _right;
AVLTreeNode<K, V>* _parent;
int _bf;//平衡因子balance factor
AVLTreeNode(const pair<K, V>& kv)
:_kv(kv)
, _left(nullptr)
, _right(nullptr)
, _parent(nullptr)
, _bf(0)
{ }
};
template<class K, class V>
class AVLTree
{
typedef AVLTreeNode<K, V> Node;
public:
//...
private:
Node* _root = nullptr;
};
2.AVL树的插入
(1)插入的过程
①按二叉搜索树规则插入
②新增一个节点,一定只影响祖先的平衡因子(不一定是所有祖先),所以要更新“新增节点->根节点”路径上的平衡因子
③更新过程中如果出现不平衡,则旋转处理,没出现则更新完插入就结束了
(2)平衡因子更新
①更新原则:
- 新增节点在parent的右子树,parent的平衡因子++;在parent的左子树,parent的平衡因子- -
- parent所在子树高度是否变化决定是否会向上更新
②更新停止条件:
-
更新后parent的平衡因子为0:说明新增节点插入在低的那边,更新结束
-
更新后parent的平衡因子为1/-1:parent所在子树符合要求,但再往上是否符合要求不确定,要继续向上更新

-
更新后parent的平衡因子为2/-2:旋转处理,旋转完之后不需要继续向上更新

bool Insert(const pair<K, V>& kv)
{
if (_root == nullptr)
{
_root = new Node(kv);
return true;
}
//1.走二叉搜索树的逻辑,确定插入位置
Node* cur = _root;
Node* parent = nullptr;
while (cur)
{
if (kv.first < cur->_kv.first)
{
parent = cur;
cur = cur->_left;
}
else if (kv.first > cur->_kv.first)
{
parent = cur;
cur = cur->_right;
}
else
return false;
}
//此时cur指向一个空位置
cur = new Node(kv);
if (kv.first < parent->_kv.first)
parent->_left = cur;
else
parent->_right = cur;
cur->_parent = parent;
cur->_bf = 0;
//2.更新平衡因子
while (parent)
{
if (cur == parent->_left)
parent->_bf--;
else
parent->_bf++;
if (parent->_bf == 0)//结束更新
break;
else if (parent->_bf == 1 || parent->_bf == -1)//继续向上更新
{
cur = parent;
parent = cur->_parent;
}
else if (parent->_bf == 2 || parent->_bf == -2)//不平衡,旋转处理
{
//...
break;
}
else
{
cout << "插入出错" << endl;
assert(false);
}
}
}
3.AVL树的旋转
旋转的原则:首先要保持二叉搜索树的规则,其次让旋转过后的树变得平衡,降低树的高度,因此旋转之后不需要再向上更新
(1)右单旋
左边太高了,要把左边的高度降一下

void RotateR(Node* parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
Node* parentParent = parent->_parent;
if (subLR)
subLR->_parent = parent;
parent->_left = subLR;
subL->_right = parent;
parent->_parent = subL;
if (parent == _root)
{
_root = subL;
subL->_parent = nullptr;
}
else
{
if (parent == parentParent->_left)
parentParent->_left = subL;
else
parentParent->_right = subL;
subL->_parent = parentParent;
}
parent->_bf = subL->_bf = 0;
}
(2)左单旋
与右单旋相反,右边太高了,要把右边的高度降一下

void RotateL(Node* parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
Node* parentParent = parent->_parent;
if (subRL)
subRL->_parent = parent;
parent->_right = subRL;
subR->_left = parent;
parent->_parent = subR;
if (_root == parent)
{
_root = subR;
subR->_parent = nullptr;
}
else
{
if (parent == parentParent->_left)
parentParent->_left = subR;
else
parentParent->_right = subR;
subR->_parent = parentParent;
}
parent->_bf = subR->_bf = 0;
}
(3)左右双旋

- 插入在b的左树和b的右树不同点:对平衡因子的影响不同
- 怎么区分在e插入还是在f插入?——观察subLR平衡因子的变化
subLR->_bf == 0:自己就是新增节点
subLR->_bf == -1:在e插入
subLR->_bf == 1:在f插入 - 旋转完成之后,单独更新subLR,subL,parent的平衡因子

void RotateLR(Node* parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
int subLR_bf = subLR->_bf;
RotateL(subL);
RotateR(parent);
if (subLR_bf == 0)//说明自己就是新增节点
{
subLR_bf = subL->_bf = parent->_bf = 0;
}
else if (subLR_bf == -1)//在e插入
{
subL->_bf = subLR->_bf = 0;
parent->_bf = 1;
}
else if(subLR_bf == 1)//在f插入
{
subL->_bf = -1;
subLR->_bf = parent->_bf = 0;
}
else
{
cout << "左右双旋出错" << endl;
assert(false);
}
}
(4)右左双旋
与左右双旋类似,读者可自行画图分析,代码如下:
void RotateRL(Node* parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
int subRL_bf = subRL->_bf;
RotateR(subR);
RotateL(parent);
if (subRL_bf == 0)
{
parent->_bf = subR->_bf = subRL->_bf = 0;
}
else if (subRL_bf == -1)//说明在e插入
{
subR->_bf = 1;
subRL->_bf = parent->_bf = 0;
}
else if (subRL_bf == 1)//说明在f插入
{
subR->_bf = subRL->_bf = 0;
parent->_bf = -1;
}
else
{
cout << "右左双旋出错" << endl;
assert(false);
}
}
至此,AVL树核心原理及代码模拟已经全部梳理完毕,下面给出一份更为完善的AVL树模拟实现代码,增加了查找,中序遍历,平衡检测等功能,提供测试代码,供读者参考
//AVLTree.h
#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
template<class K, class V>
struct AVLTreeNode
{
pair<K, V> _kv;
AVLTreeNode<K, V>* _left;
AVLTreeNode<K, V>* _right;
AVLTreeNode<K, V>* _parent;
int _bf;//平衡因子balance factor
AVLTreeNode(const pair<K, V>& kv)
:_kv(kv)
, _left(nullptr)
, _right(nullptr)
, _parent(nullptr)
, _bf(0)
{ }
};
template<class K, class V>
class AVLTree
{
typedef AVLTreeNode<K, V> Node;
public:
bool Insert(const pair<K, V>& kv)
{
if (_root == nullptr)
{
_root = new Node(kv);
return true;
}
//1.走二叉搜索树的逻辑,确定插入位置
Node* cur = _root;
Node* parent = nullptr;
while (cur)
{
if (kv.first < cur->_kv.first)
{
parent = cur;
cur = cur->_left;
}
else if (kv.first > cur->_kv.first)
{
parent = cur;
cur = cur->_right;
}
else
return false;
}
//此时cur指向一个空位置
cur = new Node(kv);
if (kv.first < parent->_kv.first)
parent->_left = cur;
else
parent->_right = cur;
cur->_parent = parent;
cur->_bf = 0;
//2.更新平衡因子
while (parent)
{
if (cur == parent->_left)
parent->_bf--;
else
parent->_bf++;
if (parent->_bf == 0)//结束更新
break;
else if (parent->_bf == 1 || parent->_bf == -1)//继续向上更新
{
cur = parent;
parent = cur->_parent;
}
else if (parent->_bf == 2 || parent->_bf == -2)//不平衡,旋转处理
{
//旋转处理,分右单旋,左单旋,左右双旋,右左双旋
if (parent->_bf == -2 && cur->_bf == -1)//右单旋
RotateR(parent);
else if (parent->_bf == 2 && cur->_bf == 1)//左单旋
RotateL(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
{
cout << "插入出错" << endl;
assert(false);
}
}
}
void InOrder()
{
_InOrder(_root);
}
bool IsBalanceTree()
{
return _IsBalanceTree(_root);
}
int Height()
{
return _Height(_root);
}
int Size()
{
return _Size(_root);
}
Node* Find(const K& key)
{
Node* cur = _root;
while (cur)
{
if (cur->_kv.first < key)
{
cur = cur->_right;
}
else if (cur->_kv.first > key)
{
cur = cur->_left;
}
else
{
return cur;
}
}
return nullptr;
}
private:
void RotateR(Node* parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
Node* parentParent = parent->_parent;
if (subLR)
subLR->_parent = parent;
parent->_left = subLR;
subL->_right = parent;
parent->_parent = subL;
if (parent == _root)
{
_root = subL;
subL->_parent = nullptr;
}
else
{
if (parent == parentParent->_left)
parentParent->_left = subL;
else
parentParent->_right = subL;
subL->_parent = parentParent;
}
parent->_bf = subL->_bf = 0;
}
void RotateL(Node* parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
Node* parentParent = parent->_parent;
if (subRL)
subRL->_parent = parent;
parent->_right = subRL;
subR->_left = parent;
parent->_parent = subR;
if (_root == parent)
{
_root = subR;
subR->_parent = nullptr;
}
else
{
if (parent == parentParent->_left)
parentParent->_left = subR;
else
parentParent->_right = subR;
subR->_parent = parentParent;
}
parent->_bf = subR->_bf = 0;
}
void RotateLR(Node* parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
int subLR_bf = subLR->_bf;
RotateL(subL);
RotateR(parent);
if (subLR_bf == 0)//说明自己就是新增节点
{
subLR_bf = subL->_bf = parent->_bf = 0;
}
else if (subLR_bf == -1)//在e插入
{
subL->_bf = subLR->_bf = 0;
parent->_bf = 1;
}
else if(subLR_bf == 1)//在f插入
{
subL->_bf = -1;
subLR->_bf = parent->_bf = 0;
}
else
{
cout << "左右双旋出错" << endl;
assert(false);
}
}
void RotateRL(Node* parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
int subRL_bf = subRL->_bf;
RotateR(subR);
RotateL(parent);
if (subRL_bf == 0)
{
parent->_bf = subR->_bf = subRL->_bf = 0;
}
else if (subRL_bf == -1)//说明在e插入
{
subR->_bf = 1;
subRL->_bf = parent->_bf = 0;
}
else if (subRL_bf == 1)//说明在f插入
{
subR->_bf = subRL->_bf = 0;
parent->_bf = -1;
}
else
{
cout << "右左双旋出错" << endl;
assert(false);
}
}
void _InOrder(Node* root)
{
if (root == nullptr)
return;
_InOrder(root->_left);
cout << root->_kv.first << ":" << root->_kv.second << endl;
_InOrder(root->_right);
}
int _Height(Node* root)
{
if (root == nullptr)
return 0;
int leftHeight = _Height(root->_left);
int rightHeight = _Height(root->_right);
return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
}
bool _IsBalanceTree(Node* root)
{
if (root == nullptr)
return true;
int leftHeight = _Height(root->_left);
int rightHeight = _Height(root->_right);
int diff = rightHeight - leftHeight;
if (abs(diff) >= 2)
{
cout << "高度差异常" << endl;
return false;
}
if (root->_bf != diff)//平衡因子可能是负数
{
cout << "根部平衡因子异常" << endl;
return false;
}
return _IsBalanceTree(root->_left) && _IsBalanceTree(root->_right);
}
int _Size(Node* root)
{
if (root == nullptr)
return 0;
return _Size(root->_left) + _Size(root->_right) + 1;
}
private:
Node* _root = nullptr;
};
//test.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include"AVLTree.h"
#include<vector>
void TestAVLTree1()
{
AVLTree<int, int> t;
// 常规的测试⽤例
// int a[] = { 16, 3, 7, 11, 9, 26, 18, 14, 15 };
// 特殊的带有双旋场景的测试⽤例
int a[] = { 4, 2, 6, 1, 3, 5, 15, 7, 16, 14 };
for (auto e : a)
{
t.Insert({ e, e });
}
t.InOrder();
cout << t.IsBalanceTree() << endl;
}
void TestAVLTree2()
{
const int N = 100000;
vector<int> v;
v.reserve(N);
srand(time(0));
for (size_t i = 0; i < N; i++)
{
v.push_back(rand() + i);
}
size_t begin2 = clock();
AVLTree<int, int> t;
for (auto e : v)
{
t.Insert(make_pair(e, e));
}
size_t end2 = clock();
cout << "Insert:" << end2 - begin2 << endl;
cout << t.IsBalanceTree() << endl;
cout << "Height:" << t.Height() << endl;
cout << "Size:" << t.Size() << endl;
size_t begin1 = clock();
for (size_t i = 0; i < N; i++)
{
t.Find((rand() + i));
}
size_t end1 = clock();
cout << "Find:" << end1 - begin1 << endl;
}
int main()
{
//TestAVLTree1();
TestAVLTree2();
return 0;
}

被折叠的 条评论
为什么被折叠?



