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. JavaSE List集合

    我们掌握了Collection接口的使用后,再来看看Collection接口中的子接口和实现类,他们都具备那些特性呢? 接下来,我们一起学习Collection中的常用几个子接口: ​ java.ut ...

  2. XHTML结构化

    XHTML 规则概要 将传统的 HTML 转换为 XHTML 1.0 是快捷且无痛的,只要你遵守一些简单的规则和容易的方针.不管是否使用过 HTML,都不会妨碍你使用 XHTML. 使用恰当的文档类型 ...

  3. Linux 学习笔记之超详细基础linux命令 Part 1

    Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122   说明:主要是在REHL Server 6操作系统下进行的测试 --字符界面虚拟终端与图形界面之间的切 方法:[ ...

  4. java设计模式学习

    每次面试都需要看设计模式,每次都很好的理解了,但是实际开发中没有应用总是忘记.现在把它汇总一下. 二十三种设计模式 总体来说设计模式分为三大类: 创建型模式,共五种:工厂方法模式.抽象工厂模式.单例模 ...

  5. recovery 升级过程LED灯闪烁

    Android设备在进入recovery升级的过程,我们在屏幕上面可以看到升级的机器人动画,以及升级的进度显示.这仅限于有屏幕的设备,比如平板PAD,电视TV等,对与没有屏幕的盒子BOX,那么在不接入 ...

  6. C语言编程比赛WBS

  7. solr-query

    解释: 1.query:获取全部数据的SQL 2.deltaImportQuery:获取增量数据时使用的SQL 3.deltaQuery:获取pk的SQL 4.parentDeltaQuery:获取父 ...

  8. python接口测试—get请求(一)

    python 做借口测试用到的是requests模块,首先要导入requests库,pip install requests 1.get直接请求方式 以豆瓣网为例: url = 'https://re ...

  9. Nginx:413 Request Entity Too Large

    现象:在 Post 文件的时候遇到413 错误 :Request Entity Too Large: 原因:Nginx 限制了上传文件的大小,需在Nginx中修改/增加允许的最大文件大小: 操作:编辑 ...

  10. asp在线压缩和解压缩文件(文件夹)

    <%'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'\\'\\ 1. c:\windows\system32\cmd.e ...