http://www.cnblogs.com/heqile/archive/2011/11/28/2265713.html
http://www.cnblogs.com/heqile/archive/2011/11/28/2266385.html
#include <iostream>
#include <list>
#include <utility>
#include <string>
using namespace std;
static const string PRINT_SPACES =" ";
template <typename Object>
class AVLTree
{
public:
AVLTree(){root=NULL;}
void insert(const Object &x)
{insert(x,root);}
void remove(const Object &x)
{remove(x,root);}
void print();
private:
struct AVLnode
{
Object data;
AVLnode *left;
AVLnode *right;
int height;
AVLnode(Object ob,AVLnode *l,AVLnode *r,int h=0):data(ob),left(l),right(r),height(h){}
};
AVLnode *root;
void insert(const Object &x,AVLnode * &t);
void remove(const Object &x,AVLnode *&t);
void leftSingleRotation(AVLnode * &t);
void rightSingleRotation(AVLnode * &t);
void leftDoubleRotation(AVLnode * &t);
void rightDoubleRotation(AVLnode * &t);
int height(AVLnode * &t)
{
//结点的高度,空结点的高度为-1
return t==NULL?-1:t->height;
}
int max(int a,int b)
{
return a<b?b:a;
}
int powerOf2(int x)//求2的x次方(x大于等于0)
{
if(x==0)return 1;
int m=1;
while(--x>=0)
m*=2;
return m;
}
AVLnode * max_node(AVLnode * t)//max和min使用递归的话不能使用引用形参,不能返回引用
{
if(!t)
return NULL;
if(t->right)
return max_node(t->right);
else
return t;
}
AVLnode * min_node(AVLnode * t)
{
if(t)//考虑一下t为空的情况,使其更全面,毕竟这个函数是可以加一个public版本的重载供用户使用
while(t->left)
t=t->left;
return t;
}
};
template <typename Object>
void AVLTree<Object>::insert(const Object &x,AVLnode * &t)
{
if(!t)
t=new AVLnode(x,NULL,NULL);
else if(x<t->data)
{
insert(x,t->left);
if(height(t->left)-height(t->right)==2)//在左子树插入结点后,不可能右子树比左子树高2
if(x<t->left->data)
leftSingleRotation(t);//左单旋转
else
leftDoubleRotation(t);//左双旋转
else//不需要调整就满足平衡条件的话,只需要重新计算其高度就好
t->height=max(height(t->left),height(t->right))+1;
}
else if(x>t->data)
{
insert(x,t->right);
if(height(t->right)-height(t->left)==2)
if(x>t->right->data)
rightSingleRotation(t);//右单旋转
else
rightDoubleRotation(t);//右双旋转
else
t->height=max(height(t->left),height(t->right))+1;
}
else;//不考虑重复的值
}
template <typename Object>
void AVLTree<Object>::leftSingleRotation(AVLnode * &t)//左单旋转
{
AVLnode *s=t->left;
t->left=s->right;
s->right=t;
t->height=max(height(t->left),height(t->right))+1;//重新计算s和t的高度
s->height=max(height(s->left),t->height)+1;
t=s;
}
template <typename Object>
void AVLTree<Object>::rightSingleRotation(AVLnode * &t)
{
AVLnode *s=t->right;
t->right=s->left;
s->left=t;
t->height=max(height(t->left),height(t->right))+1;
s->height=max(t->height,height(s->right))+1;
t=s;
}
template <typename Object>
void AVLTree<Object>::leftDoubleRotation(AVLnode * &t)//左双旋转
{
AVLnode *p=t->left;
AVLnode *q=p->right;
t->left=q->right;
p->right=q->left;
q->left=p;
q->right=t;
t->height=max(height(t->left),height(t->right))+1;//重新计算3个结点的高度
p->height=max(height(p->left),height(p->right))+1;
q->height=max(p->height,t->height)+1;
t=q;
}
template <typename Object>
void AVLTree<Object>::rightDoubleRotation(AVLnode * &t)
{
AVLnode *p=t->right;
AVLnode *q=p->left;
t->right=q->left;
p->left=q->right;
q->right=p;
q->left=t;
t->height=max(height(t->left),height(t->right))+1;
p->height=max(height(p->left),height(p->right))+1;
q->height=max(t->height,p->height)+1;
t=q;
}
template <typename Object>
void AVLTree<Object>::remove(const Object &x,AVLnode *&t)
{
if(!t)return;//没有找到要删除的值,do nothing
if(x<t->data)
{
remove(x,t->left);
if(height(t->right)-height(t->left)==2)
{
//右子树比左子树高2,那么在删除操作之前右子树比左子树高1,
//也就是说t的右子树必然不为空,甚至必然有非空子树(高度至少为1).
AVLnode *s=t->right;
if(height(s->left)>height(s->right))
rightDoubleRotation(t);//右双旋转
else
rightSingleRotation(t);//右单旋转
}
else
//不需要调整就满足平衡条件的话,只需要重新计算其高度就好
t->height=max(height(t->left),height(t->right))+1;
}
else if(x>t->data)
{
remove(x,t->right);
if(height(t->left)-height(t->right)==2)
{
AVLnode *s=t->left;
if(height(s->right)>height(s->left))
leftDoubleRotation(t);//左双旋转
else
leftSingleRotation(t);//左单旋转
}
else
t->height=max(height(t->left),height(t->right))+1;
}
else
{
if(t->left&&t->right)
//t的左右子树都非空,把remove操作转移到只有一个非空子树的结点或者叶子结点上去
{
if(height(t->left)>height(t->right))
//把remove操作往更高的那颗子树上转移
{
//左子树中的最大值
t->data=max_node(t->left)->data;
remove(t->data,t->left);
}
else
{
//右子树中的最小值
t->data=min_node(t->right)->data;
remove(t->data,t->right);
}
}
else
{
AVLnode *oldnode=t;
t=t->left?t->left:t->right;
delete oldnode;
}
}
}
//使用队列层次遍历打印树
template <typename Object>
void AVLTree<Object>::print()
{
if(!root)return;
list<pair<AVLnode *,int> > lis;//int是标志位,0表示正常值,-1表示此处没有值,应打印空格
lis.push_back(make_pair(root,0));
AVLnode *em=new AVLnode(0,NULL,NULL);//一个空的点
int count=1,j=0;//count表示当前行的最大结点数,j表示当前行已打印的结点数(空结点也计数)
int hg=root->height;//当前行的高度,计算空格时使用
while(hg>=0)//当hg<0时说明最后一行(树叶)已经打印完毕
{
while(j++!=count)
{
for(int i=1;i<powerOf2(hg);i++)
cout<<PRINT_SPACES;//打印前一部分空格
if(lis.front().second==0)
cout<<lis.front().first->data;
else
cout<<PRINT_SPACES;//int位为-1,则打印空格以对齐
if(lis.front().first->left)//左子树入队
lis.push_back(make_pair(lis.front().first->left,0));
else
lis.push_back(make_pair(em,-1));
if(lis.front().first->right)//右子树入队
lis.push_back(make_pair(lis.front().first->right,0));
else
lis.push_back(make_pair(em,-1));
for(int i=0;i<powerOf2(hg);i++)
cout<<PRINT_SPACES;//打印后一部分空格
lis.pop_front();
}
j=0;
count*=2;//下一行的最大节点数是上一行的两倍
--hg;//高度减1
cout<<endl; //换行
}
delete em;
lis.clear();
}