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. Java中ArrayList的删除元素总结

    Java中循环遍历元素,一般有for循环遍历,foreach循环遍历,iterator遍历. 先定义一个List对象 List<String> list = new ArrayList&l ...

  2. arcgis point 随着 line类型的轨迹运动的动画

    animate : function(frampoint,topoint,speed = 60){ var path = topoint.geometry.paths[0]; var i = 1; v ...

  3. java 深度复制与浅复制 copyOf、arraycopy、copyOfRange

    1.copyOf 原型:public static <T> T[] copyOf(T[] original, int newLength) original:原数组 newLength:要 ...

  4. 其它 nginx

    原地址忘了 常用命令: 在CMD下输入如下命令,可以强行关闭所有Nginx进程 taskkill /f /im nginx.exe start nginx nginx -s stop nginx -s ...

  5. 浅谈openstack中使用linux_bridge实现vxlan网络

    openstack环境: 1 版本:ocata 2 系统:ubuntu16.04.2 3 控制节点 1个 + 计算节点 1个 4 控制节点网卡为ens33,ip = 172.171.5.200 ens ...

  6. linux查看进程已经运行了多长时间

    ps -eo lstart 启动时间 ps -eo etime 运行多长时间. ps -eo pid,lstart,etime | grep 717

  7. c3p0数据源的第一次尝试

    开始补习 以前学习过的基础 正在尝试从c3p0 获取到connection 好的,首先上代码吧 public static DataSource ds = null; static { ComboPo ...

  8. maven工程 添加本地jar依赖

    和第三方平台对接的时候要用到对方提供的一个jar包,jar包怎么直接添加到pom文件的依赖中呢? jar包一般都是公共的,要上传到私服仓库.我们都是直接登录私服,操作仓库. 登录私服可以在项目的pom ...

  9. my.conf配置信息

    # mysql conf /etc/my.cnf# Created by http://www.wdlinux.cn# Last Updated 2010.06.01 [client]port = 3 ...

  10. 通行导论-IP数据网络基础(2)

    传输控制协议(TCP) 差错控制:TCP使用差错控制提供可靠性,包括检测受到损伤.丢失.失序的报文段 实现方法:1.16位检验和,2.确认机制:采用确认证实收到的报文段,3.重传(设置一个重传超时RT ...