PAT甲级题解-1123. Is It a Complete AVL Tree (30)-AVL树+满二叉树
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~
http://www.cnblogs.com/chenxiwenruo/p/6806292.html
特别不喜欢那些随便转载别人的原创文章又不给出链接的
所以不准偷偷复制博主的博客噢~~
给一个序列,对其进行AVL树的插入操作
然后输出该二叉树的层次遍历序列
若该二叉树为满二叉树,则输出YES,否则输出NO。
AVL树的插入操作,模板题,不多说了。
可以在BFS的同时,判断其是否为满二叉树。
一层层遍历,每遍历一个节点,cnt++,统计节点个数。
当第一次遇到-1的时候,若cnt=n,说明已经遍历完n个节点,为满二叉树,输出YES。
否则,说明后面还有节点,两个节点之间有空缺,不符合满二叉树的性质,输出NO。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
const int maxn=;
int n;
struct Node{
int l,r;
int val;
int h;
}; struct AVLTree{
Node node[maxn];
int cnt=;
int height(int u){
if(u==-)
return ;
return node[u].h;
}
/**
k1 is the current root,(向)右旋,顺时针旋转
对k1的左儿子L的左子树L进行了一次插入,所以是LL
*/
int RotateLL(int k1){
int k2;
k2=node[k1].l;
node[k1].l=node[k2].r;
node[k2].r=k1;
node[k1].h=max(height(node[k1].l),height(node[k1].r))+;
node[k2].h=max(height(node[k2].l),node[k1].h)+;
return k2; //new root
}
/**
k1 is the current root,(向)左旋,逆时针旋转
对k1的右儿子R的右子树R进行了一次插入,所以是RR
*/
int RotateRR(int k1){
int k2;
k2=node[k1].r;
node[k1].r=node[k2].l;
node[k2].l=k1;
node[k1].h=max(height(node[k1].l),height(node[k1].r))+;
node[k2].h=max(height(node[k2].r),node[k1].h)+;
return k2;// new root
}
/**
对k1的左儿子L的右子树R进行插入,所以是LR
先对k1的左儿子进行(向)左旋操作
再对k1进行(向)右旋操作
*/
int RotateLR(int k1){
node[k1].l=RotateRR(node[k1].l);
int root=RotateLL(k1);
return root;
}
/**
对k1的右儿子R的左子树L进行插入,所以是RL
先对k1的右儿子进行(向)右旋操作
再对k1进行(向)左旋操作
*/
int RotateRL(int k1){
node[k1].r=RotateLL(node[k1].r);
int root=RotateRR(k1);
return root;
}
/**
插入操作
就分LL\LR\RR\RL四种情况
*/
int insert_val(int val,int root){
//int res=root;
if(root==-){
node[cnt].l=node[cnt].r=-;
node[cnt].val=val;
node[cnt].h=;
root=cnt;
cnt++;
//return cnt;
}
else if(val<node[root].val){
node[root].l=insert_val(val,node[root].l);
int left=node[root].l;
int right=node[root].r;
if(height(left)-height(right)==){
if(val<node[left].val){
root=RotateLL(root);
}
else{
root=RotateLR(root);
}
}
}
else if(val>node[root].val){
node[root].r=insert_val(val,node[root].r);
int left=node[root].l;
int right=node[root].r;
if(height(left)-height(right)==-){
if(val>node[right].val){
root=RotateRR(root);
}
else{
root=RotateRL(root);
}
}
}
else{
//nothing
}
node[root].h=max(height(node[root].l),height(node[root].r))+;
return root;
}
}avltree; bool bfs(int root){
int u;
int cnt=;
bool first=true;
bool mark=true; //标记第一个-1的出现,即没有节点
queue<int>q;
q.push(root);
while(!q.empty()){
u=q.front();
q.pop();
if(u==-){
//按照层次遍历,如果第一次遍历到-1的时候,节点个数cnt=n,则为满二叉树
if(mark && cnt==n)
return true;
else{
mark=false;
continue;
}
}
else{
if(first){
printf("%d",avltree.node[u].val);
first=false;
}
else
printf(" %d",avltree.node[u].val);
cnt++;
q.push(avltree.node[u].l);
q.push(avltree.node[u].r);
}
}
return false;
} int main()
{
int root=-;
int a;
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d",&a);
root=avltree.insert_val(a,root);
} if(bfs(root)){
printf("\nYES\n");
}
else{
printf("\nNO\n");
}
return ;
}
PAT甲级题解-1123. Is It a Complete AVL Tree (30)-AVL树+满二叉树的更多相关文章
- 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 ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- pat甲级题解(更新到1013)
1001. A+B Format (20) 注意负数,没别的了. 用scanf来补 前导0 和 前导的空格 很方便. #include <iostream> #include <cs ...
- PAT甲级题解分类byZlc
专题一 字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...
- 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【AVL树】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805351302414336 题意: 给定n个树,依次插入一棵AVL ...
- PAT甲级题解-1066. Root of AVL Tree (25)-AVL树模板题
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6803291.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT甲级题解-1047. Student List for Course (25)-排序
一开始是建立了course[2501][40001]数组,存储每节课的学生编号然后for循环两层输出,但这样复杂度为O(2500*40000),也很明显导致最后时间超时后来发现最多40000学生,每个 ...
- PAT甲级题解-1057. Stack (30)-树状数组
不懂树状数组的童鞋,正好可以通过这道题学习一下树状数组~~百度有很多教程的,我就不赘述了 题意:有三种操作,分别是1.Push key:将key压入stack2.Pop:将栈顶元素取出栈3.PeekM ...
随机推荐
- gitlab hooks配置
1.邮件格式过滤 pre-recieive rev_type=commit # Only check the first commit information due to a lot of comm ...
- centos-7 虚拟机安装图形界面
centos-7 虚拟机安装图形界面 想到安装一个docker环境,于是拿出了以前装的虚拟机centos7,记得装完后,没进行任何配置(默认安装的是命令行界面). 配置网络 现有的虚拟机是没有办法联网 ...
- selenium+python自动化笔记之一
一.webdriver的API与定位元素 元素定位核心部分 元素名称 webdriver API 说明 id find_element_by_id() id定位 name find_element_b ...
- js实现Mac触摸板双指事件(上/下/左/右/放大/缩小)
前言 这几天在修复一个web问题时,需要捕获Mac触摸板双指事件(上.下.左.右.放大.缩小),但发现并没有现成的轮子,还是要自己造. 例如:jquery.mousewheel.js(添加跨浏览器的鼠 ...
- css固定背景图位置 实现屏幕滚动时 显示背景图不同区域
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- C#的static
1.static意思是静态,可以修饰类.字段.属性.方法2.标记为static的就不用创建实例对象调用了,可以通过类名直接点出来3.static三种用法:4.用于变量前,表示每次重新使用该变量所在方法 ...
- maven 打包生成doc和源码插件
<!--配置生成Javadoc包--> <plugin> <groupId>org.apache.maven.plugins</groupId> < ...
- 关于PHP中浏览器禁止Cookie后,Session能使用吗?
sessionid是存储在cookie中的,解决方案如下: Session URL重写,保证在客户端禁用或不支持COOKIE时,仍然可以使用Session session机制.session机制是一种 ...
- 项目Alpha冲刺(团队)-第六天冲刺
格式描述 课程名称:软件工程1916|W(福州大学) 作业要求:项目Alpha冲刺(团队)-代码规范.冲刺任务与计划 团队名称:为了交项目干杯 作业目标:描述第六天冲刺的项目进展.问题困难.心得体会 ...
- mvc:message-converters简单介绍
说说@ResponseBody注解,很明显这个注解就是将方法的返回值作为reponse的body部分.我们进一步分析下这个过程涉及到的内容,首先就是方法返回的类型,可以是字节数组.字符串.对象引用等, ...