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 ;
}

PAT (Advanced Level) 1066. Root of AVL Tree (25)的更多相关文章

  1. PAT甲级:1066 Root of AVL Tree (25分)

    PAT甲级:1066 Root of AVL Tree (25分) 题干 An AVL tree is a self-balancing binary search tree. In an AVL t ...

  2. PTA (Advanced Level) 1066 Root of AVL Tree

    Root of AVL Tree An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of ...

  3. PAT甲级题解-1066. Root of AVL Tree (25)-AVL树模板题

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6803291.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  4. 【PAT甲级】1066 Root of AVL Tree (25 分)(AVL树建树模板)

    题意: 输入一个正整数N(<=20),接着输入N个结点的值,依次插入一颗AVL树,输出最终根结点的值. AAAAAccepted code: #define HAVE_STRUCT_TIMESP ...

  5. pat 甲级 1066. Root of AVL Tree (25)

    1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An A ...

  6. PAT 甲级 1066 Root of AVL Tree (25 分)(快速掌握平衡二叉树的旋转,内含代码和注解)***

    1066 Root of AVL Tree (25 分)   An AVL tree is a self-balancing binary search tree. In an AVL tree, t ...

  7. PAT Advanced 1066 Root of AVL Tree (25) [平衡⼆叉树(AVL树)]

    题目 An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child ...

  8. PAT 1066. Root of AVL Tree (25)

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...

  9. 1066. Root of AVL Tree (25)

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...

随机推荐

  1. 第十节,While循环和for循环

    While循环 While循环,是一个循环加判断的组合,满足判断条件返回 真(True)开始循环代码块,不满足判断条件返回 假()不循环 格式: While 条件: 代码块 注意:在While循环里如 ...

  2. tomcat 高并发配置 与优化

    公司的一个服务器使用Tomcat6默认配置,在后台一阵全点击服务器就报废了,查了一下就要是PERMSIZE默认值过小造成(16-64) TOMCAT_HOME/bin/catalina.sh 添加一行 ...

  3. Autofac 组件、服务、自动装配(2)

    一.组件 创建出来的对象需要从组件中来获取,组件的创建有如下4种(延续第一篇的Demo,仅仅变动所贴出的代码)方式: 1.类型创建RegisterType AutoFac能够通过反射检查一个类型,选择 ...

  4. nefu 115 斐波那契的整除

    Description 已知斐波那契数列有如下递归定义,f(1)=1,f(2)=1, 且n>=3,f(n)=f(n-1)+f(n-2),它的前几项可以表示为1, 1,2 ,3 ,5 ,8,13, ...

  5. Angularjs中的ng-class

    在angular中为我们提供了3种方案处理class:1:scope变量绑定.(不推荐使用)2:字符串数组形式.3:对象key/value处理. 我们继续其他两种解决方案:1字符串数组形式是针对cla ...

  6. c语言编译命令

    第14章 预处理及用户配置文件 • 预处理命令通常在程序编译时进行一些符号处 理,其并不执行具体的硬件操作.C51语言中的预 处理命令主要有宏定义指令.文件包指令和条 件编译指令,还有其他一些调试时使 ...

  7. Apache的最小配置

    以下配置是Apache的最小配置,路径为/usr/local/apache2/conf/include/lww.xhprof.conf <VirtualHost *:> ServerNam ...

  8. 【记录】ACM计划

    ACM进阶计划ACM队不是为了一场比赛而存在的,为的是队员的整体提高.大学期间,ACM队队员必须要学好的课程有:lC/C++两种语言l高等数学l线性代数l数据结构l离散数学l数据库原理l操作系统原理l ...

  9. 硬盘安装win8系统方法汇总

    从硬盘安装 (推荐)硬盘安装方法一 使用Nt6 hdd installer进行安装,此方法适合XP,vista, Windows 7等系统. 下载Nt6 hdd installer(Win8硬盘安装工 ...

  10. LNMP环境的安装配置

    0.安装必要的依赖软件 如果已经安装了可能会进行升级,版本完全一致则不会进行任何操作. yum -y install bzip2-devel curl-devel freetype-devel gcc ...