前言
上一篇的BST(二叉搜索树)的详解中,我们知道在极端情况,二叉搜索树有可能退化成单支树的,这极大的降低了查找效率。
因此如果通过某些办法让二叉树保持平衡的,就能优化二叉搜索树。
也就是二叉搜索平衡树。
概念
两位俄罗斯的数学家 G.M.Adelson-Velskii 和 E.M.Landis 在1962年发明了一种解决上述问题的方法:
当向二叉搜索树中插入新结点后,如果能保证每个结点的左右子树高度之差的绝对值不超过1(需要对树中的结点进行调整),即可降低树的高度,从而减少平均搜索长度。
一棵AVL树或者是空树,或者是具有以下性质的二叉搜索树:
- 它的左右子树都是AVL树
- 左右子树高度之差(简称平衡因子)的绝对值不超过1(-1/0/1)
如果一棵二叉搜索树是高度平衡的,它就是AVL树。
如果它有n个结点,其高度可保持在
O
(
l
o
g
2
n
)
O(log_2 n)
O(log2n),搜索时间复杂度
O
(
l
o
g
2
n
)
O(log_2 n)
O(log2n)。
对比:
- 满二叉树,不缺节点
- 完全二叉树,最后一层缺一些节点
- AVL二叉树,最后两层缺一些节点
insert
单旋
void RotateR(Node* parent)
{
Node* ppNode = parent->_parent;
Node* subL = parent->_left;
Node* subLR = subL->_right;
parent->_left = subLR;
parent->_parent = subL;
if (subLR != nullptr)
subLR->_parent = parent;
subL->_right = parent;
if (parent == _root) // parent可能为根节点 旋转后更新root
_root = subL, _root->_parent = nullptr;
else
{
if (parent == ppNode->_left)
ppNode->_left = subL;
else
ppNode->_right = subL;
subL->_parent = ppNode;
}
parent->_bf = 0, subL->_bf = 0; // 旋转完更新平衡因子
}
void RotateL(Node* parent)
{
Node* ppNode = parent->_parent;
Node* subR = parent->_right;
Node* subRL = subR->_left;
parent->_right = subRL;
parent->_parent = subR;
if (subRL != nullptr) // 30的右子树可能为空
subRL->_parent = parent;
subR->_left = parent;
if (parent == _root) // parent可能为根节点 旋转完成后要更新root
_root = subR, _root->_parent = nullptr;
else // 考虑清楚是子树的右还是左 要连接ppNode 和 subR
{
if (parent == ppNode->_left)
ppNode->_left = subR;
else
ppNode->_right = subR;
subR->_parent = ppNode;
}
parent->_bf = 0, subR->_bf = 0; // 旋转完更新平衡因子
}
Node* _root = nullptr;
};
双旋
void RotateRL(Node* parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
int bf = subRL->_bf;
RotateR(parent->_right);
RotateL(parent);
// 双旋完之后更新bf
if (bf == 0) // h == 0 的情况
subRL->_bf = 0, subR->_bf = 0, parent->_bf = 0;
else if (bf == -1) // h >= 1 的一种情况,新增节点再subRL的左边
subRL->_bf = 0, subR->_bf = 1, parent->_bf = 0;
else if (bf == 1) // h >= 1 的一种情况,新增节点再subRL的右边
subRL->_bf = 0, subR->_bf = 0, parent->_bf = -1;
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) // h == 0
subLR->_bf = 0, subL->_bf = 0, parent->_bf = 0;
else if (bf == 1) // 在subRL的右新增节点
subLR->_bf = 0, subL->_bf = -1, parent->_bf = 0;
else if (bf == -1) // 在subRL的左新增节点
subLR->_bf = 0, subL->_bf = 0, parent->_bf = 1;
else
assert(false);
}
完整代码:
bool Insert(const pair<K, V>& kv)
{
// 按照搜索树的规则插入,再利用balance factor判断并处理不平衡的情况
if (_root == nullptr)
{
_root = new Node(kv);
_root->_bf = 0;
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; // 注意是三叉链
// 更新balance factor
while (parent != nullptr) // 最远更新到根节点
{
if (cur == parent->_right) // parent的 bf必须更新
parent->_bf++;
else
parent->_bf--;
// 再判断是否要继续更新祖先的bf
if (parent->_bf == 0) // 1 or -1 --> 0 说明插入的节点填上了矮的那边
break; // 高度不变,更新结束
else if (parent->_bf == 1 || parent->_bf == -1) // 0 --> 1 or -1 插入节点导致一边变高了
cur = cur->_parent, parent = parent->_parent; // 子树高度变了 继续更新祖先
else if (parent->_bf == 2 || parent->_bf == -2) // 1 or -1 --> 2 or -2 插入节点导致本来高的那边更高了
{
// 子树不平衡了,需要旋转
if (parent->_bf == 2 && cur->_bf == 1) // 右边高 左单旋 说明cur在parent右
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); // 走到这说明插入之前AVL就不平衡了
}
return true;
}
判断平衡树
int _Height(Node* root)
{
if (root == nullptr)
return 0;
int lh = _Height(root->_left);
int rh = _Height(root->_right);
return lh > rh ? lh + 1 : rh + 1; // 左右子树高的+1
}
bool _IsBalanceTree(Node* root)
{
// 空树也是AVL树
if (nullptr == root)
return true;
// 计算root 节点的平衡因子
int leftHeight = _Height(root->_left);
int rightHeight = _Height(root->_right);
int diff = rightHeight - leftHeight;
// 如果计算出的平衡因子与root的平衡因子不相等,或者
// root 平衡因子的绝对值超过1,则一定不是AVL树
if (abs(diff) >= 2)
{
cout << root->_kv.first << "节点平衡因子异常" << endl;
return false;
}
if (diff != root->_bf)
{
cout << root->_kv.first << "节点平衡因子不符合实际" << endl;
return false;
}
// root的左和右如果都是AVL树,则该树一定是AVL树
return _IsBalanceTree(root->_left)
&& _IsBalanceTree(root->_right);
}
bool IsBalanceTree(){ return _IsBalanceTree(_root); }
int Height(){ return _Height(_root); }
测试
void TestAVLTree2()
{
//int a[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
int a[] = { 30,29,28,27,26,25,24,11,8,7,6,5,4,3,2,1 };
//int a[] = { 8,7,6,5,4,3,2,1 };
AVLTree<int, int> t;
for (auto e : a)
{
t.Insert(make_pair(e, e));
}
t.levelOrder();
/*
11
5 27
3 7 25 29
2 4 6 8 24 26 28 30
1
*/
// 较为有序
}
void TestAVLTree3()
{
const size_t N = 1024;
vector<int> v;
v.reserve(N);
srand(time(0));
for (size_t i = 0; i < N; ++i)
{
v.push_back(rand());
//v.push_back(i);
}
AVLTree<int, int> t;
for (auto e : v)
{
t.Insert(make_pair(e, e));
}
cout << "是否平衡?" << t.IsBalanceTree() << endl;
cout << "高度:" << t.Height() << endl;
//t.levelOrder();
}
注意:
平衡因子不是必须要维护的,在操作时也可以直接通过高度函数来算,只不过比较麻烦
源代码
#pragma once
#include <utility>
#include <assert.h>
#include <iostream>
#include <map>
#include <string>
#include <set>
#include <algorithm>
#include <vector>
#include <queue>
#include <time.h>
using namespace std;
namespace yzq
{
template<class K, class V>
struct AVLTreeNode
{
pair<K, V> _kv;
AVLTreeNode<K, V>* _left;
AVLTreeNode<K, V>* _right;
AVLTreeNode<K, V>* _parent;
// AVL树没有规定必须设计balance factor
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)
{
// 按照搜索树的规则插入,再利用balance factor判断并处理不平衡的情况
if (_root == nullptr)
{
_root = new Node(kv);
_root->_bf = 0;
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; // 注意是三叉链
// 更新balance factor
while (parent != nullptr) // 最远更新到根节点
{
if (cur == parent->_right) // parent的 bf必须更新
parent->_bf++;
else
parent->_bf--;
// 再判断是否要继续更新祖先的bf
if (parent->_bf == 0) // 1 or -1 --> 0 说明插入的节点填上了矮的那边
break; // 高度不变,更新结束
else if (parent->_bf == 1 || parent->_bf == -1) // 0 --> 1 or -1 插入节点导致一边变高了
cur = cur->_parent, parent = parent->_parent; // 子树高度变了 继续更新祖先
else if (parent->_bf == 2 || parent->_bf == -2) // 1 or -1 --> 2 or -2 插入节点导致本来高的那边更高了
{
// 子树不平衡了,需要旋转
if (parent->_bf == 2 && cur->_bf == 1) // 右边高 左单旋 说明cur在parent右
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); // 走到这说明插入之前AVL就不平衡了
}
return true;
}
vector<vector<int>> levelOrder() {
vector<vector<int>> vv;
if (_root == nullptr)
return vv;
queue<Node*> q;
int levelSize = 1;
q.push(_root);
while (!q.empty())
{
// levelSize控制一层一层出
vector<int> levelV;
while (levelSize--)
{
Node* front = q.front();
q.pop();
levelV.push_back(front->_kv.first);
if (front->_left)
q.push(front->_left);
if (front->_right)
q.push(front->_right);
}
vv.push_back(levelV);
for (auto e : levelV)
{
cout << e << " ";
}
cout << endl;
// 上一层出完,下一层就都进队列
levelSize = q.size();
}
return vv;
}
void InOrder()
{
_InOrder(_root);
cout << endl;
}
bool IsBalanceTree(){ return _IsBalanceTree(_root); }
int Height(){ return _Height(_root); }
private:
void RotateRL(Node* parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
int bf = subRL->_bf;
RotateR(parent->_right);
RotateL(parent);
// 双旋完之后更新bf
if (bf == 0) // h == 0 的情况
subRL->_bf = 0, subR->_bf = 0, parent->_bf = 0;
else if (bf == -1) // h >= 1 的一种情况,新增节点再subRL的左边
subRL->_bf = 0, subR->_bf = 1, parent->_bf = 0;
else if (bf == 1) // h >= 1 的一种情况,新增节点再subRL的右边
subRL->_bf = 0, subR->_bf = 0, parent->_bf = -1;
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) // h == 0
subLR->_bf = 0, subL->_bf = 0, parent->_bf = 0;
else if (bf == 1) // 在subRL的右新增节点
subLR->_bf = 0, subL->_bf = -1, parent->_bf = 0;
else if (bf == -1) // 在subRL的左新增节点
subLR->_bf = 0, subL->_bf = 0, parent->_bf = 1;
else
assert(false);
}
void RotateR(Node* parent)
{
Node* ppNode = parent->_parent;
Node* subL = parent->_left;
Node* subLR = subL->_right;
parent->_left = subLR;
parent->_parent = subL;
if (subLR != nullptr)
subLR->_parent = parent;
subL->_right = parent;
if (parent == _root) // parent可能为根节点 旋转后更新root
_root = subL, _root->_parent = nullptr;
else
{
if (parent == ppNode->_left)
ppNode->_left = subL;
else
ppNode->_right = subL;
subL->_parent = ppNode;
}
parent->_bf = 0, subL->_bf = 0; // 旋转完更新平衡因子
}
void RotateL(Node* parent)
{
Node* ppNode = parent->_parent;
Node* subR = parent->_right;
Node* subRL = subR->_left;
parent->_right = subRL;
parent->_parent = subR;
if (subRL != nullptr) // 30的右子树可能为空
subRL->_parent = parent;
subR->_left = parent;
if (parent == _root) // parent可能为根节点 旋转完成后要更新root
_root = subR, _root->_parent = nullptr;
else // 考虑清楚是子树的右还是左 要连接ppNode 和 subR
{
if (parent == ppNode->_left)
ppNode->_left = subR;
else
ppNode->_right = subR;
subR->_parent = ppNode;
}
parent->_bf = 0, subR->_bf = 0; // 旋转完更新平衡因子
}
void _InOrder(Node* root)
{
if (root == nullptr)
return;
_InOrder(root->_left);
cout << root->_kv.first << " ";
_InOrder(root->_right);
}
int _Height(Node* root)
{
if (root == nullptr)
return 0;
int lh = _Height(root->_left);
int rh = _Height(root->_right);
return lh > rh ? lh + 1 : rh + 1; // 左右子树高的+1
}
bool _IsBalanceTree(Node* root)
{
// 空树也是AVL树
if (nullptr == root)
return true;
// 计算root 节点的平衡因子
int leftHeight = _Height(root->_left);
int rightHeight = _Height(root->_right);
int diff = rightHeight - leftHeight;
// 如果计算出的平衡因子与root的平衡因子不相等,或者
// root 平衡因子的绝对值超过1,则一定不是AVL树
if (abs(diff) >= 2)
{
cout << root->_kv.first << "节点平衡因子异常" << endl;
return false;
}
if (diff != root->_bf)
{
cout << root->_kv.first << "节点平衡因子不符合实际" << endl;
return false;
}
// root的左和右如果都是AVL树,则该树一定是AVL树
return _IsBalanceTree(root->_left)
&& _IsBalanceTree(root->_right);
}
Node* _root = nullptr;
};
void TestAVLTree1()
{
AVLTree<int, int> t;
t.Insert(make_pair(1, 1));
t.Insert(make_pair(2, 2));
t.Insert(make_pair(3, 3));
t.Insert(make_pair(3, 3));
}
void TestAVLTree2()
{
//int a[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
int a[] = { 30,29,28,27,26,25,24,11,8,7,6,5,4,3,2,1 };
//int a[] = { 8,7,6,5,4,3,2,1 };
AVLTree<int, int> t;
for (auto e : a)
{
t.Insert(make_pair(e, e));
}
t.levelOrder();
/*
11
5 27
3 7 25 29
2 4 6 8 24 26 28 30
1
*/
// 较为有序
}
void TestAVLTree3()
{
const size_t N = 1024;
vector<int> v;
v.reserve(N);
srand(time(0));
for (size_t i = 0; i < N; ++i)
{
v.push_back(rand());
//v.push_back(i);
}
AVLTree<int, int> t;
for (auto e : v)
{
t.Insert(make_pair(e, e));
}
cout << "是否平衡?" << t.IsBalanceTree() << endl;
cout << "高度:" << t.Height() << endl;
//t.levelOrder();
}
}
尾声
🌹🌹🌹
写文不易,如果有帮助烦请点个赞~ 👍👍👍
Thanks♪(・ω・)ノ🌹🌹🌹
😘😘😘
👀👀由于笔者水平有限,在今后的博文中难免会出现错误之处,本人非常希望您如果发现错误,恳请留言批评斧正,希望和大家一起学习,一起进步ヽ( ̄ω ̄( ̄ω ̄〃)ゝ,期待您的留言评论。
附GitHub仓库链接