AVLTree搜索树
AVL树本质上是一棵二叉搜索树,它的特点是:
1.本身首先是一棵二叉搜索树。
2.带有平衡条件:每个结点的左右子树的高度之差的绝对值(平衡因子)最多为1。
也就是说,AVL树,本质上是带了平衡功能的二叉查找树(二叉排序树,二叉搜索树)。
调整平衡的方法如下:
实现代码如下:
#include <math.h>
#include <iostream>
using namespace std;
template<class K,class V>
struct AVLTreeNode
{
K _key;
V _val;
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)
, _val(value)
, _bf(0)
{}
};
template<class K,class V>
class AVLTree
{
typedef AVLTreeNode<K, V> Node;
public:
AVLTree()
:_root(NULL)
{}
//~AVLTree()
//{}
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->_right;
}
else if (cur->_key>key)//左边
{
parent = cur;
cur = cur->_left;
}
else
{
return false;//不存在
}
}
cur = new Node(key, value);
if (key > parent->_key)
{
parent->_right =cur;
cur->_parent = parent;
}
else
{
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;
parent = parent->_parent;
}
else //_bf=2 _bf=-2
{
if (parent->_bf == 2)
{
if (cur->_bf == 1)
{
RotateL(parent);
return true;
}
else if (cur->_bf == -1)
{
RotateRL(parent);
return true;
}
}
else if (parent->_bf == -2)
{
if (cur->_bf == -1)
{
RotateR(parent);
return true;
}
else if (cur->_bf == 1)
{
RotateLR(parent);
return true;
}
}
}
}
return true;
}
int Depth(Node* &root)
{
if (root == NULL)
{
return 0;
}
int left = Depth(root->_left);
int right = Depth(root->_right);
return right > left ? right + 1 : left + 1;
}
bool IsBalance()
{
return _IsBalance(_root);
}
bool _IsBalance(Node* &root)
{
if (root == NULL)
{
return true;
}
int left = Depth(root->_left);
int right = Depth(root->_right);
int bf = right - left;
if (abs(bf) > 1 ||bf!=root->_bf)
{
cout <<"平衡因子失常"<<root->_bf<<" "<<root->_key<< endl;
return false;
}
return abs(right - left) < 2 && _IsBalance(root->_left) && _IsBalance(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;
Node* ppnode = parent->_parent;
parent->_parent = subR;/
if (ppnode==NULL)
{
_root = subR;
subR->_parent = NULL;
}
else if (parent==ppnode->_left)
{
ppnode->_left = subR;
}
else
{
ppnode->_right = subR;
}
subR->_parent = ppnode;
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;
subL->_parent = parent->_parent;
Node* ppnode = parent->_parent;
parent->_parent = subL;
if (ppnode == NULL)
{
_root = subL;
subL->_parent = NULL;
}
else if (ppnode->_left == parent)
{
ppnode->_left = subL;
}
else
{
ppnode->_right = subL;
}
subL->_parent = ppnode;
parent->_bf = subL->_bf = 0;
}
void RotateRL(Node* &parent)
{
/*RotateR(parent->_right);
RotateL(parent);*/
Node* subR = parent->_right;
Node* subRL = subR->_left;
int bf = subRL->_bf;
RotateR(parent->_right);
RotateL(parent);
if (bf==1)
{
subR->_bf = 0;
parent->_bf =-1 ;
}
else if (bf==-1)
{
subR->_bf = 1;
parent->_bf = 0;
}
else //0
{
subR->_bf = parent->_bf = 0;
}
subRL->_bf = 0;
}
void RotateLR(Node* &parent)
{
/*RotateL(parent->_left);
RotateR(parent);
*/
Node* subL = parent->_left;
Node* subLR = subL->_right;
int bf = subLR->_bf;
RotateL(parent->_left);
RotateR(parent);
if (bf == -1)
{
subL->_bf = 0;
parent->_bf = 1;
}
else if (bf == 1)
{
subL->_bf = -1;
parent->_bf = 0;
}
else
{
parent->_bf = subL->_bf = 0;
}
subLR->_bf = 0;
}
void InOrder()
{
_InOrder(_root);
cout << endl;
}
protected:
void _InOrder(Node* root)
{
if (root==NULL)
{
return ;
}
_InOrder(root->_left);
cout << root->_key << " ";
_InOrder(root->_right);
}
private:
Node* _root;
};
void Test()
{
//int a[] = { 5, 3, 4, 1, 7, 8, 2, 6, 0, 9 };
int a[] ={ 4, 2, 6, 1, 3, 5, 15, 7, 16,14};
AVLTree<int,int> t;
for (size_t i = 0; i < sizeof(a) / sizeof(a[0]); i++)
{
t.Insert(a[i],i);
cout<<t.IsBalance()<<endl;
}
t.InOrder();
}