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<math.h>
#include<queue>
using namespace std;
struct node{
int value, depth;
node* l=NULL;
node* r=NULL;
node(int v): value(v), depth(0), l(NULL), r(NULL){
}
};
int getheight(node* root){
return root==NULL?0:max(getheight(root->l), getheight(root->r))+1;
} node* RotationLL(node* root){
node* temp=root->l;
root->l=temp->r;
temp->r=root;
temp->depth=getheight(temp);
root->depth=getheight(root);
return temp;
} node* RotationRR(node* root){
node* temp=root->r;
root->r=temp->l;
temp->l=root;
temp->depth=getheight(temp);
root->depth=getheight(root);
return temp;
} node* RotationLR(node* root){
root->l=RotationRR(root->l);
return RotationLL(root);
} node* RotationRL(node* root){
root->r=RotationLL(root->r);
return RotationRR(root);
} node* insert(node* root, int val){
if(root==NULL){
root=new node(val);
return root;
}else if(val<root->value){
root->l=insert(root->l, val);
if(getheight(root->l)-getheight(root->r)==2)
if(val<root->l->value)
root=RotationLL(root);
else
root=RotationLR(root); }else{
root->r=insert(root->r, val);
if(getheight(root->l)-getheight(root->r)==-2)
if(val<root->r->value)
root=RotationRL(root);
else
root=RotationRR(root);
}
root->depth=getheight(root);
return root;
}
int main(){
int n, flag=0, ans=0, first=0;
cin>>n;
node* root=NULL;
for(int i=0; i<n; i++){
int val;
cin>>val;
root=insert(root, val);
}
queue<node*> q;
q.push(root);
while(!q.empty()){
node* temp=q.front();
first++==0?cout<<temp->value:cout<<" "<<temp->value;
q.pop();
if(temp->l!=NULL){
q.push(temp->l);
flag==1?ans=1:ans=ans;
}
else
flag=1;
if(temp->r!=NULL){
q.push(temp->r);
flag==1?ans=1:ans=ans;
}
else
flag=1;
}
cout<<endl;
ans==1?cout<<"NO"<<endl:cout<<"YES"<<endl;
return 0;
}

PAT 1123 Is It a Complete AVL Tree的更多相关文章

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

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

  2. PAT甲级1123. Is It a Complete AVL Tree

    PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了 ...

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

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

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

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

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

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

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

随机推荐

  1. openssh常用命令记录

    command description date ssh [user@]hostname[:port] 登录远程机器 2017-03-21 scp <local_file> <use ...

  2. ssh使用秘钥文件连接提示WARNING: UNPROTECTED PRIVATE KEY FILE!(转载)

    转自:http://www.01happy.com/ssh-unprotected-private-key-file/ 在centos 6.4下使用ssh连接远程主机时,用的是另外一个密钥,需要用-i ...

  3. php 批量检测bom头,去除bom头工具

    <?php //有些php文件由于不小心保存成了含bom头的格式而导致出现一系列的问题.以下是批量清除bom头的代码 if (isset ( $_GET ['dir'] )) { //confi ...

  4. CSS3常用知识点

    CSS3常用知识点 1 css3选择器 1.1 属性选择器 /* E[attr~=val] 表示的一个单独的属性值 这个属性值是以空格分隔的*/ .attr2 a[class~="kawa& ...

  5. C# 类型转换方法

    C# 类型转换方法 C# 提供了下列内置的类型转换方法: 序号 方法 & 描述 1 ToBoolean 如果可能的话,把类型转换为布尔型. 2 ToByte 把类型转换为字节类型. 3 ToC ...

  6. 解决:阿里云ECS上启动tomcat后,第一次访问时间特别长

    Re在ECS上启动tomcat后,第一次访问时间特别长      2017-04-25 10:16:04 INFO com.world.socket.ServerSocketListener  25- ...

  7. 关于java中replace的用法

    今天突然看到Java中的replace有两种方法,一种是直接替换,另一种是可以进行匹配替换的方式: public String replace(CharSequence target, CharSeq ...

  8. 我要上google

    我要上google 一.下载google浏览器(百度下载) 二.获取和运行xx-net 1.https://github.com/XX-net/XX-Net 2.解压下载的xx-net,运行文件夹中的 ...

  9. 87. [NOIP2000] 乘积最大

    ★☆   输入文件:cjzd.in   输出文件:cjzd.out   简单对比 时间限制:1 s   内存限制:128 MB 问题描述 今年是国际数学联盟确定的“2000——世界数学年”,又恰逢我国 ...

  10. js操作元素透明度以及浏览器兼容性

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...