AVLTree
平衡搜索树:AVL树又称为高度平衡的二叉搜索树
性质:
1. 左子树和右子树的高度之差的绝对值不超过1
2. 树中的每个左子树和右子树都是AVL树
3. 每个节点都有一个平衡因子(balance factor–bf),任一节点的平衡因子是-1,0,1。(每个节点的平衡因子等于右子树的高度减去左子树的高度)
4.左孩子的值<父节点<右孩子的值
一棵AVL树有N个节点,其高度可以保持在lgN,插入/删除/查找的时间复杂度也是lgN。每次的改变都要更新树。下面我们主要讨论下AVL树的插入——
1.不做改变
2.左单旋
3. 右单旋
4.左右双旋
5.右左双旋
绘图功底有限(图略)看代码
代码实现
#pragma once
#include <iostream>
#include <cassert>
using namespace std;
template<class K, class V> // k = key 键 v = value 值
struct AVLTreeNode //结构体构造节点 三叉链
{
K _key;
V _value;
AVLTreeNode<K, V>* _left;
AVLTreeNode<K, V>* _right;
AVLTreeNode<K, V>* _parent;
int _bf; // 平衡因子
AVLTreeNode(const K& key, const V& value) // 拷贝构造
:_left(NULL)
, _right(NULL)
, _parent(NULL)
, _key(key)
, _value(value)
, _bf(0)
{}
};
template<class K, class V>
class AVLTree
{
typedef AVLTreeNode<K, V> Node; //重命名 类中类
public:
AVLTree()
:_root(NULL)
{}
bool Insert(const K& key, const V& value) //插入节点
{
// 树是否存在
if (_root == NULL)
{
_root = new Node(key, value);
return true;
}
Node* parent = NULL; // 根
// 树是存在
Node* cur = _root;
while (cur) //寻找节点应该去的位置
{
if (cur->_key > key) // 小于根 往左走
{
parent = cur;
cur = cur->_left;
}
else if (cur->_key < key) // 大于根 往右走
{
parent = cur;
cur = cur->_right;
}
else
{
return false;
}
}
cur = new Node(key, value); // cur的位置即为应在位置
//完成新节点三叉链的链接
if (parent->_key < key)
{
parent->_right = cur;
cur->_parent = parent;
}
else
{
parent->_left = cur;
cur->_parent = parent;
}
// 调平衡
// 1.更新AVL整棵树的平衡因子
while (parent)//插入节点的父节点
{
// 更新parent的平衡因子
if (cur == parent->_right)
parent->_bf++; //bf = 右高度 - 左高度
else
parent->_bf--;
// 递归往上
// cur没有兄弟
if (parent->_bf == -1 || parent->_bf == 1)//bf取值范围 -2,-1,0,1,2
{
cur = parent;
parent = parent->_parent;
}
else if (parent->_bf == 0) // 有兄弟 其余平衡因子不变
{
break;
}
else // -2/2
{
// 旋转
if (parent->_bf == 2)
{
if (cur->_bf == 1)
{
RotateL(parent);
}
else // -1
{
RotateRL(parent);
}
}
else // -2
{
if (cur->_bf == -1)
{
RotateR(parent);
}
else
{
RotateLR(parent);
}
}
break;
}
}
return true;
}
//左单旋
void RotateL(Node* parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
Node* ppNode = parent->_parent;
parent->_right = subRL;
if (subRL) //subRL 即为空 上面代码仍成立
subRL->_parent = parent;
subR->_left = parent;
parent->_parent = subR;
if (parent == _root) //判断是否为根节点
{
_root = subR;
_root->_parent = NULL;
}
else
{
if (ppNode->_left == parent) //parent 位于其父节点的左边
{
ppNode->_left = subR;
}
else
{
ppNode->_right = subR;
}
subR->_parent = ppNode; //无论是否存在祖父节点 统一指向其
}
parent->_bf = subR->_bf = 0; // 平衡因子制0
}
// 右单旋
void RotateR(Node* parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
Node* ppNode = parent->_parent;
parent->_left = subLR;
if (subLR)
subLR->_parent = parent;
subL->_right = parent;
parent->_parent = subL;
if (_root == parent)
{
_root = subL;
subL->_parent = NULL;
}
else
{
if (ppNode->_right == parent)
{
ppNode->_right = subL;
}
else
{
ppNode->_left = 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; // 前面两个的最终平衡因子与subLR有关
RotateL(parent->_left); //左旋
RotateR(parent); //右旋
if (bf == 0)
{
subLR->_bf = subL->_bf = parent->_bf = 0;
}
else if (bf == 1)
{
parent->_bf = 0;
subL->_bf = -1;
subLR->_bf = 0;
}
else if (bf == -1)
{
parent->_bf = 1;
subL->_bf = 0;
subLR->_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)
{
subRL->_bf = subR->_bf = parent->_bf = 0;
}
else if (bf == 1)
{
subR->_bf = 0;
parent->_bf = -1;
subRL->_bf = 0;
}
else if (bf == -1)
{
parent->_bf = 0;
subR->_bf = 1;
subRL->_bf = 0;
}
else
{
assert(false);
}
}
//打印 递归打印需要两个函数体
void _InOrder(Node* root)
{
if (root == NULL)
{
return;
}
_InOrder(root->_left);
cout << root->_key << " ";
_InOrder(root->_right);
}
void InOrder()
{
_InOrder(_root);
cout << endl;
}
//检测是否平衡
//优化为时间复杂度 O(N) 故要传高度 优化递归 左右中 中序遍历
bool _IsBalance(Node* root, int& height) // 传根和高度
{
if (root == NULL) //递归出口 叶子节点
{
height = 0;
return true;
}
int leftHeight = 0;
int rightHeight = 0;
if (_IsBalance(root->_left, leftHeight)
&& _IsBalance(root->_right, rightHeight))
{
if (rightHeight - leftHeight != root->_bf)
{
cout << "平衡因子异常" << root->_key << endl;
return false;
}
height = leftHeight > rightHeight ? \
leftHeight + 1 : rightHeight + 1;
return abs(leftHeight - rightHeight) < 2; //abs()取绝对值
//若返回false则不是AVL树
}
else
{
return false;
}
}
bool IsBalance()
{
int height = 0;
return _IsBalance(_root, height);
}
private:
Node* _root;
};
void TestAVLtree()
{
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> t;
for (size_t i = 0; i < sizeof(a) / sizeof(int); ++i)
{
t.Insert(a[i], i);
cout << a[i] << ":" << t.IsBalance() << endl;
}
t.InOrder();
cout <<"Is Balance:"<< t.IsBalance() << endl;
}
RBTree(红黑树):https://blog.csdn.net/Romantic_C/article/details/81263820