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. vue-cli中引入jquery的方法

    vue-cli中引入jquery的方法 以前写vue项目都没有引入过jquery,今天群里面的一位小伙伴问了我这个问题,我就自己捣鼓了一下,方法如下: 我们先进入webpack.base.conf.j ...

  2. How to Disable System Integrity Protection (rootless) in OS X El Capitan

    mac在10.11之后增加了一个功能,号称"System Integrity Protection, often called rootless",有了这个功能,以下目录的东西都不 ...

  3. MongoDB入门(7)- SpringDataMongoDB

    入门 本文介绍如何应用SpringDataMongoDB操作实体和数据库,本文只介绍最基本的例子,复杂的例子在后面的文章中介绍. SpringDataMongoDB简介 SpringDataMongo ...

  4. 51Nod 1049最大子段和 | 模板

    Input示例 6 -2 11 -4 13 -5 -2 Output示例 20 1.最大子段和模板 #include "bits/stdc++.h" using namespace ...

  5. 安装显卡后蓝屏0x00000116解决办法

    1. 亲自遇到蓝屏的问题. 2. 进入安全模式后重装系统,成功. 3. 安装驱动,软件没毛病.安装显卡驱动后,蓝屏. 4. 查看蓝屏错误代码0x00000116, 百度后查看到是显卡驱动的问题. 5. ...

  6. 【BZOJ】1666 [Usaco2006 Oct]Another Cow Number Game 奶牛的数字游戏

    [算法]贪心&&堆 [题解]反过来看就是合并任意两块木板,花费为木板长度之和. 显然从最小的两块开始合并即可,用堆(优先队列)维护. 经典DP问题石子归并是只能合并相邻两堆石子,所以不 ...

  7. Spring 框架的设计理念与设计模式分析(山东数漫江湖)

    Spring 的骨骼架构 Spring 总共有十几个组件,但是真正核心的组件只有几个,下面是 Spring 框架的总体架构图: 图 1 .Spring 框架的总体架构图 从上图中可以看出 Spring ...

  8. 基于Django Form源码开发自定义Form组件

    import copy import re class ValidateError(Exception): def __init__(self, detail): self.detail = deta ...

  9. python基础===autopep8__python代码规范

    关于PEP 8 PEP 8,Style Guide for Python Code,是Python官方推出编码约定,主要是为了保证 Python 编码的风格一致,提高代码的可读性. 官网地址:http ...

  10. Python抓取花瓣网高清美图

    一:前言 嘀嘀嘀,上车请刷卡.昨天看到了不错的图片分享网——花瓣,里面的图片质量还不错,所以利用selenium+xpath我把它的妹子的栏目下爬取了下来,以图片栏目名称给文件夹命名分类保存到电脑中. ...