AVL树的性质
1. 左子树和右子树的高度之差的绝对值不超过12. 树中的每个左子树和右子树都是AVL树
3. 每个节点都有一个平衡因子(balance factor--bf),任一节点的平衡因子是-1,0,1。(每个节点的平衡因子等于右子树的高度减去左子
树的高度 )
AVL树的效率
一棵AVL树有N个节点,其高度可以保持在log2N,插入/删除/查找的时间复杂度也是log2N。(ps:log2N是表示log以2为底N的对数。)
调整情况有四种:
1:左单旋
2:右单旋:
3:左右双旋:
4:右左双旋:
#include<iostream>
#include<math.h>
using namespace std;
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)
:_bf(0), _left(NULL), _right(NULL), _parent(NULL), _key(key), _value(value)
{}
};
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 (cur->_key > key)
{
parent = cur;
cur = cur->_left;
}
else if (cur->_key < key)
{
parent = cur;
cur = cur->_right;
}
else
return false;
}
Node* tmp;
if (parent->_key>key)
{
tmp = new Node(key, value);
parent->_left = tmp;
tmp->_parent = parent;
}
else
{
tmp = new Node(key, value);
parent->_right = tmp;
tmp->_parent = parent;
}
//更新_bf
bool isRotate = false;
cur = tmp;
parent = cur->_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 = cur->_parent;
}
else //用旋转来调整平衡因子
{
if (parent->_bf == 2)
{
if (cur->_bf == 1)
RotateL(parent);
else
RotateRL(parent);
}
else
{
if (cur->_bf == -1)
RotateR(parent);
else
RotateLR(parent);
}
isRotate = true;
break;
}
}
if (isRotate)
{
Node* ppNode = parent->_parent;
if (ppNode == NULL)
_root = parent;
else if (ppNode->_key > parent->_key)
ppNode->_left = parent;
else
ppNode->_right = parent;
}
return true;
}
void InOrder()
{
_InOrder(_root);
cout << endl;
}
bool IsBlance()
{
return _IsBlance(_root);
}
protected:
void _InOrder(Node* root)
{
if (root == NULL)
return;
_InOrder(root->_left);
cout << root->_key << " ";
_InOrder(root->_right);
}
int _Height(Node* root)
{
if (root == NULL)
return NULL;
int left = _Height(root->_left);
int right = _Height(root->_right);
return left > right ? left + 1 : right + 1;
}
bool _IsBlance(Node* root)
{
if (root == NULL)
return true;
int bf = _Height(root->_right) - _Height(root->_left);
if (root->_bf != bf)
cout << "平衡因子异常" << root->_key << endl;
return root->_bf == bf&&abs(bf) < 2 && _IsBlance(root->_left) && _IsBlance(root->_right);
}
void RotateL(Node* &parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
parent->_right = subRL;
if (subRL)
subRL->_parent = parent;
subR->_left = parent;
subR->_parent = parent->_parent;
parent->_parent = subR;
parent->_bf = subR->_bf = 0;
parent = subR;
}
void RotateR(Node* &parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
parent->_left = subLR;
if (subLR)
subLR->_parent = parent;
subL->_right = parent;
subL->_parent = parent->_parent;
parent->_parent = subL;
parent->_bf = subL->_bf = 0;
parent = subL;
}
void RotateRL(Node* &parent)
{
Node* pNode = parent;
Node* subRNode = parent->_right;
Node* subRLNode = subRNode->_left;
int bf = subRLNode->_bf;
RotateR(parent->_right);
RotateL(parent);
if (bf == 1)
{
subRNode->_bf = 0;
pNode->_bf = -1;
}
else if (bf == -1)
{
subRNode->_bf = 1;
pNode->_bf = 0;
}
else
{
subRNode->_bf = 0;
pNode->_bf = 0;
}
subRLNode->_bf = 0;
}
void RotateLR(Node* &parent)
{
Node* pNode = parent;
Node* subLNode = parent->_left;
Node* subLRNode = subLNode->_right;
int bf = subLRNode->_bf;
RotateL(parent->_left);
RotateR(parent);
if (bf == -1)
{
subLNode->_bf = 0;
pNode->_bf = 1;
}
else if (bf == 1)
{
subLNode->_bf = -1;
pNode->_bf = 0;
}
else
{
subLNode->_bf = 0;
pNode->_bf = 0;
}
subLRNode->_bf = 0;
}
protected:
Node* _root;
};
void test()
{
AVLTree<int, int> t;
int a[] = { 16, 3, 7, 11, 9, 26, 18, 14, 15 };
for (size_t i = 0; i < sizeof(a) / sizeof(a[0]); ++i)
{
t.Insert(a[i], i);
}
t.InOrder();
cout << "t是否平衡?" << t.IsBlance() << endl;
}
int main()
{
test();
return 0;
}