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 ...
随机推荐
- 2015/8/30 Python基础(4):序列操作符
序列是指成员有序排列,可以通过下标偏移量访问的类型.Python序列包括:字符串.列表和元组.序列的每个元素可以指定一个偏移量得到,多个元素是通过切片操作得到的.下标偏移量从0开始计数到总数-1结束. ...
- C11简洁之道:循环的改善
1. for循环的新用法 在C++98/03中,通过for循环对一个容器进行遍历,一般有两种方法,常规的for循环,或者使用<algorithm>中的for_each方法. for循环遍 ...
- flask 自定义url转换器
from werkzeug.routing import BaseConverter app = Flask(__name__) class TeleConveter(BaseConverter): ...
- Gradle加载本地jar包
有时,我们需要的jar包不一定能在远程仓库中找到,这时我们需要加载本地的jar包. 加载单独的jar包 在项目底下添加libs目录,将jar包仍进libs目录 build.gradle配置如下: de ...
- 铺砖问题 (状态压缩dp)
问题描述: 给定m×n个格子,每个格子被染成了黑色或白色.现在要用1×2的砖块覆盖这些格子,要求快于快之间互相不重叠,且覆盖了所有白色的格子(用 . 表示),但不覆盖任意一个黑色的格子(用 x 表示) ...
- HTML跳转新窗口的方法
笔试遇到这样的一个问题,特意整理一下. 方法一 纯HTML <a href="http://www.cnblogs.com" target="_blank" ...
- 关于js闭包官方解释庖丁解牛式理解
闭包:是一个拥有许多变量和绑定了这些变量的环境的表达式(通常是一个函数),因而这些变量也是该表达式的一部分. 变量+环境 首先按这个句子主谓宾来分解.闭包是一个表达式,通常是一个函数. 这意味着第一它 ...
- 基于ARM 构架(带MMU)的copy_from_user与copy_to_user详细分析
[转自:http://blog.chinaunix.net/uid-20543672-id-3195249.html] 在学习Linux内核驱动的时候,一开始就会碰到copy_from_use ...
- C基础 读写锁中级剖析
引言 读写锁 是为了 解决, 大量 ''读'' 和 少量 ''写'' 的业务而设计的. 读写锁有3个特征: 1.当读写锁是写加锁状态时,在这个锁被解锁之前,所有试图对这个锁加锁的线程都会被阻塞 2.当 ...
- Kettle提高输入输出数据总结
1 mysql在数据连接是可以通过设置一下三个三处的方式 useServerPrepStmts=false useCursorFetch=true useCompression= ...