目录
1.什么是AVL树
AVL树(Adelson-Velsky and Landis Tree)是一种自平衡的二叉查找树(Binary Search Tree, BST),它的特点是每个节点的左子树和右子树的高度差(称为平衡因子)不能超过1。AVL树是由俄罗斯数学家Adelson-Velsky和Landis于1962年提出的。
1.1 树的结构
AVL树与普通的二叉搜索树一样,满足以下两个条件:
1. 二叉查找树性质:对于树中的任意一个节点,左子树的所有节点的值小于该节点的值,右子树的所有节点的值大于该节点的值。
2. 平衡性:对于树中的每个节点,左子树和右子树的高度差的绝对值(即平衡因子)不能大于1。
1.2平衡因子的引入
对于树中某个节点 ( N ),其平衡因子 ( BF(N) ) 定义为:
BF(N) = 右子树高度-左子树高度
- 如果 BF(N) = 0 ,表示该节点的左右子树高度相等。
- 如果 BF(N) = 1 ,表示该节点的左子树比右子树高1。
- 如果 BF(N) = -1 ,表示该节点的右子树比左子树高1。
- 如果 ( |BF(N)| > 1 ),表示该节点的子树不平衡,需进行旋转操作来恢复平衡。
1.3 旋转操作的大致说明
为了保持AVL树的平衡性,当插入或删除节点后,树可能会失去平衡。此时需要通过旋转操作来恢复平衡。常见的旋转操作有四种:
1. 右旋转(Single Rotation, Right Rotation):适用于左子树过高(左重)的情况。
2. 左旋转(Single Rotation, Left Rotation):适用于右子树过高(右重)的情况。
3. 左-右旋转(Double Rotation, Left-Right Rotation): 适用于左子树的右子树过高的情况。
4. 右-左旋转(Double Rotation, Right-Left Rotation): 适用于右子树的左子树过高的情况。
1.4 插入与删除操作
- 插入操作:当一个新节点插入AVL树时,首先按照二叉查找树的方式插入节点。然后,通过遍历树的路径来检查是否有节点失衡,如果有,进行相应的旋转操作。
- 删除操作:删除节点后,可能导致树的不平衡,需要检查并恢复平衡,通常需要进行旋转操作。
平衡性与时间复杂度
- 插入、删除、查找操作的时间复杂度为 O(log n) ,其中 n 是树中节点的数量。由于AVL树保证了平衡,因此在最坏情况下,树的高度为 O(log n) ),使得这些操作的时间复杂度得到保证。
1.5AVL树的优点和缺点
优点:
- 相比普通的二叉查找树,AVL树提供了更稳定的查询时间,因为它保持了树的平衡性。
- 对于频繁进行查找操作的应用,AVL树的性能较好。
缺点:
- 由于在插入和删除操作后需要进行旋转操作,AVL树的插入和删除操作较为复杂。
- 相比于其他自平衡树(如红黑树),AVL树的维护成本稍高,因为它需要频繁检查并调整平衡。
部分图例展示
该图就是要进行旋转操作
2.AVL树的实现
2.1 AVL树的整体框架
AVL树的结构跟二叉搜索树几乎是类似的,我们需要添加一个int变量来记录平衡因子,且增加一个parent结点指针来辅助平衡因子的找寻和修改,在后续的旋转操作我们会认识到它的作用。
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
template<class K, class V>
struct AVLTreeNode {
pair<K, V> _kv;
AVLTreeNode<K, V>* _parent;
AVLTreeNode<K, V>* _left;
AVLTreeNode<K, V>* _right;
int _BF;
AVLTreeNode(const pair<K, V>& kv)
: _kv(kv), _parent(nullptr), _left(nullptr), _right(nullptr), _BF(0){}
};
template<class K,class V>
class AVLTree {
using Node = AVLTreeNode<K, V>;
public:
//AVL树的构建和操作
bool Insert(const pair<K, V>& kv) {}
void RotateR(Node* parent) {}
void RotateL(Node* parent){}
void RotateLR(Node* parent) {}
void RotateRL(Node* parent) {}
Node Find(const K& key){}
private:
Node* _root = nullptr;
};
int main() {
return 0;
}
2.2 AVL树的插入
2.2.1 AVL树插入一个值的过程
2.2.2 平衡因子更新
更新原则
更新停止条件
旋转的目标
1、把 parent子树旋转平衡。
2、降低parent子树的高度,恢复到插入结点以前的高度。所以旋转后也不需要继续往上更新,插入结束。
图例展示
更新到10结点,发现平衡因子变为2,破坏了10子树结构,需要进行旋转。
更新到中间结点,3为根的子树高度不变,不会影响上一层,更新结束
最坏的情况一直更新到根结点
2.2.3 结点插入和平衡因子的更新
当一个新节点插入AVL树时,首先按照二叉查找树的方式插入节点。然后,通过遍历树的路径来检查是否有节点失衡,如果有,进行相应的旋转操作。
先找到插入结点的位置,在进行插入,然后检查更新平衡因子,这里我们在前面定义的_parent的作用就逐渐体现出来了。
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 (kv.first < parent->_kv.first) {
parent->_left = cur;
}
else
parent->_right = cur;
//链接父亲,方便后续更新平衡因子
cur->_parent = parent;
//控制平衡,平衡因子更新
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 = parent->_parent;
}
else if (parent->_BF == 2 && parent->_BF == -2) {
//旋转处理
break;
}
else
return false;
}
}
2.3 旋转
2.3.1 旋转的原则
保持搜索树的规则,让旋转的树从不满足变平衡,其次降低旋转树的高度
旋转总共分为四种,左单旋/右单旋/左右双旋/右左双旋。
2.3.2 右单旋
图例文字描述
图展示的是10为根的树,有a/b/c抽象为三棵高度为h的子树(h>=0),a/b/c均符合AVL树的要 求。10可能是整棵树的根,也可能是一个整棵树中局部的子树的根。这里a/b/c是⾼度为h的子树, 是一种概括抽象表示,代表了所有右单旋的场景,实际右单旋形态有很多种。
右单旋代码
void RotateR(Node* parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
// 需要注意除了要修改孩⼦指针指向,还是修改⽗亲
parent->_left = subLR;
if (subLR)
subLR->_parent = parent;
Node* parentParent = parent->_parent;
subL->_right = parent;
parent->_parent = subL;
// parent有可能是整棵树的根,也可能是局部的⼦树
// 如果是整棵树的根,要修改_root
// 如果是局部的指针要跟上⼀层链接
if (parentParent == nullptr)
{
_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.3.3 左单旋
图例文字描述
左单旋代码
void RotateL(Node* parent){
Node* subR = parent->_right;
Node* subRL = subR->_left;
parent->_right = subRL;
if (subRL)
subRL->_parent = parent;
subR->_left = parent;
Node* parent_Parent = parent->_parent;
parent->_parent = subR;
if (parent_Parent == nullptr) {
_root = subR;
subR->_parent = nullptr;
}
else {
if (parent == parent_Parent->_left)
parent_Parent->_left = subR;
else
parent_Parent->_right = subR;
subR->_parent = parent_Parent;
}
parent->_BF = subR->_BF = 0;
}
2.3.4 左右双旋
图形文字描述
只进行了右旋的错误示范图
代码书写
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;
parent->_BF = 0;
subL->_BF = 0;
}
else if (bf == 1) {
subLR->_BF = 0;
parent->_BF = 0;
subL->_BF = -1;
}
else if (bf == -1) {
subLR->_BF = 0;
parent->_BF = 1;
subL->_BF = 0;
}
else
{
assert(false);
}
}
重点是平衡因子的更新
2.3.5 右左双旋
代码展示
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);
}
}
2.4 AVL树的查找
仿照二叉搜索树的逻辑进行查找,效率O(log n)
Node*Find(const K& key){
Node* cur = _root;
while (cur)
{
if (cur->_kv.first > key)
cur = cur->_left;
else if (cur->_kv.first < key)
cur = cur->_right;
else
return cur;
}
return nullptr;
}
2.5 AVL树平衡检测(了解)
bool _IsBalanceTree(Node* root)
{
// 空树也是AVL树
if (nullptr == root)
return true;
// 计算pRoot结点的平衡因子:即pRoot左右子树的高度差
int leftHeight = _Height(root->_left);
int rightHeight = _Height(root->_right);
int diff = rightHeight - leftHeight;
// 如果计算出的平衡因子与pRoot的平衡因子不相等,或者
// pRoot平衡因子的绝对值超过1,则一定不是AVL树
if (abs(diff) >= 2)
{
cout << root->_kv.first << "高度差异常" << endl;
return false;
}
if (root->_BF != diff)
{
cout << root->_kv.first << "平衡因子异常" << endl;
return false;
}
// pRoot的左和右如果都是AVL树,则该树一定是AVL树
return _IsBalanceTree(root->_left) && _IsBalanceTree(root->_right);
}
我们仿照前面二叉树的学习将高度和大小,中序遍历求解也写下
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 Left_height = _Height(root->_left);
int Right_height = _Height(root->_right);
return Left_height > Right_height ? Left_height + 1 : Right_height + 1;
}
int _Size(Node* root)
{
if (root == nullptr)
return 0;
return _Size(root->_left) + _Size(root->_right) + 1;
}
我们同样按照二叉搜索树一样是将这些函数封装在private中,在public中在定义函数调用这些函数,不然在外面传根节点有点麻烦。
3. 完整代码(附测试代码函数)
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <assert.h>
#include <vector>
using namespace std;
template<class K, class V>
struct AVLTreeNode {
pair<K, V> _kv;
AVLTreeNode<K, V>* _parent;
AVLTreeNode<K, V>* _left;
AVLTreeNode<K, V>* _right;
int _BF;
AVLTreeNode(const pair<K, V>& kv)
: _kv(kv), _parent(nullptr), _left(nullptr), _right(nullptr), _BF(0){}
};
template<class K,class V>
class AVLTree {
using Node = AVLTreeNode<K, V>;
public:
//AVL树的构建和操作
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 (kv.first < parent->_kv.first) {
parent->_left = cur;
}
else
parent->_right = cur;
//链接父亲,方便后续更新平衡因子
cur->_parent = parent;
//控制平衡,平衡因子更新
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 = 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)
RotateRL(parent);
else if (parent->_BF == -2 && cur->_BF == 1)
RotateLR(parent);
else
assert(false);
break;
}
else
return false;
}
return true;
}
void RotateR(Node* parent) {
Node* subL = parent->_left;
Node* subLR = subL->_right;
parent->_left = subLR;
if (subLR)
subLR->_parent = parent;
Node* parent_Parent = parent->_parent;
subL->_right = parent;
parent->_parent = subL;
//parent可能是整棵树的根,也可能是局部根
//局部根要向上链接,更新祖父的结点信息
if(parent_Parent==nullptr){
_root = subL;
subL->_parent = nullptr;
}
else {
if (parent_Parent->_left == parent)
parent_Parent->_left = subL;
else
parent_Parent->_right = subL;
subL->_parent = parent_Parent;
}
parent->_BF = subL->_BF = 0;
}
void RotateL(Node* parent) {
Node* subR = parent->_right;
Node* subRL = subR->_left;
parent->_right = subRL;
if (subRL)
subRL->_parent = parent;
subR->_left = parent;
Node* parent_Parent = parent->_parent;
parent->_parent = subR;
// 更新 parent_Parent 的子节点指针
if (parent_Parent == nullptr) {
_root = subR;
subR->_parent = nullptr;
}
else {
if (parent == parent_Parent->_left)
parent_Parent->_left = subR;
else
parent_Parent->_right = subR;
subR->_parent = parent_Parent;
}
parent->_BF = subR->_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;
parent->_BF = 0;
subL->_BF = 0;
}
else if (bf == 1) {
subLR->_BF = 0;
parent->_BF = 0;
subL->_BF = -1;
}
else if (bf == -1) {
subLR->_BF = 0;
parent->_BF = 1;
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);
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);
}
}
Node*Find(const K& key){
Node* cur = _root;
while (cur)
{
if (cur->_kv.first > key)
cur = cur->_left;
else if (cur->_kv.first < key)
cur = cur->_right;
else
return cur;
}
return nullptr;
}
void InOrder()
{
_InOrder(_root);
cout << endl;
}
int Height()
{
return _Height(_root);
}
int Size()
{
return _Size(_root);
}
bool IsBalanceTree()
{
return _IsBalanceTree(_root);
}
private:
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 Left_height = _Height(root->_left);
int Right_height = _Height(root->_right);
return Left_height > Right_height ? Left_height + 1 : Right_height + 1;
}
int _Size(Node* root)
{
if (root == nullptr)
return 0;
return _Size(root->_left) + _Size(root->_right) + 1;
}
bool _IsBalanceTree(Node* root)
{
// 空树也是AVL树
if (nullptr == root)
return true;
// 计算pRoot结点的平衡因子:即pRoot左右子树的高度差
int leftHeight = _Height(root->_left);
int rightHeight = _Height(root->_right);
int diff = rightHeight - leftHeight;
// 如果计算出的平衡因子与pRoot的平衡因子不相等,或者
// pRoot平衡因子的绝对值超过1,则一定不是AVL树
if (abs(diff) >= 2)
{
cout << root->_kv.first << "高度差异常" << endl;
return false;
}
if (root->_BF != diff)
{
cout << root->_kv.first << "平衡因子异常" << endl;
return false;
}
// pRoot的左和右如果都是AVL树,则该树一定是AVL树
return _IsBalanceTree(root->_left) && _IsBalanceTree(root->_right);
}
private:
Node* _root = nullptr;
};
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 test2() {
AVLTree<int, int> tree;
const int N = 100000;
vector<int> v;
v.reserve(N);
srand(time(0));
for (int i = 0; i < N; i++) {
v.push_back(rand() + i);
}
for (auto e : v) {
tree.Insert({ e, e });
}
size_t end2 = clock();
cout << tree.IsBalanceTree() << endl;
cout << "Height:" << tree.Height() << endl;
cout << "Size:" << tree.Size() << endl;
size_t begin1 = clock();
// 确定在的值
for (auto e : v)
{
tree.Find(e);
}
// 随机值
/*for (size_t i = 0; i < N; i++)
{
t.Find((rand() + i));
}*/
}
int main() {
//test2();
TestAVLTree1();
return 0;
}
测试部分我们用了随机的数据插入来验证AVL树更具有说服力。
结束语
AVL树是一种自平衡的二叉查找树,能够保证在插入、删除、查找等操作中的时间复杂度为 O(log n) 。通过平衡因子的控制和旋转操作,AVL树保持了树的平衡性,有效地避免了树变成链表的情况,从而提升了操作效率。
本节内容就到此结束了,感谢友友们的阅读和支持!!!