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 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平衡二叉树,完全二叉树的更多相关文章
- 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 ...
- 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 ...
- PAT甲级:1064 Complete Binary Search Tree (30分)
PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...
- 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 ...
- PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)
1064 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a bin ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 1474 十进制转m进制
1474 十进制转m进制 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题解 题目描述 Description 将十进制数n转换成m进制数 m ...
- js控制两个元素高度保持一致
<script type="text/javascript"> $(function(){ if($("#left").height() > ...
- js 键盘码
键盘各按键对应的数字 keycode 9 = Tab keycode 12 = Clear keycode 13 = Enter keycode 16 = Shift keycode 17 = Con ...
- Oracle 11g即时客户端在windows下的配置
Oracle 11g即时客户端在windows下的配置 by:授客QQ:1033553122 instantclient-basic-nt-11.2.0.3.0.zip客户端压缩包为例 步骤 1. 假 ...
- 排错-lr回放错误Vuser failed to initialize extensi...解决方法
lr回放错误:Vuser failed to initialize extension LrXml.dll解决方法 by:授客 QQ:1033553122 步骤1:找到LR安装位置,打开协议目录 ...
- HBuilder开发ios App离线打包启动画面无效的解决方法
其中容易忽略的一点是manifest.json文件.plus下加入如下配置: "splashscreen": { "autoclose": false,/*如果 ...
- (网页)java中Collections.sort排序详解(转)
转自CSDN: Comparator是个接口,可重写compare()及equals()这两个方法,用于比价功能:如果是null的话,就是使用元素的默认顺序,如a,b,c,d,e,f,g,就是a,b, ...
- screen mac linux下一种让程序后台运行的方法
1: screen 场景的意思.字面意思就是软件运行在不同场景 (1)创建会话 使用命令“screen -S RunWork”来创建一个screen会话,命令执行之后,就会得到一个新的shell窗口, ...
- python第八十八天----dom js
DOM操作 1. 找到标签直接查找 document.getElementById 根据ID获取一个标签 document.getElementsByName 根据name属性获取标签集合 docum ...
- SQL去除空格、截取数据的方法:trim、substring
1.如device表中的identity字段正常的字段长度是32位,但是某些不正常的数据,后面多出空格,需要去掉后面的空格,可执行以下命令: ; ; 2.使用substring函数截取某字段的的其中一 ...