hello~ 很高兴见到大家! 这次带来的是C++中关于AVL树这部分的一些知识点,如果对你有所帮助的话,可否留下你宝贵的三连呢? 个 人 主 页: 默|笙


template<class K, class V>
struct AVLTreeNode
{
AVLTreeNode(const pair<K, V>& kv)
:_kv(kv)
,_left(nullptr)
,_right(nullptr)
,_parent(nullptr)
,_bf(0)
{ }
pair<K, V> _kv;
AVLTreeNode<K, V>* _left;
AVLTreeNode<K, V>* _right;
AVLTreeNode<K, V>* _parent;//指向父亲节点
int _bf;//平衡因子
};
template<class K, class V>
class AVLTree
{
typedef AVLTreeNode<K, V> Node;
```
private:
Node* _root = nullptr;//给缺省值,用编译器自动生成的默认构造函数
};
cpp平衡因子 = 右子树高度 - 左子树高度
右子树高度+1,平衡因子会+1
左子树高度+1,平衡因子会-1

插入节点更新平衡因子代码实现:
bool Insert(const pair<K, V>& kv)
{
//考虑空树的情况
if (_root == nullptr)
{
_root = new Node(kv);
}
else
{
Node* cur = _root;
Node* parent = nullptr;
while (cur)
{
if (cur->_kv.first > kv.first)
{
parent = cur;
cur = cur->_left;
}
else if (cur->_kv.first < kv.first)
{
parent = cur;
cur = cur->_right;
}
else
{
return false;
}
}
cur = new Node(kv);
if (parent->_kv.first > cur->_kv.first)
{
parent->_left = cur;
}
else
{
parent->_right = cur;
}
cur->_bf = 0;
cur->_parent = parent;//完成对节点的插入
//更新平衡因子
while (parent)
{
if (parent->_left == cur)
{
//是左子树就--
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;
}
}
}
}旋转分为四种针对四种不同的情况,分别是:
右旋转针对parent左子树高度比右子树高1,且parent左孩子的左子树的高度也比parent左孩子的右子树高1的情况。

接下来给出一段代码:
void RotateR(Node* parent)
{
//记录节点
Node* subL = parent->_left;
Node* subLR = subL->_right;
parent->_left = subLR;
subL->_right = parent;
parent->_bf = subL->_bf = 0;
}
右旋转代码:
void RotateR(Node* parent)
{
//记录节点
Node* subL = parent->_left;
Node* subLR = subL->_right;
Node* parentParent = parent->_parent;
//改变指针
parent->_left = subLR;
subL->_right = parent;
if (parentParent == nullptr)
{
_root = subL;
subL->_parent = nullptr;
}
else
{
if (parentParent->_left == parent)
{
parentParent->_left = subL;
}
else
{
parentParent->_right = subL;
}
subL->_parent = parentParent;
}
//避免subLR = nullptr出现空指针解引用的情况
if (subLR)
{
subLR->_parent = parent;
}
parent->_parent = subL;
//改变平衡因子
parent->_bf = subL->_bf = 0;
}左旋转针对parent右子树高度比左子树高1,且parent右孩子的右子树的高度也比parent右孩子的左子树高1的情况。

左旋转代码:
void RotateL(Node* parent)
{
//记录节点
Node* subR = parent->_right;
Node* subRL = subR->_left;
Node* parentParent = parent->_parent;
//改变指针指向
subR->_left = parent;
parent->_right = subRL;
if (parentParent == nullptr)
{
_root = subR;
subR->_parent = nullptr;
}
else
{
if (parentParent->_left == parent)
{
parentParent->_left = subR;
}
else
{
parentParent->_right = subR;
}
subR->_parent = parentParent;
}
if (subRL)
{
subRL->_parent = parent;
}
parent->_parent = subR;
//改变平衡因子
parent->_bf = subR->_bf = 0;
}左右双旋针对parent左子树高度比右子树高1,且parent左孩子的右子树高度比parent左孩子的左子树高1的情况。

左右双旋代码:
void RotateLR(Node* parent)
{
//记录一下节点
Node* subL = parent->_left;
Node* subLR = subL->_right;
int bf = subLR->_bf;
//左右双旋
RotateL(parent->_left);
RotateR(parent);
//改变平衡因子
if (bf == 0)
{
parent->_bf = 0;
subL->_bf = 0;
subLR->_bf = 0;
}
else if (bf == -1)
{
parent->_bf = 1;
subL->_bf = 0;
subLR->_bf = 0;
}
else if (bf == 1)
{
parent->_bf = 0;
subL->_bf = -1;
subLR->_bf = 0;
}
else
{
assert(false);
}
}右左双旋针对parent右子树高度比左子树高1,且parent的右孩子的左子树高度比parent右孩子的右子树高1的情况。

void RotateRL(Node* parent)
{
//记录一下节点
Node* subR = parent->_right;
Node* subRL = subR->_left;
int bf = subRL->_bf;
//右旋转和左旋转各一次
RotateR(parent->_right);
RotateL(parent);
//更新平衡因子
if (bf == 0)
{
parent->_bf = parent->_bf = subRL->_bf = 0;
}
else if (bf == 1)
{
subR->_bf = 0;
parent->_bf = -1;
subRL->_bf = 0;
}
else if (bf == -1)
{
subR->_bf = 1;
parent->_bf = 0;
subRL->_bf = 0;
}
else
{
assert(false);
}
}
};//算树的高度
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 RightHight = _Height(root->_right);
int bf = RightHight - LeftHeight;
if (bf != root->_bf)
{
cout << root->_kv.first << endl;
}
if (abs(bf) > 1)
{
return false;
}
return _IsBalanceTree(root->_left) && _IsBalanceTree(root->_right);
}#pragma once
#include<assert.h>
#include<iostream>
using namespace std;
template<class K, class V>
struct AVLTreeNode
{
AVLTreeNode(const pair<K, V>& kv)
:_kv(kv)
,_left(nullptr)
,_right(nullptr)
,_parent(nullptr)
,_bf(0)
{ }
pair<K, V> _kv;
AVLTreeNode<K, V>* _left;
AVLTreeNode<K, V>* _right;
AVLTreeNode<K, V>* _parent;
int _bf;
};
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);
}
else
{
Node* cur = _root;
Node* parent = nullptr;
while (cur)
{
if (cur->_kv.first > kv.first)
{
parent = cur;
cur = cur->_left;
}
else if (cur->_kv.first < kv.first)
{
parent = cur;
cur = cur->_right;
}
else
{
return false;
}
}
cur = new Node(kv);
if (parent->_kv.first > cur->_kv.first)
{
parent->_left = cur;
}
else
{
parent->_right = cur;
}
cur->_bf = 0;
cur->_parent = parent;//完成对节点的插入
//更新平衡因子
while (parent)
{
if (parent->_left == cur)
{
//是左子树就--
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 && parent->_left->_bf == -1)
{
//进行右旋转
RotateR(parent);
}
else if (parent->_bf == 2 && parent->_right->_bf == 1)
{
//进行左旋转
RotateL(parent);
}
else if (parent->_bf == -2 && parent->_left->_bf == 1)
{
//进行左右双旋
RotateLR(parent);
}
else if (parent->_bf == 2 && parent->_right->_bf == -1)
{
//进行右左双旋
RotateRL(parent);
}
else
{
//平衡因子出错
assert(false);
}
break;
}
else
{
//平衡因子出错
assert(false);
}
}
}
}
//中序遍历
void InOrder()
{
_Inorder(_root);
cout << endl;
}
//AVL树平衡检测
bool IsBalanceTree()
{
if (_IsBalanceTree(_root))
{
cout << "是平衡二叉树" << endl;
}
else
{
cout << "不是平衡二叉树" << endl;
}
return _IsBalanceTree(_root);
}
private:
//算树的高度
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 RightHight = _Height(_root->_right);
int bf = RightHight - LeftHeight;
if (bf != _root->_bf)
{
cout << root->_kv.first << endl;
}
if (abs(bf) > 1)
{
return false;
}
return _IsBalanceTree(root->_left) && _IsBalanceTree(root->_right);
}
void RotateR(Node* parent)
{
//记录节点
Node* subL = parent->_left;
Node* subLR = subL->_right;
Node* parentParent = parent->_parent;
//改变指针
parent->_left = subLR;
subL->_right = parent;
if (parentParent == nullptr)
{
_root = subL;
subL->_parent = nullptr;
}
else
{
if (parentParent->_left == parent)
{
parentParent->_left = subL;
}
else
{
parentParent->_right = subL;
}
subL->_parent = parentParent;
}
//避免subLR = nullptr出现空指针解引用的情况
if (subLR)
{
subLR->_parent = parent;
}
parent->_parent = subL;
//改变平衡因子
parent->_bf = subL->_bf = 0;
}
void RotateL(Node* parent)
{
//记录节点
Node* subR = parent->_right;
Node* subRL = subR->_left;
Node* parentParent = parent->_parent;
//改变指针指向
subR->_left = parent;
parent->_right = subRL;
if (parentParent == nullptr)
{
_root = subR;
subR->_parent = nullptr;
}
else
{
if (parentParent->_left == parent)
{
parentParent->_left = subR;
}
else
{
parentParent->_right = subR;
}
subR->_parent = parentParent;
}
if (subRL)
{
subRL->_parent = parent;
}
parent->_parent = subR;
//改变平衡因子
parent->_bf = subR->_bf = 0;
}
void RotateLR(Node* parent)
{
//记录一下节点
Node* subL = parent->_left;
Node* subLR = subL->_right;
//左右双旋
RotateL(parent->_left);
RotateR(parent);
//改变平衡因子
if (subLR->_bf == 0)
{
parent->_bf = 0;
subL->_bf = 0;
subLR->_bf = 0;
}
else if (subLR->_bf == -1)
{
parent->_bf = 1;
subL->_bf = 0;
subLR->_bf = 0;
}
else if (subLR->_bf == 1)
{
parent->_bf = 0;
subL->_bf = -1;
subLR->_bf = 0;
}
else
{
assert(false);
}
}
void RotateRL(Node* parent)
{
//记录一下节点
Node* subR = parent->_right;
Node* subRL = subR->_left;
//右旋转和左旋转各一次
RotateR(parent->_right);
RotateL(parent);
//更新平衡因子
if (subRL->_bf == 0)
{
parent->_bf = parent->_bf = 0;
}
else if (subRL->_bf == 1)
{
subR->_bf = 0;
parent->_bf = -1;
}
else if (subRL->_bf == -1)
{
subRL->_bf = 1;
parent->_bf = 0;
}
else
{
assert(false);
}
}
//中序遍历
void _Inorder(const Node* root)
{
if (root == nullptr)
{
return;
}
_Inorder(root->_left);
cout << root->_kv.first << " ";
_Inorder(root->_right);
}
Node* _root = nullptr;
};今天的分享就到此结束啦,如果对读者朋友们有所帮助的话,可否留下宝贵的三连呢~~ 让我们共同努力, 一起走下去!