- AVL树
AVL树又被称作高度平衡的二叉搜索树,是由两个俄罗斯数学家提出的。引入它是为了提高二叉搜索树的效率,减少树的平均搜索长度。 - 什么是AVL树(性质)
- 左子树和右子树的高度之差的绝对值不超过1
- 树中的每个左子树和右子树都是AVL树
- 每个节点都有一个平衡因子(balance factor–bf),任一节点的平衡因子是-1,0,1。 (每个节点的平衡因子等于右子树的高度减去左子
树的高度 ) - AVL树的效率
一棵AVL树有N个节点,其高度可以保持在log(2)N, 插入/删除/查找的时间复杂度也是log(2)N。(log以2为底N的对数)
这就是一颗高度平衡的AVL树,注意它的左右子树的高度差是1。
首先给出树结点的代码:
template<class K, class V>
struct AVLTreeNode
{
AVLTreeNode<K,V>* _left;
AVLTreeNode<K,V>* _right;
AVLTreeNode<K,V>* _parent;
K _key; //键值
V _value; //数据
int _bf; // 平衡因子
AVLTreeNode(const K& key, const V& value)
:_left(NULL)
,_right(NULL)
,_parent(NULL)
,_key(key)
,_value(value)
,_bf(0)
{}
};
- 插入
当我们对树进行一系列操作(插入、删除等)后,AVL树很可能就不能保持AVL的特性,所以在进行操作时,我们必须重新平衡这颗二叉树!也就是对树进行旋转(这里主要讲插入操作)。
(下面用图来解释旋转) - 左单旋转
- 右单旋转
- 左右双旋
- 右左双旋
可以看到对于单旋p节点和cur节点的平衡因子都会变成0;
而对于双旋就得分情况:
可以看到其实影响parent和subR平衡因子的结点就是subRL,同样的左右旋转的时候就是subLR,至于平衡因子的改变,无非就是哪个parent或subR/subL子树中,高度变化,稍微梳理下便可。
新结点的平衡因子为0.现在需要考查它的父结点p。如果新节点c是父亲p的右孩子父亲节点p的平衡因子_bf增加1,是左孩子的话减1。当插入新结点c后,p的_bf(平衡因子)改变后有三种情况:
1)父节点p平衡因子为0。说明插入到低子树的一边了,以p为根的树处在平衡状态,不用管。
2)父节点p平衡因子绝对值为1。说明原来父结点p平衡因子为0,插入后父节点p为根的树还没有失去平衡,但是该子树的高度增加了,所以需要向根的方向回溯,看ppar是否平衡。
3)父节点p平衡因子的绝对值为2。说明新节点c插入到较高的子树上了,使得树不平衡,需要进行平衡化旋转。但是旋转又分为几种情况:
- p->_bf=2,这说明右子树高,再分别处理:
1)cur->_bf=1,执行左单旋。
2)cur->_bf=-1,执行右左双旋。
- p->_bf=-2,这说明左子树高,分别处理:
1)cur->_bf=-1,执行右单旋。
2)cur->_bf=1,执行左右双旋。
旋转以后,以p节点为根的子树高度降低,不需要再向上回溯。
实现逻辑代码:
bool Insert(const K& key, const V& value)
{
if (_root == NULL)
{
_root = new Node(key, value);
return true;
}
Node* cur = _root;
Node* parent = NULL;
//找位置
while (cur)
{
if (key < cur->_key)
{
parent = cur;
cur = cur->_left;
}
else if (key>cur->_key)
{
parent = cur;
cur = cur->_right;
}
else
{
return false;
}
}
cur = new Node(key, value);
if (key < parent->_key)
{
parent->_left = cur;
}
else
{
parent->_right = cur;
}
cur->_parent = parent;
//更新平衡因子,调整树(旋转)
while (parent)
{
if (parent->_left == cur)
parent->_bf -= 1;
else
parent->_bf += 1;
if (parent->_bf == 0)
break;
if (parent->_bf == 1
|| parent->_bf == -1)//满足条件,向上调整
{
cur = parent;
parent = cur->_parent;
}
else if (parent->_bf == 2 || parent->_bf == -2)
{
if (parent->_bf == 2)//
{
if (cur->_bf == 1)
{
RotateL(parent);
}
else
{
RotateRL(parent);
}
}
else//-2
{
if (cur->_bf == -1)
{
RotateR(parent);
}
else
{
RotateLR(parent);
}
}
break;
}
else
{
cout << "平衡因子出现错误" << endl;
assert(false);
}
}
return true;
}
- 判断是否平衡
size_t Depth()
{
return _Depth(_root);
}
bool IsBalance()
{
return _IsBalance(_root);
}
bool _IsBalance(Node* root)
{
if (root == NULL)
return true;
int leftDepth = _Depth(root->_left);
int rightDepth = _Depth(root->_right);
if ((rightDepth - leftDepth) != root->_bf)
{
cout << "平衡因子error!" << root->_key << << endl;
return false;
}
return abs(rightDepth - leftDepth) <= 1
&& _IsBalance(root->_left)
&& _IsBalance(root->_right);
}
size_t _Depth(Node* root)
{
if (root == NULL)
{
return 0;
}
size_t leftHeight = _Depth(root->_left);
size_t rightHeight = _Depth(root->_right);
return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
}
- 完整代码:
#include<iostream>
#include<assert.h>
using namespace std;
template<class K, class V>
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)
:_key(key)
, _value(value)
, _left(NULL)
, _right(NULL)
, _parent(NULL)
, _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* cur = _root;
Node* parent = NULL;
//找位置
while (cur)
{
if (key < cur->_key)
{
parent = cur;
cur = cur->_left;
}
else if (key>cur->_key)
{
parent = cur;
cur = cur->_right;
}
else
{
return false;
}
}
cur = new Node(key, value);
if (key < parent->_key)
{
parent->_left = cur;
}
else
{
parent->_right = cur;
}
cur->_parent = parent;
//更新平衡因子,调整树(旋转)
while (parent)
{
if (parent->_left == cur)
parent->_bf -= 1;
else
parent->_bf += 1;
if (parent->_bf == 0)
break;
if (parent->_bf == 1 || parent->_bf == -1)//满足条件,向上调整
{
cur = parent;
parent = cur->_parent;
}
else if (parent->_bf == 2 || parent->_bf == -2)
{
if (parent->_bf == 2)//
{
if (cur->_bf == 1)
{
RotateL(parent);
}
else
{
RotateRL(parent);
}
}
else//-2
{
if (cur->_bf == -1)
{
RotateR(parent);
}
else
{
RotateLR(parent);
}
}
break;
}
else
{
cout << "平衡因子出现错误" << endl;
assert(false);
}
}
return true;
}
size_t Depth()
{
return _Depth(_root);
}
bool IsBalance()
{
return _IsBalance(_root);
}
void InOrder()
{
_InOrder(_root);
cout << endl;
}
protected:
void RotateL(Node* parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
parent->_right = subRL;
if (subRL)
subRL->_parent = parent;
Node* pparent = parent->_parent;
parent->_parent = subR;
subR->_left = parent;
if (pparent == NULL)
{
_root = subR;
_root->_parent = NULL;
}
else
{
if (parent == pparent->_left)
{
pparent->_left = subR;
}
else
{
pparent->_right = subR;
}
subR->_parent = pparent;
}
parent->_bf = subR->_bf = 0;
}
void RotateR(Node* parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
parent->_left = subLR;
if (subLR)
subLR->_parent = parent;
subL->_right = parent;
Node* pparent = parent->_parent;
parent->_parent = subL;
if (pparent)
{
if (pparent->_left == parent)
{
pparent->_left = subL;
}
else
{
pparent->_right = subL;
}
subL->_parent = pparent;
}
else
{
_root = subL;
subL->_parent = NULL;
}
parent->_bf = subL->_bf = 0;
}
void RotateLR(Node* parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
int bf = subLR->_bf;
RotateL(subL);
RotateR(parent);
if (bf == 1)
{
subL->_bf = -1;
parent->_bf = 0;
}
else if (bf==-1)
{
parent->_bf = 1;
subL->_bf = 0;
}
else
{
parent->_bf = subL->_bf = 0;
}
subLR->_bf = 0;
}
void RotateRL(Node* parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
int bf = subRL->_bf;
RotateR(subR);
RotateL(parent);
if (bf == 1)
{
subR->_bf = 0;
parent->_bf = -1;
}
else if (bf == -1)
{
subR->_bf = 0;
parent->_bf = 0;
}
else //bf==0
{
subR->_bf = parent->_bf = 0;
}
subRL->_bf = 0;
}
size_t _Depth(Node* root)
{
if (root == NULL)
{
return 0;
}
size_t leftHeight = _Depth(root->_left);
size_t rightHeight = _Depth(root->_right);
return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
}
bool _IsBalance(Node* root)
{
if (root == NULL)
return true;
int leftDepth = _Depth(root->_left);
int rightDepth = _Depth(root->_right);
if ((rightDepth - leftDepth) != root->_bf)
{
cout << "平衡因子error" << root->_key << << endl;
return false;
}
return abs(rightDepth - leftDepth) <= 1 && _IsBalance(root->_left) && _IsBalance(root->_right);
}
void _InOrder(Node* root)
{
if (root == NULL)
{
return;
}
_InOrder(root->_left);
cout << root->_key << " ";
_InOrder(root->_right);
}
protected:
Node* _root;
};
void TestAVLTree1()
{
AVLTree<int, int> t1;
int a[] = { 16, 3, 7, 11, 9, 26, 18, 14, 15 };
for (size_t i = 0; i < sizeof(a) / sizeof(a[0]); ++i)
{
t1.Insert(a[i], i);
}
t1.InOrder();
}