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 <stdio.h>
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
#include <queue>
#include <set>
using namespace std;
struct node{
int data,height;
node *lchild,*rchild;
};
node* newNode(int v){
node* root = new node;
root->data = v;
root->lchild = root->rchild = NULL;
root->height = ;
return root;
}
int getHeight(node* root){
if(root==NULL)return ;
return root->height;
}
void updateHeight(node* root){
root->height = max(getHeight(root->lchild),getHeight(root->rchild))+;
}
int getBalanceFactor(node *root){
return getHeight(root->lchild)-getHeight(root->rchild);
}
void L(node* &root){
node* tmp=root->rchild;
root->rchild = tmp->lchild;
tmp->lchild = root;
updateHeight(root);
updateHeight(tmp);
root=tmp;
}
void R(node* &root){
node* tmp=root->lchild;
root->lchild = tmp->rchild;
tmp->rchild = root;
updateHeight(root);
updateHeight(tmp);
root = tmp;
}
void insert(node* &root,int v){
if(root==NULL){
root = newNode(v);
return;
}
if(v<root->data){
insert(root->lchild,v);
updateHeight(root);
if(getBalanceFactor(root)==){
if(getBalanceFactor(root->lchild)==){
R(root);
}
else if(getBalanceFactor(root->lchild)==-){
L(root->lchild);
R(root);
}
}
}
else{
insert(root->rchild,v);
updateHeight(root);
if(getBalanceFactor(root)==-){
if(getBalanceFactor(root->rchild)==-){
L(root);
}
else if(getBalanceFactor(root->rchild)==){
R(root->rchild);
L(root);
}
}
}
}
node* create(int data[],int n){
node* root = NULL;
for(int i=;i<n;i++){
insert(root,data[i]);
}
return root;
}
int flag=,after=;
void levelOrder(node* root,int n){
queue<node*> q;
int num=;
q.push(root);
while(!q.empty()){
node* now = q.front();
num++;
printf("%d",now->data);
if(num!=n)printf(" ");
else printf("\n");
q.pop();
if(now->lchild!=NULL){
if(after==)flag=;
q.push(now->lchild);
}
else after=;
if(now->rchild!=NULL){
if(after==)flag=;
q.push(now->rchild);
}
else after=;
}
}
int main(){
int n;
scanf("%d",&n);
int data[];
for(int i=;i<n;i++){
scanf("%d",&data[i]);
}
node* root = create(data,n);
levelOrder(root,n);
printf("%s",flag==?"YES":"NO");
}

注意点:第一次做到平衡二叉树和完全二叉树的判定的题目,重新看了一遍算法笔记,还是很生疏。AVL的插入左旋右旋要熟练记住,考前再看一眼。

完全二叉树的判定:层序遍历时,出现了有子节点为空的节点,后面的节点还出现子节点非空的情况,这就不是完全二叉树

PAT A1123 Is It a Complete AVL Tree (30 分)——AVL平衡二叉树,完全二叉树的更多相关文章

  1. 1066 Root of AVL Tree (25分)(AVL树的实现)

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

  2. PAT甲级:1066 Root of AVL Tree (25分)

    PAT甲级:1066 Root of AVL Tree (25分) 题干 An AVL tree is a self-balancing binary search tree. In an AVL t ...

  3. PAT甲级:1064 Complete Binary Search Tree (30分)

    PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...

  4. PAT 甲级 1066 Root of AVL Tree (25 分)(快速掌握平衡二叉树的旋转,内含代码和注解)***

    1066 Root of AVL Tree (25 分)   An AVL tree is a self-balancing binary search tree. In an AVL tree, t ...

  5. PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)

    1064 Complete Binary Search Tree (30 分)   A Binary Search Tree (BST) is recursively defined as a bin ...

  6. PTA 04-树6 Complete Binary Search Tree (30分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/669 5-7 Complete Binary Search Tree   (30分) A ...

  7. PTA 04-树5 Root of AVL Tree (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/668 5-6 Root of AVL Tree   (25分) An AVL tree ...

  8. PAT-2019年冬季考试-甲级 7-4 Cartesian Tree (30分)(最小堆的中序遍历求层序遍历,递归建树bfs层序)

    7-4 Cartesian Tree (30分)   A Cartesian tree is a binary tree constructed from a sequence of distinct ...

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

随机推荐

  1. ES6学习之关键字

    前言:什么是ES6?ECMAScript 6(简称ES6)是JavaScript语言的下一代标准,已经在2015年6月正式发布了.其中相比较于ES5新增了诸多的特性,并且ES6可转换为ES5的语法.- ...

  2. Django框架理解和使用常见问题

    1.什么是中间件? 中间件是介于request与response处理之间的一道处理过程,相对比较轻量级,并且在全局上改变django的输入与输出. 中间件一般做认证或批量请求处理,django中的中间 ...

  3. 【CF932E】Team Work(第二类斯特林数)

    [CF932E]Team Work(第二类斯特林数) 题面 洛谷 CF 求\(\sum_{i=1}^nC_{n}^i*i^k\) 题解 寒假的时候被带飞,这题被带着写了一遍.事实上并不难,我们来颓柿子 ...

  4. 2017-11-06 日语编程语言"抚子" - 第三版特色初探

    "中文编程"知乎专栏原链 原文: 日语编程语言"抚子" - 第三版特色初探 它山之石可以攻玉. 学习其他的母语编程语言, 相信对中文编程语言的设计和实践有借鉴意 ...

  5. jsPlumb.jsAPI阅读笔记(官方文档翻译)

    jsPlumb DOCS 公司要开始做流程控制器,所以先调研下jsPlumb,下文是阅读jsPlumb提供的document所产生的归纳总结 setup 如果不使用jQuery或者类jQuery库,则 ...

  6. DNS协议总结

    1.DNS用于根据域名返回ip地址. 2.一般情况下,DNS-server是通过在UDP协议与客户端之间交互的,UDP端口号是53. 特别注意.DNS有时会使用TCP 53端口与客户端进行交互,所以, ...

  7. 安卓开发_浅谈TimePicker(时间选择器)

    TimePicker也继承自FrameLayout类.时间选择控件向用户显示一天中的时间(可以为24小时,也可以为AM/PM制),并允许用户进行选择.如果要捕获用户修改时间数据的事件,便需要为Time ...

  8. id、name、setter方法注入、构造方法注入、工厂方法注入、注解注入、方法注入、方法替换、Web作用域、普通bean引用Web作用域的bean

    spring IoC的id和name id的命名需要满足XML对id的命名规范,必须以字母开始,后面可以是字母.数字.连字符.下画线.句号.冒号等等号,但逗号和空格是非法的.如果用户确实希望用一些特殊 ...

  9. centos6分辨率设置

    问题描述 centos 6.9最小化安装后, 分辨率会很大, 当然也可以最小化VM虚拟机, 但是有强迫症的朋友可以设置一下. 解决方法 打开/etc/grub.conf配置文件, 在kernel 的最 ...

  10. P进制转Q进制

    // 对一个P进制的数,如果要转换成Q进制的数 // 1)将P进制数x转换成十进制数y int y=0,product=1;//product在循环中会不断成P,得到1.P^2..... while( ...