AVL树的旋转。居然1A了....

了解旋转方式之后,数据较小可以当做模拟写。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<algorithm>
using namespace std; const int maxn=;
int n,a[maxn];
struct Node
{
int num;
int L,R;
int LH,RH;
int fa;
}node[maxn];
int sz;
int root; void init()
{
root=;
sz=;
node[root].fa=-;
node[root].L=-,node[root].R=-;
node[root].num=a[];
node[root].LH=,node[root].RH=;
} void dfs(int now)
{
if(node[now].L!=-) dfs(node[now].L);
if(node[now].R!=-) dfs(node[now].R);
node[now].LH=max(node[node[now].L].LH,
node[node[now].L].RH)+;
node[now].RH=max(node[node[now].R].LH,
node[node[now].R].RH)+;
} void Update(int fa,int num,int tag)
{
sz++;
node[sz].fa=fa;
node[sz].L=-,node[sz].R=-;
node[sz].num=num;
node[sz].LH=,node[sz].RH=;
if(tag==) node[fa].R=sz;
else node[fa].L=sz;
dfs(root);
} void Insert(int num)
{
int p=root;
while()
{
if(num>=node[p].num)
{
if(node[p].R!=-) p=node[p].R;
else { Update(p,num,); break; }
}
else
{
if(node[p].L!=-) p=node[p].L;
else { Update(p,num,); break; }
}
}
} void LL(int id)
{
int Lson=node[id].L; Node tmp1=node[id];
Node tmp2=node[Lson]; if(tmp1.fa!=-)
{
if(node[tmp1.fa].L==id) node[tmp1.fa].L=Lson;
else node[tmp1.fa].R=Lson;
}
node[Lson].fa=tmp1.fa; node[Lson].R=id;
node[id].fa=Lson; node[id].L=tmp2.R;
node[tmp2.R].fa=id;
} void RR(int id)
{
int Rson=node[id].R;
Node tmp1=node[id];
Node tmp2=node[Rson]; if(tmp1.fa!=-)
{
if(node[tmp1.fa].L==id) node[tmp1.fa].L=Rson;
else node[tmp1.fa].R=Rson;
} node[Rson].fa=tmp1.fa; node[Rson].L=id;
node[id].fa=Rson; node[id].R=tmp2.L;
node[tmp2.L].fa=id;
} void LR(int id)
{
int Lson=node[id].L;
RR(Lson);
LL(id);
} void RL(int id)
{
int Rson=node[id].R;
LL(Rson);
RR(id);
} void Rotate(int id)
{
int need=-;
int p=node[id].fa;
while()
{
if(p==-) break;
if(abs(node[p].LH-node[p].RH)>) { need=p; break; }
p=node[p].fa;
} if(need==-) return; if(node[need].LH>node[need].RH)
{
int Lson=node[need].L;
if(node[Lson].LH>node[Lson].RH) LL(need);
else LR(need);
} else
{
int Rson=node[need].R;
if(node[Rson].RH>node[Rson].LH) RR(need);
else RL(need);
} for(int i=;i<=sz;i++) if(node[i].fa==-) root=i;
dfs(root);
} int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&a[i]); init();
for(int i=;i<=n;i++)
{
Insert(a[i]);
Rotate(sz);
} printf("%d\n",node[root].num); return ;
}

最新文章

  1. springmvc注解事例
  2. [MySQL Reference Manual] 24 MySQL sys框架
  3. [转载] 构造linux 系统下免密码ssh登陆  _How to establish password-less login with SSH
  4. Thinkphp的初级注意点
  5. MongoDB 副本集的相关概念【转】
  6. Android BLE 蓝牙低功耗教程,中央BluetoothGatt和周边BluetoothGattServer的实现
  7. 生物信息大数据&amp;数据库(NCBI、EBI、UCSC、TCGA)
  8. [Effective JavaScript 笔记]第17条:间接调用eval函数优于直接调用
  9. 021ARM处理器工作模式
  10. android 77 fragment
  11. 八、C# 值类型
  12. 《Web 前端面试指南》2、JavaScript 的 Bind 函数进阶
  13. Javascript 中神奇的 this
  14. [Swust OJ 795]--Penney Game
  15. MongoDB日常保养
  16. Unix/Linux环境C编程新手教程(41) C语言库函数的文件操作具体解释
  17. js 中 new fn与new fn()的区别
  18. Mybatis面试整理
  19. 川普和习G-20会面为缓和中美贸易战提供了很大的机会
  20. Visual Studio Code and local web server

热门文章

  1. Struts2原理图
  2. android网络编程之HttpUrlConnection的讲解--GET请求
  3. 如何使用 AngularJS 的 ngShow 和 ngHide
  4. SSH自动断开连接的原因
  5. TextUtils使用
  6. ASP.NET中使用Server.Transfer()方法在页间传值 实例
  7. seo优化 标点符号
  8. 上海赛趣-top.mainFrame.tabAddHandler方法详解
  9. OpenGL ES着色器语言之着色概览(官方文档)
  10. iOS 开发之照片框架详解之二 —— PhotoKit 详解(上)