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. angular 搜索记录保留

    #方法1: 点击后退到home后,再点击搜索, locationChangeStart 事件会多次触发. # $scope.keyword = $location.search().search # ...

  2. 监控 monitor java 代码

    /* * To change this license header, choose License Headers in Project Properties. * To change this t ...

  3. dfs.replication 参数 动态修改

    首先 dfs.replication这个参数是个client参数,即node level参数.需要在每台datanode上设置.其实默认为3个副本已经够用了,设置太多也没什么用. 一个文件,上传到hd ...

  4. OSPF 原理

    关于OSPF的数据结构Link-State Protocol Data Structures链路状态路由器与距离矢量路由器,可以知道关于整个网络的更多信息Neighbor table:also kno ...

  5. Stu Website

    GIT: 分支的新建与合并 https://git-scm.com/book/zh/v2/Git-分支-分支的新建与合并 分支的管理 https://git-scm.com/book/zh/v1/Gi ...

  6. mongodb备份

    一:数据导出,数据导入作为DBA(管理员),经常会碰到导入导出数据的需求,下面介绍实用工具(自带的):1:数据导入 mongoexport导出csv格式的文件./mongoexport -d Test ...

  7. C#获取周的第一天、最后一天、月第一天和最后一天

    [csharp] view plaincopyprint? public class DateTimeTool { /// <summary> /// 获取指定日期所在周的第一天,星期天为 ...

  8. ORA-12170: TNS:Connect timeout occurred

    VM 作为ORACLE 服务器,客户端登陆提示超时,本地连接使用网络连接正常. D:>sqlplus system/oracle123@//192.168.63.121:15021/pdb01 ...

  9. springmvc+mybatis下载项目自带模板

    1.首先如果要获取javaweb项目中的文件在哪,用到的代码:request.getSession().getServletContext().getRealPath("/WEB-INF/d ...

  10. makefile的编写规则

    2.       编写makefile 示例: test:main.o func.o gcc -o test main.o func.o func.o:func.c gcc -c func.c mai ...