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. Chapter 2 Open Book——19

    He bent over and began scraping together a pile of the white mush. 他弯下腰,开始团起一个白色的雪球. "I'll see ...

  2. 嵌套表SHAPE

    SQL语法 SHAPE {<master query>} APPEND ({ <child table query> } RELATE <master column> ...

  3. hdu_5748_Bellovin(LIS)

    题目链接:hdu_5748_Bellovin 题意: 给你一个数列ai,设f(a1,a2,a3,..an)=(f1,f2,f3,...,fn),其中fi表示以ai结尾的最长递增子序列长度,注意:必须要 ...

  4. shell脚本学习(二)

    4.cat命令 1)  cat -s    摆脱多余的空白行 2)  cat -T    将制表符显示为^I 3)  cat -n    显示行号 4) cat -b    跳过空白行,然后显示行号 ...

  5. LeetCode OJ 223.Rectangle Area

    Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...

  6. servlet第2讲(下集)----创建servlet实例(继承GenericServlet)

  7. 经典.net面试题目(3)

    1. asp.net中web应用程序获取数据的流程: A.Web Page B.Fill  C.Sql05  D.Data Sourse  E.DataGrid  F.DataSet  G.Selec ...

  8. how computer boot up?

    The power button activates the power supply in the PC, sending power to the motherboard and other co ...

  9. url重写步骤

    url重写:在global文件中,在application_BeginRequest事件中1:获取URL string url=Request.AppRelativeCurrentExecutionF ...

  10. RichTextEditor 文本编辑

    <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...