在计算机科学中,AVL树是最先发明的自平衡二叉查找树。在AVL树中任何节点的两个子树的高度最大差别为一,所以它也被称为高度平衡树。查找、插入和删除在平均和最坏情况下都是O(log n)。增加和删除可能需要通过一次或多次树旋转来重新平衡这个树。AVL树得名于它的发明者 G.M. Adelson-Velsky 和 E.M. Landis。AVL处理了搜索二叉树的特殊情况。
AVL树中的每个左子树和右子树都是AVL树 ;每个节点都有一个平衡因子(balance factor--bf),任一节点的平衡因子是-1,0,1。(每个节点的平衡因子等于右子树的高度减去左子 树的高度 )
#include<iostream>
#include<cstdlib>
using namespace std;
template<class K,class V>
struct AVLNode
{
K _key;
V value;
AVLNode<K,V>* left;
AVLNode<K,V>* right;
AVLNode<K,V>* _parent;
int _bf;
AVLNode(const K key,const V value)
:_key(key)
,value(value)
,left(NULL)
,right(NULL)
,_parent(NULL)
,_bf(0)
{}
#include<cstdlib>
using namespace std;
template<class K,class V>
struct AVLNode
{
K _key;
V value;
AVLNode<K,V>* left;
AVLNode<K,V>* right;
AVLNode<K,V>* _parent;
int _bf;
AVLNode(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 AVLNode<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->right;
}
else if(cur->_key>key)
{
parent=cur;
cur=cur->left;
}
else
{
return false;
}
}
cur=new Node(key,value); //将要插入的结点创建
if(parent!=NULL) //在合适的位置插入节点
{
if(parent->_key<key)
{
parent->right=cur;
}
else if(parent->_key>key)
{
parent->left=cur;
cur->_parent=parent;
}
}
while(parent) //调节平衡因子
{
template<class K,class V>
class AVLTree
{
typedef AVLNode<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->right;
}
else if(cur->_key>key)
{
parent=cur;
cur=cur->left;
}
else
{
return false;
}
}
cur=new Node(key,value); //将要插入的结点创建
if(parent!=NULL) //在合适的位置插入节点
{
if(parent->_key<key)
{
parent->right=cur;
}
else if(parent->_key>key)
{
parent->left=cur;
cur->_parent=parent;
}
}
while(parent) //调节平衡因子
{
if(parent->left==cur)
parent->_bf--;
else
parent->_bf++;
if(parent->_bf==0)
return true;
else if(parent->_bf==1||parent->_bf==-1)
{
cur=parent;
parent=cur->_parent;
}
else
{
if(parent->_bf==2) //不满足情况时进行旋转
{
if(cur->_bf==1)
RotateL(parent);
if(cur->_bf==-1)
RotateRL(parent);
}
else
{
if(cur->_bf==-1)
RotateR(parent);
if(cur->_bf==1)
RotateLR(parent);
parent->_bf--;
else
parent->_bf++;
if(parent->_bf==0)
return true;
else if(parent->_bf==1||parent->_bf==-1)
{
cur=parent;
parent=cur->_parent;
}
else
{
if(parent->_bf==2) //不满足情况时进行旋转
{
if(cur->_bf==1)
RotateL(parent);
if(cur->_bf==-1)
RotateRL(parent);
}
else
{
if(cur->_bf==-1)
RotateR(parent);
if(cur->_bf==1)
RotateLR(parent);
}
break;
}
}
return true;
}
break;
}
}
return true;
}
bool IsAVLtree(Node* root,int& h) //递归判断是否满足AVL树
{
if(root==NULL)
h=0;
return true;
int hR=0;
int hL=0;
if(IsAVLtree(root->left)==false)
return false;
if(IsAVLtree(root->right)==false)
return false;
h=hR>hL?hR:hL;
return abs(hR-hL)<2;
}
{
if(root==NULL)
h=0;
return true;
int hR=0;
int hL=0;
if(IsAVLtree(root->left)==false)
return false;
if(IsAVLtree(root->right)==false)
return false;
h=hR>hL?hR:hL;
return abs(hR-hL)<2;
}
protected:
void RotateL(Node* parent)
{
Node* subR=parent->right;
Node* subRL=subR->left;
parent->right=subR;
parent->left=parent;
if(subRL)
subRL->_parent=parent;
{
Node* subR=parent->right;
Node* subRL=subR->left;
parent->right=subR;
parent->left=parent;
if(subRL)
subRL->_parent=parent;
subR->left=parent;
Node* pnode=parent->_parent;
parent->_parent=subR;
if(pnode==NULL)
{
_root=subR;
subR->_parent=NULL;
}
else
{
if(pnode->left=parent)
pnode->left=subR;
else
pnode->right=subR;
subR->_parent=pnode;
}
parent->_bf=subR->_bf=0;
}
Node* pnode=parent->_parent;
parent->_parent=subR;
if(pnode==NULL)
{
_root=subR;
subR->_parent=NULL;
}
else
{
if(pnode->left=parent)
pnode->left=subR;
else
pnode->right=subR;
subR->_parent=pnode;
}
parent->_bf=subR->_bf=0;
}
void RotateR(Node* parent)
{
Node* subL=parent->left;
Node* subLR=subL->right;
parent->left=subLR;
subL->right=parent;
if(subLR)
subLR->_parent=parent;
{
Node* subL=parent->left;
Node* subLR=subL->right;
parent->left=subLR;
subL->right=parent;
if(subLR)
subLR->_parent=parent;
subL->right=parent;
Node* pnode=parent->_parent;
parent->_parent=subL;
if(pnode==NULL)
{
_root=subL;
subL->_parent=NULL;
}
else
{
if(pnode->left==parent)
pnode->left=subL;
else
pnode->right=subL;
subL->_parent=pnode;
}
parent->_bf=subL->_bf=0;
}
Node* pnode=parent->_parent;
parent->_parent=subL;
if(pnode==NULL)
{
_root=subL;
subL->_parent=NULL;
}
else
{
if(pnode->left==parent)
pnode->left=subL;
else
pnode->right=subL;
subL->_parent=pnode;
}
parent->_bf=subL->_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)
subR->_bf=subRL->_bf=parent->_bf=0;
if(bf==-1)
{
subR->_bf=1;
parent->_bf=0;
subRl->_bf=0;
}
if(bf==1)
{
subL->_bf=0;
subLR->_bf=0;
parent->_bf=-1;
}
}
}
void RotateLR(Node* parent)
{
Node* subL=parent->left;
Node* subLR=subL->right;
int bf=subLR->_bf;
RotateL(parent->left);
RotateR(parent);
if(bf==0)
subL->_bf=subLR->_bf=parent->_bf=0;
if(bf==-1)
{
subL->_bf=0;
parent->_bf=1;
subLR->_bf=0;
}
if(bf==1)
{
subL->_bf=-1;
subLR->_bf=0;
parent->_bf=0;
}
}
private:
Node* _root;
};
int main()
{
int a[9]={16, 3, 7, 11, 9, 26, 18, 14, 15};
AVLTree<int,int> tree;
for(int i=0;i<9;i++)
{
tree.Insert(a[i],i);
}
system("pause");
return 0;
Node* _root;
};
int main()
{
int a[9]={16, 3, 7, 11, 9, 26, 18, 14, 15};
AVLTree<int,int> tree;
for(int i=0;i<9;i++)
{
tree.Insert(a[i],i);
}
system("pause");
return 0;
}