#include <cstdio>
#include <algorithm>
using namespace std;
//AVL 树
//1、结点
struct node{
int v,height;//v为结点权值,height为当前子树高度
node *lchild,*rchild ;
};
//新建结点
node * newnode(int v)
{
node* Node=new node;
Node->v=v;
Node->height=1; //初始高度为1
Node->lchild=Node->rchild=NULL;
return Node;
}
//获取以root为根的子树的当前height
int getheight(node* root)
{
if(root==NULL)
return 0;
else
return root->height;
}
//计算结点root的平衡因子
int getbalance(node *root)
{
return getheight(root->lchild)-gethight(root->rchild);
}
//更新结点root 的height
void updateheight(node* root)
{
root->height=max(getheight(root->lchild),getheight(root->rchild))+1;
}
//基本操作
//1 查找
void search(node* root,int x)
{
if(root==NULL)
{
printf("search faild\n");
return;
}
if(x==root->v)
printf("%d\n",root->v);
else if(root->v<x)
search(root->rchild,x);
else
search(root->lchild,x);
}
//2 插入操作
//1)左旋
void L(node* &root)
{
node* temp=root->rchild; //root指向结点A,temp指向结点B
root->rchild=temp->lchild; //1让B的左子树成为 A的右子树
temp->lchild=root;//2 让A成为B的左子树
updateheight(root);//更新结点A的高度
updateheight(temp); //更新结点B的高度
root=temp; //3、将根节点设定为B
}
//2)右旋
void R(node * &root)
{
node*temp=root->lchild;
root->lchild=temp->rchild;
temp->rchild=root;
updateheight(root);
updateheight(temp);
root=temp;
}
//插入权值为v的结点
void insert(node* &root,int v)
{
if(root==NULL) //到达空结点
{
root=newnode(v);
return;
}
if(v-<root->v)
{
insert(root->lchild,v);//往左子树插入
updateheight(root);//更新树高
if(getbalance(root)==2)
{
if(getbalance(root->lchild)==1) //LL型
{
R(root);//右旋
}
else if(getbalance(root->lchild)==-1) //LR型
{
L(root->lchild);//先将根节点的左子树左旋
R(root); //再将根节点右旋
}
}
}
else
{
insert(root->rchild,v);//往右子树插入
updateheight(root);
if(getbalance(root)==-2)//不平衡,需调整
{
if(getbalance(root->rchild)==-1)//RR型
L(root);
else if(getbalance(root->rchild)==1)//RL型
{
R(root->rchild);//先将根节点右子树右旋
L(root); //再将根节点左旋
}
}
}
}
//AVL树的建立
node *create(int data[],int n)
{
node* root=NULL;//新建空根结点root
for(int i=0;i<n;++i)
{
insert(root,data[i]);
}
return root;
}