一、背景知识
AVL树是高度平的二叉搜索树,它能降低二叉树的高度,减少树的平均搜索长度.
二、AVL树的性质
1、左子树和右子树的高度差的绝对值不超过1
2、树中的每个左子树和右子树都是AVL树
3、每个节点都有一个平衡因子(balance factor–bf),任一节点的平衡因子都为-1,0,1。(每个节点的平衡因子等于右子树的高度减去左子树的高度)
三、AVL的效率
一棵AVL树有N个节点,高度可以保持在log2n,插入/删除/查找的时间复杂度也是log2n.
(注:log2n表示log以2为底n的对数。)
四、AVL的实现(代码及分析)
AVL树(K/V)的结点
template<class K,class T>
struct AVLTreeNode
{
K _key; //搜索的关键字
T _value; //数值
int _bf; //平衡因子
AVLTreeNode<K, T>* _left;
AVLTreeNode<K, T>* _right;
AVLTreeNode<K, T>* _parent;
AVLTreeNode(const K& key, const T& value)
:_key(key)
, _value(value)
, _bf(0)
, _left(NULL)
, _right(NULL)
, _parent(NULL)
{}
};
AVL树的旋转
当增加一个元素,如果破坏了-1,0,1这样的平衡因子的时候,就需要通过旋转来修改平衡因子。
代码实现(左单旋转)
void RotateL(Node* parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
parent->_right = subRL;
if (subRL) //判断
subRL->_parent = parent;
subR->_left = parent;
Node* ppNode = parent->_parent;
parent->_parent = subR;
if (ppNode == NULL)
{
_root = subR;
subR->_parent = NULL;
}
else
{
if (ppNode->_left == parent)
ppNode->_left = subR;
else if (ppNode->_right == parent)
ppNode->_right = subR;
subR->_parent = ppNode;
}
subR->_bf = parent->_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* ppNode = parent->_parent; //保存parent的父亲
parent->_parent = subL;
if (ppNode == NULL)
{
_root = subL;
subL->_parent = NULL;
}
else
{
if (ppNode->_left == parent)
{
ppNode->_left = subL;
}
else
if (ppNode->_right == parent)
{
ppNode->_right = subL;
}
subL->_parent = ppNode;
}
parent->_bf = 0;
subL->_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 = parent->_bf = subL->_bf = 0;
}
else if (bf == 1)
{
parent->_bf = 0;
subL->_bf = -1;
subLR->_bf = 0;
}
else//bf == -1
{
subL->_bf = 0;
subLR->_bf = 0;
parent->_bf = 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 = subRL->_bf = 0;
}
else if (bf == 1)
{
subR->_bf = 0;
parent->_bf = -1;
subRL->_bf = 0;
}
else // bf == -1
{
parent->_bf = 0;
subR->_bf = 1;
subRL->_bf = 0;
}
}
AVL树的所有代码:
#include<string>
template<class K,class T>
struct AVLTreeNode
{
K _key;
T _value;
int _bf;
AVLTreeNode<K, T>* _left;
AVLTreeNode<K, T>* _right;
AVLTreeNode<K, T>* _parent;
AVLTreeNode(const K& key, const T& value)
:_key(key)
, _value(value)
, _bf(0)
, _left(NULL)
, _right(NULL)
, _parent(NULL)
{}
};
template<class K,class T>
class AVLTree
{
typedef AVLTreeNode<K, T> Node;
protected:
Node * _root;
public:
AVLTree()
:_root(NULL)
{}
~AVLTree()
{}
void Insert(const K& key, const T& value)//AVL树的插入
{
if (_root == NULL)
{
_root = new Node(key, value);
return;
}
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
break;
}
cur = new Node(key, value);
if (parent->_key < key)
{
parent->_right = cur;
cur->_parent = parent;
}
else if (parent->_key > key)
{
parent->_left = cur;
cur->_parent = parent;
}
//平衡平衡因子
while (parent)
{
if (cur == parent->_left)
parent->_bf--;
else if (cur == parent->_right)
parent->_bf++;
if (parent->_bf == 0)
break;
else if (parent->_bf == 1 || parent->_bf == -1)
{
cur = parent;
parent = cur->_parent;
}
else //
{
if (parent->_bf == -2)
{
Node* subL = parent->_left;
if (subL->_bf == -1)
RotateR(parent);
else
RotateLR(parent);
}
else if (parent->_bf == 2)
{
Node* subR = parent->_right;
if (subR->_bf == 1)
RotateL(parent);
else
RotateRL(parent);
}
break;
}
}
return;
}
void InOrder() //中序遍历
{
InOrder(_root);
}
bool IsBalance() //判断是否平衡
{
int height = 0;
return IsBalance(_root, height);
}
size_t Height() //求树的高度
{
Height(_root);
}
protected:
bool IsBalance(Node* root, int& height)
{
if (root == NULL)
{
height = 0;
return true;
}
int left, right ;
if (IsBalance(root->_left, left) && IsBalance(root->_right, right)
&& abs(right - left) < 2)
{
height = left < right ? right + 1 : left + 1;
if (root->_bf != right - left)
{
cout << "平衡因子异常" << root->_key << endl;
return false;
}
return true;
}
else
return false;
}
void RotateR(Node* parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
parent->_left = subLR;
if (subLR)
subLR->_parent = parent;
subL->_right = parent;
Node* ppNode = parent->_parent; //保存parent的父亲
parent->_parent = subL;
if (ppNode == NULL)
{
_root = subL;
subL->_parent = NULL;
}
else
{
if (ppNode->_left == parent)
{
ppNode->_left = subL;
}
else
if (ppNode->_right == parent)
{
ppNode->_right = subL;
}
subL->_parent = ppNode;
}
parent->_bf = 0;
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* ppNode = parent->_parent;
parent->_parent = subR;
if (ppNode == NULL)
{
_root = subR;
subR->_parent = NULL;
}
else
{
if (ppNode->_left == parent)
ppNode->_left = subR;
else if (ppNode->_right == parent)
ppNode->_right = subR;
subR->_parent = ppNode;
}
subR->_bf = parent->_bf = 0;
}
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 = subRL->_bf = 0;
}
else if (bf == 1)
{
subR->_bf = 0;
parent->_bf = -1;
subRL->_bf = 0;
}
else // bf == -1
{
parent->_bf = 0;
subR->_bf = 1;
subRL->_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 = parent->_bf = subL->_bf = 0;
}
else if (bf == 1)
{
parent->_bf = 0;
subL->_bf = -1;
subLR->_bf = 0;
}
else//bf == -1
{
subL->_bf = 0;
subLR->_bf = 0;
parent->_bf = 1;
}
}
void InOrder(Node* root)
{
if (root == NULL)
return;
InOrder(root->_left);
cout << root->_value << " ";
InOrder(root->_right);
}
size_t Height(Node* root)
{
if (root == NULL)
return 0;
int left = Height(root->_left);
int right = Height(root->_right);
return left < right ? right + 1 : left + 1;
}
};
AVL树虽然实用性和效率并不是十分高,但是了解其实现过程,对红黑树、以及相关性能更高的树有着铺垫作用。