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 ...
随机推荐
- TinkPHP框架学习-04命名空间
1-----命名空间 2-----调用其他控制器的方法 3-----U()函数 一命名空间 ①看做是虚拟目录 --ThinkPHP/Library 初始命名空间 --在Library文件夹下的所有文件 ...
- Java mysql
- JAVA_Class.forName()用法详解
Class.forName(xxx.xx.xx)返回的是一个类. Class.forName(xxx.xx.xx)的作用是要求JVM查找并加载指定的类,JVM会执行该类的静态代码段. Class.fo ...
- True和数字相加的结果
num = 5 print(num + True) # 其结果为6
- Dev破解组件。
将UI项目中的properties的license.lcx内容清空
- 1_translation_1
It is always difficult to start describing a programming language because little details do not make ...
- el-input的color修改无效问题
相信很多前端初学者跟我一样也遇到过el-input的color修改无效问题 如下图:我想把el-input里面的文字改成蓝色,但是使用总是失败 修改方法:打开调试界面,找到el-input对应的sty ...
- Game Engine Architecture 1
[Game Engine Architecture 1] 1.This book is really just the beginning of a fascinating and potential ...
- mysql修改联合主键
参考 https://blog.csdn.net/BockSong/article/details/80933477 alter table TABNAME drop primary key; alt ...
- 修改Windows server 时间同步
1.关闭“与Internet时间同步”选项. 2.禁用Windows时间服务,并将其设置为手动. 3.禁用Hyper-v时间同步服务,并将其设置为手动,这个在Hyper-v软件上选中要修改的虚拟机,设 ...