AVL树的插入,旋转。

#include<map>
#include<set>
#include<ctime>
#include<cmath>
#include<queue>
#include<string>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std; int n; struct X
{
int val,L,R,hL,hR,fa;
}s[];
int sz,F,root=; void Insert(int x)
{
int p=root;
while()
{
if(x<s[p].val)
{
if(s[p].L==-)
{
sz++;
s[p].L=sz;
s[sz].val=x;
s[sz].fa=p;
break;
}
else p=s[p].L;
}
else
{
if(s[p].R==-)
{
sz++;
s[p].R=sz;
s[sz].val=x;
s[sz].fa=p;
break;
}
else p=s[p].R;
}
}
} void dep(int x)
{
if(s[x].L!=-) dep(s[x].L);
if(s[x].R!=-) dep(s[x].R); if(s[x].L!=-) s[x].hL = max(s[s[x].L].hL,s[s[x].L].hR)+;
else s[x].hL=; if(s[x].R!=-) s[x].hR = max(s[s[x].R].hL,s[s[x].R].hR)+;
else s[x].hR=;
} void dfs(int x)
{
if(abs(s[x].hL-s[x].hR)>) F=x; if(s[x].L!=-) dfs(s[x].L);
if(s[x].R!=-) dfs(s[x].R);
} void Left(int x)
{
int son = s[x].R; X tmpA = s[x];
X tmpB = s[son]; s[x].R = tmpB.L;
s[x].fa = son; s[son].L = x;
s[son].fa = tmpA.fa; s[tmpB.L].fa = x; if(s[son].fa!=-)
{
int Fa = s[son].fa;
if(s[Fa].L==x) s[Fa].L=son;
else s[Fa].R=son;
}
} void Right(int x)
{
int son = s[x].L; X tmpA = s[x];
X tmpB = s[son]; s[x].L = tmpB.R;
s[x].fa = son; s[son].R = x;
s[son].fa = tmpA.fa; s[tmpB.R].fa = x; if(s[son].fa!=-)
{
int Fa = s[son].fa;
if(s[Fa].L==x) s[Fa].L=son;
else s[Fa].R=son;
}
} void bfs()
{
queue<int>Q; Q.push(root);
vector<int>ans; while(!Q.empty())
{
int h = Q.front(); Q.pop();
ans.push_back(s[h].val);
if(s[h].L!=-) Q.push(s[h].L);
if(s[h].R!=-) Q.push(s[h].R);
} for(int i=;i<ans.size();i++)
{
printf("%d",ans[i]);
if(i<ans.size()-) printf(" ");
else printf("\n");
} } bool fail; void check(int x,int id)
{
if(id>n) fail=;
if(s[x].L!=-) check(s[x].L,*id);
if(s[x].R!=-) check(s[x].R,*id+);
} int main()
{
scanf("%d",&n); for(int i=;i<=;i++)
{
s[i].val=s[i].L=s[i].R=-;
s[i].hL = s[i].hR = ;
} int x; scanf("%d",&x); s[].val=x; s[].fa=-; for(int i=;i<=n;i++)
{
int x; scanf("%d",&x); Insert(x); dep(root); F=-; dfs(root);
if(F==-) continue; if(s[F].hL>s[F].hR&&s[s[F].L].hL>s[s[F].L].hR) Right(F);
else if(s[F].hL<s[F].hR&&s[s[F].R].hL<s[s[F].R].hR) Left(F);
else if(s[F].hL>s[F].hR&&s[s[F].L].hL<s[s[F].L].hR) Left(s[F].L), Right(F);
else if(s[F].hL<s[F].hR&&s[s[F].R].hL>s[s[F].R].hR) Right(s[F].R), Left(F); for(int j=;j<=sz;j++) if(s[j].fa==-) root=j;
} bfs(); fail=; check(root,);
if(fail) printf("NO\n");
else printf("YES\n"); return ;
}

PAT 1123. Is It a Complete AVL Tree (30)的更多相关文章

  1. PAT 1123 Is It a Complete AVL Tree

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

  2. PAT Advanced 1123 Is It a Complete AVL Tree (30) [AVL树]

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

  3. 1123. Is It a Complete AVL Tree (30)

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

  4. PAT甲级题解-1123. Is It a Complete AVL Tree (30)-AVL树+满二叉树

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

  5. PAT甲级1123. Is It a Complete AVL Tree

    PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了 ...

  6. 1123 Is It a Complete AVL Tree

    1123 Is It a Complete AVL Tree(30 分) An AVL tree is a self-balancing binary search tree. In an AVL t ...

  7. PAT甲级——1123 Is It a Complete AVL Tree (完全AVL树的判断)

    嫌排版乱的话可以移步我的CSDN:https://blog.csdn.net/weixin_44385565/article/details/89390802 An AVL tree is a sel ...

  8. PAT A1123 Is It a Complete AVL Tree (30 分)——AVL平衡二叉树,完全二叉树

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

  9. 1123 Is It a Complete AVL Tree(30 分)

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

随机推荐

  1. Spring实战第一部分总结

                                                         Spring实战第一部分总结 第一章 综述 1. DI依赖注入让相互协作的组件保持松散耦合,而 ...

  2. 【Android】Android之USB

    [转载请注明出处] 首先介绍一个概念:USB Host and Accessory Android通过两种模式支持一系列的USB外围设备和Android USB附件(实现了Android附件协议的硬件 ...

  3. PowerDesigner16 设置导出sql文件的编码

    一: 导出数据库结构sql脚本 Database ->  Generate  Database 在弹窗中选择Format选项卡,修改Encoding,选择自己需要的编码格式. 二:比较数据库差异 ...

  4. 用一个时钟在FPGA中计算直方图

    直方图对数字数据的分析通常是一种有用的工具.不过,要从一个直方图获得可靠的结果,必须获得大量数据,通常是要10万到100万个点.如果需要分析一个ADC的数字输出,可以采用一片FPGA(图1). 图中显 ...

  5. 【BZOJ1038】【ZJOI2008】瞭望塔 [模拟退火]

    瞭望塔 Time Limit: 10 Sec  Memory Limit: 162 MB[Submit][Status][Discuss] Description 致力于建设全国示范和谐小村庄的H村村 ...

  6. 【BZOJ】1774: [Usaco2009 Dec]Toll 过路费

    [题意]给定无向图,距离定义为边权和+最大点权,询问若干个两点最短距离.n<=250. [算法]排序+floyd [题解]考虑floyd的过程是每次找一个中转点,为了在当前找到一条新路径时方便地 ...

  7. 2017-2018-1 20179205《Linux内核原理与设计》第十周作业

    <Linux内核原理与设计>第十周作业 教材17.19.20章学习及收获 1.在Linux以及所有unix系统中,设备被分为以下三种:块设备(blkdev)以块为单位寻址,通过块设备节点来 ...

  8. 关于EditText.setText()无法显示的问题

    将EditText在初始化后调用EditText.setSaveEnabled(false); 让Android 系统不保存值,这样就不会恢复了.

  9. 文字顺时针旋转90度(纵向)&古诗词排版

    1.文字旋转90度 width: 100px; height: 200px; line-height: 100px; text-align: center; writing-mode: vertica ...

  10. dockerfile实例--安装nginx

    [root@localhost ~]# vi Dockerfile //ADD FROM centos_with_net MAINTAINER frankie onez0714@.com RUN yu ...