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 subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Figures 1-4 illustrate the rotation rules.
Now given a sequence of insertions, you are supposed to output the level-order traversal sequence of the resulting AVL tree, and to tell if it is a complete binary tree.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (<= 20). Then N distinct integer keys are given in the next line. All the numbers in a line are separated by a space.
Output Specification:
For each test case, insert the keys one by one into an initially empty AVL tree. Then first print in a line the level-order traversal sequence of the resulting AVL tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line. Then in the next line, print "YES" if the tree is complete, or "NO" if not.
Sample Input 1:
5
88 70 61 63 65
Sample Output 1:
70 63 88 61 65
YES
Sample Input 2:
8
88 70 61 96 120 90 65 68
Sample Output 2:
88 65 96 61 70 90 120 68
NO
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
struct Node{
int data, h;
Node *left, *right;
};
bool completeAVL = true;
int printFlag; Node *newNode(int x){
Node *p = new Node();
p->data = x;
p->left = p->right = NULL;
p->h = ;
return p;
}
int getHeight(Node *root){//获得高度
if(root == NULL) return ;
return root->h;
} int getBanance(Node *root){//获得平衡因子
if(root == NULL) return ;
return getHeight(root->left)-getHeight(root->right);
} void updateHeight(Node *root){//更新高度
if(root){
root->h = max(getHeight(root->left), getHeight(root->right))+;
}
} void R(Node *&root){//右旋
Node *p = root->left;
root->left = p->right;
p->right = root;
updateHeight(root);
updateHeight(p);
root = p;
} void L(Node *&root){//左旋
Node *p = root->right;
root->right = p->left;
p->left = root;
updateHeight(root);
updateHeight(p);
root = p;
} void insert(Node *&root, int x){
if(root == NULL){
root = newNode(x);
return;
}
if(x < root->data){
insert(root->left, x);
updateHeight(root);
if(getBanance(root) == ){
if(getBanance(root->left) == ){
R(root);
}else{//-1
L(root->left);
R(root);
}
}
}else{
insert(root->right, x);
updateHeight(root);
if(getBanance(root) == -){
if(getBanance(root->right) == -){
L(root);
}else{//
R(root->right);
L(root);
}
}
}
} void levelOrder(Node *root){
queue<Node *> q;
q.push(root);
int hasMeetLeaf = false;
while(!q.empty()){
Node* t = q.front(); q.pop();
if(t->left){
if(hasMeetLeaf) completeAVL = false;
q.push(t->left);
}else{
hasMeetLeaf = true;
}
if(t->right){
if(hasMeetLeaf) completeAVL = false;
q.push(t->right);
}else{
hasMeetLeaf = true;
}
if(-- printFlag) printf("%d ", t->data);
else printf("%d\n", t->data);
}
} int main()
{
int N;
Node *root = NULL;
scanf("%d", &N);
for(int i = ; i < N; i ++){
int x;
scanf("%d", &x);
insert(root, x);
}
printFlag = N; levelOrder(root);
if(completeAVL) printf("YES\n");
else printf("NO\n");
return ;
}
1123. Is It a Complete AVL Tree (30)的更多相关文章
- 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甲级题解-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 (30)
AVL树的插入,旋转. #include<map> #include<set> #include<ctime> #include<cmath> #inc ...
- 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
PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了 ...
- 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树的判断)
嫌排版乱的话可以移步我的CSDN:https://blog.csdn.net/weixin_44385565/article/details/89390802 An AVL tree is a sel ...
- 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甲级1123 Is It a Complete AVL Tree【AVL树】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805351302414336 题意: 给定n个树,依次插入一棵AVL ...
随机推荐
- 如何清空css 的默认边距
在网页开发中,html的元素,有部分元素默认是有内外边距的,例如body 元素,是有默认边距的 所以在通常情况下,我们都要先清空元素的内外边距:使用通配符选择器* 清空元素的内边距和外边距 ;; } ...
- 用phpstudy配置网站遇到的一些问题
第一次是配置在我本机,总是连不上数据库,后来查看到mysql.ini配置文件里面端口号有一个不是3306,更改之后就好了. 第二次是配置在笔记本电脑上,安装的时候比较顺利,也就遇到80端口被占用还有缺 ...
- gridcontrol 根据某一列数据来控制其他列合并
首先需要属性栏中设置这一列可以合并,再在CellMerge方法中写 private void gridView1_CellMerge(object sender, DevExpress.XtraGri ...
- 服务器端AJAX的Servlet代码实现
package com.itheima.servlet; import java.io.IOException; import javax.servlet.ServletException; impo ...
- Redis主从集群及哨兵模式
本次实验环境准备用一台服务器模拟3台redis服务器,1主2从 主从集群搭建 第一步:安装Redis 安装Redis,参考前面安装Redis文章,保证单机使用没有问题. 第二步:配置服务器文件 定位到 ...
- winform窗体运行时的大小和设计时不一致
窗体设置的尺寸为1946*850,而电脑分辨率是1920*1280 按说宽度已经超过屏幕大小很多了,应该显示占满屏幕宽度才对,但是运行时宽度只有设计时的一半 高度最多只能是1946像素,再拉大也不管用 ...
- spring boot + apache camel 传输文件
一 sftp搭建略 这里简单说一下为什么使用sftp.ftp和sftp各有优点,差别并不是太大.sftp安全性好,性能比ftp低.ftp对于java来说并不复杂,效率也高.之所以使用sftp主要是可以 ...
- 破损的键盘 (Broken Keyboard)--又名悲剧文本(线性表)
题目: 你有一个破损的键盘.键盘上的所有键都可以正常工作,但有时Home键或者End键会自 动按下.你并不知道键盘存在这一问题,而是专心地打稿子,甚至连显示器都没打开.当你 打开显示器之后, 展现在 ...
- grep -iq 与grep -qi 意思
就是有的时候你不需要直接打印出结果,比如在shell脚本中,你只需要知道grep有没有找到指定的字符串,而不需要满屏幕打印出来,因为那样会很难看.这只可以加-q选项,执行结果是:如果找到了,会返回0, ...
- Exp3 免杀原理与实践 20165110
Exp3 免杀原理与实践 20165110 一.. 实践内容 1.正确使用msf编码器(0.5分),msfvenom生成如jar之类的其他文件(0.5分),veil-evasion(0.5分),加壳工 ...