PAT 1123. Is It a Complete AVL Tree (30)
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)的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- PAT甲级题解-1123. Is It a Complete AVL Tree (30)-AVL树+满二叉树
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6806292.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT甲级1123. Is It a Complete AVL Tree
PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- springboot以jar包方式启动、关闭、重启脚本
springboot以jar包方式启动.关闭.重启脚本 启动 编写启动脚本startup.sh #!/bin/bash echo Starting application nohup java -ja ...
- JS学习之数组
- [USACO13FEB]出租车Taxi
洛谷题目链接:[USACO13FEB]出租车Taxi 题目描述 Bessie is running a taxi service for the other cows on the farm. The ...
- python实现堆栈、队列
一.利用python列表实现堆栈和队列 堆栈: 堆栈是一个后进先出的数据结构,其工作方式就像生活中常见到的直梯,先进去的人肯定是最后出. 我们可以设置一个类,用列表来存放栈中的元素的信息,利用列表的a ...
- UIDatePicker---iOS-Apple苹果官方文档翻译
本系列所有开发文档翻译链接地址: iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址 UIDatePicker //转载请注明出处--本文永久链接:http://www ...
- js监听浏览器后退事件
$(document).ready(function(e) { var counter = 0; if (window.history && ...
- poj 1797
2013-09-08 09:48 最大生成树,输出生成树中最短的边儿即可 或者对边儿排序,二份答案+BFS判断是否1连通N 时间复杂度都是O(NlogN)的 附最大生成树pascal代码 //By B ...
- python基础===requests学习笔记
这里有一个新的学习requests网站:http://docs.python-requests.org/zh_CN/latest/user/quickstart.html2017/11/30 Requ ...
- 64_g5
golang-github-kr-text-devel-0-0.11.git6807e77.f..> 11-Feb-2017 07:48 14250 golang-github-kr-text- ...
- C语言调用Cmd命令以及执行系统软件
C语言调用Cmd命令以及执行系统软件 http://blog.csdn.net/qq_16814591/article/details/43676377