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)的更多相关文章

  1. 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 ...

  2. PAT甲级题解-1123. Is It a Complete AVL Tree (30)-AVL树+满二叉树

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6806292.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  3. PAT 1123. Is It a Complete AVL Tree (30)

    AVL树的插入,旋转. #include<map> #include<set> #include<ctime> #include<cmath> #inc ...

  4. 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 ...

  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(30 分)

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...

  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 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 ...

  9. PAT甲级1123 Is It a Complete AVL Tree【AVL树】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805351302414336 题意: 给定n个树,依次插入一棵AVL ...

随机推荐

  1. Linux查看与挂载新磁盘

    问题 把CentOS都换成了Ubuntu Server(16.04 LTS),用df -h查看磁盘占用情况,确发现之前插入的一块大容量磁盘/dev/sdb1消失了.是磁盘坏了?还是没被系统识别? 解决 ...

  2. python杂记二

    1. 写文件可以直接使用print函数 file_name = open("file_name.txt","w") print("file conta ...

  3. Linux命令学习——strings

    strings命令的作用是打印文件中的可打印字符. 常用的参数有: -f --print-file-name,在每个输出的字符串前打印文件名. -t [radix] --radix=[radix],输 ...

  4. linux 之用户管理

    用户的家目录 普通用户:/home/用户名/,所有者和所属组都是此用户,权限是700 超级用户:/root/,所有者和所属组都是root用户,权限是550 用户邮箱目录 /var/spool/mail ...

  5. 有关于并发中的死锁(Deadlock)、饥饿(Starvation)、活锁(Livelock)

    最近在看<实战Java高并发程序设计>,发现了之前没有接触过的几个名词. 死锁:之前在接触多线程的时候,接触过死锁的情况.死锁是线程中最糟糕的情况,如下面的图中的四辆车子一样,如果没有一辆 ...

  6. 小A买彩票-(组合数)

    链接:https://ac.nowcoder.com/acm/contest/549/C来源:牛客网 题目描述 小A最近开始沉迷买彩票,并且希望能够通过买彩票发家致富.已知购买一张彩票需要3元,而彩票 ...

  7. elasticsearch批量修改,批量更新某个字段

    1.源生API 在这里没有用官方提供的bulk API,而是用的另外一种方式. POST /infomations/infomations/_update_by_query JSON请求格式 { &q ...

  8. Java学习笔记(二十四):单例设计模式singleton

    为什么要使用singleton设计模式? 假设设计了一个操作数组的工具类(Tools),里面有一个锤子的方法(Hammer),如果不使用singleton设计模式,每次想调用Hammer方法都需要ne ...

  9. 一起学libcef--搭建自己的libcef运行环境(Win32程序,错误C2220解决方案)

    转自 http://blog.csdn.net/wangshubo1989/article/details/50180413 现在就来讲讲如何在自己的win32程序中搭建libcef运行环境. 首先就 ...

  10. Dockerfile的常见命令

    FROM 格式: FROM  <image> 或者  FROM <image>:<tag> FROM指令的功能是为后面的指令提供基础镜像,所以该指令一定是Docke ...