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. NOI2001 方程的解数

    1735 方程的解数 http://codevs.cn/problem/1735/ 2001年NOI全国竞赛  时间限制: 5 s  空间限制: 64000 KB     题目描述 Descripti ...

  2. Java——Iterate through a HashMap

    遍历Map import java.util.*; public class IterateHashMap { public static void main(String[] args) { Map ...

  3. 省队集训 Day5 选举

    [题目大意] 小奇和魔法猪要竞选膜钟国的总统. 有 $n$ 个选民,编号为$1...n$,他们中有的人支持小奇,有的人支持魔法猪,还有的人保持中立. 现在你需要把选民分成若干个区间,每个区间的长度在$ ...

  4. 【STSRM10】数学上来先打表

    [算法]DP+数学计数 [题意]给出n个点(不同点之间有区别),求出满足下列条件的连边(双向边)方案(对1004535809取模): 1.每条边连接两个不同的点,每两个点之间至多有一条边. 2.不存在 ...

  5. 在前端发起ajax遇到问题

    1.请注意设置datatype的类型. 如下图:

  6. bzoj 1046 LIS

    假设我们知道以每个点开始到最后的最长上升序列,设为w[i],这样首先我们在w值中取max,如果询问的值比max大,这样显然就是无解,如果小的话,我们需要求出来字典序最小的方案. 那么对于所有i,我们肯 ...

  7. 关于ssh三大框架整合的碎碎念

    三大框架整合,无非就是一个导jar包,修改配置文件的过程.完了就没事了. 还是有很多细节性的问题 比如在spring中写applicationContext.xml文件时不提示: 解决方法如下: 如果 ...

  8. frp 使用入门

    1.下载安装对应系统版本 https://github.com/fatedier/frp/releases/ 2.将下载的frp移动到系统软件目录 mv frp/ /usr/local 3.配置frp ...

  9. .gitignore 文件添加或更新后规则无效的解决方案

    项目已经提交之后,突然想忽略某个文件或目录 A,于是在 .gitignore 里添加了忽略规则.但是提交(commit)之后,发现一旦修改了 A,git 同样会检测到 A 的变化(changes) , ...

  10. sublime Text3 === 无法输入input的问题解决办法

    sublimetext无法对input或者raw_input执行.因此搜了很多方法后,解决了这个问题: 1.先下载插件sublimerepl ,如果无法下载,请点击https://github.com ...