A1123. 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 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<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef struct NODE{
struct NODE* lchild, *rchild;
int data, lev;
}node;
int N, cnt = ;
int height(node* root){
if(root == NULL)
return ;
else return root->lev;
}
void update(node* root){
root->lev = max(height(root->lchild), height(root->rchild)) + ;
}
void L(node* &root){
node* temp = root;
root = root->rchild;
temp->rchild = root->lchild;
root->lchild = temp;
update(temp);
update(root);
}
void R(node* &root){
node* temp = root;
root = root->lchild;
temp->lchild = root->rchild;
root->rchild = temp;
update(temp);
update(root);
}
void insert(node* &root, int x){
if(root == NULL){
root = new node;
root->lchild = NULL;
root->rchild = NULL;
root->data = x;
root->lev = ;
return;
}
if(x <= root->data){
insert(root->lchild, x);
update(root);
if(abs(height(root->lchild) - height(root->rchild)) == ){
if(height(root->lchild->lchild) - height(root->lchild->rchild) == ){
R(root);
}else if(height(root->lchild->lchild) - height(root->lchild->rchild) == -){
L(root->lchild);
R(root);
}
}
}else{
insert(root->rchild, x);
update(root);
if(abs(height(root->lchild) - height(root->rchild)) == ){
if(height(root->rchild->rchild) - height(root->rchild->lchild) == ){
L(root);
}else if(height(root->rchild->rchild) - height(root->rchild->lchild) == -){
R(root->rchild);
L(root);
}
}
}
} int levelOrder(node* root){
int tag = , prt = ;
queue<node*> Q;
Q.push(root);
while(Q.empty() == false){
node* temp = Q.front();
Q.pop();
cnt++;
if(temp == NULL){
if(cnt < N + )
tag = ;
}else{
prt++;
if(prt == N)
printf("%d\n", temp->data);
else printf("%d ", temp->data);
Q.push(temp->lchild);
Q.push(temp->rchild);
}
}
return tag;
}
int main(){
scanf("%d", &N);
int num;
node* root = NULL;
for(int i = ; i < N; i++){
scanf("%d", &num);
insert(root, num);
}
int isCom = levelOrder(root);
if(isCom == )
printf("YES\n");
else printf("NO\n");
cin >> N;
return ;
}
总结:
1、按插入顺序建立平衡二叉树,然后再判断该树是否是完全二叉树。
2、建立平衡二叉树: 在左子树插入后,先更新根节点高度,再求平衡因子。求平衡因子判断平衡应判断左减右是否等于2,而不是绝对值。
3、左旋右旋共三步。旋转完成之后必须更新temp和root的高度,由于temp会成为root的子树,所以先更新temp高度,再更新root。
4、判断完全二叉树: 将null节点也加入队列。设置计数器记录访问节点个数,当访问时遇到空节点看计数器是否大于N,如果否,则不是完全二叉树。
A1123. Is It a Complete AVL Tree的更多相关文章
- 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 ...
- PAT甲级——A1123 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 ...
- PAT_A1123#Is It a Complete AVL Tree
Source: PAT A1123 Is It a Complete AVL Tree (30 分) Description: An AVL tree is a self-balancing bina ...
- PAT甲级1123. Is It a Complete AVL Tree
PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- Artifact project04:war :Error during artifact deployment. See server log for details
困扰了我好长时间,我的错误是 先 Run clean 再package就成功了.
- hive条件函数
case相当于if,when相当于=:then是条件满足的结论.否则实行else后语句,一end结束
- Java Json 数据下划线与驼峰格式进行相互转换
概述 今天遇见一个需求,需要对json数据进行下划线与驼峰格式之间进行转换,在Fastjson.Jackson.Gson都提供了转换的方式,在这里进行一下列举. User类: public class ...
- 【转】解决Maxwell发送Kafka消息数据倾斜问题
最近用Maxwell解析MySQL的Binlog,发送到Kafka进行处理,测试的时候发现一个问题,就是Kafka的Offset严重倾斜,三个partition,其中一个的offset已经快200万了 ...
- Spring JDBC模版以及三种数据库连接池的使用
jar包版本有点乱,直接忽略版本号,将就一下. 这里引了aop包是因为在spring3版本之后用模版对数据库库操作时会出现问题,但是不会报错,也没有提示. 所以这里直接引入,以及之后会用到的DBCP与 ...
- todo项目总结
vue+webpack项目工程配置 1.vue-loader+webpack项目配置 2.webpack配置项目加载各种静态资源 3.webpack-dev-server的配置和使用 安装: pack ...
- 手机连接WiFi有感叹号x怎么回事?如何消除手机WiFi感叹号?
经过多年的革新,现在的安卓系统已经非常优秀了,某些程度已经超越iOS,卡顿和耗电也不再是安卓系统的代名词了.而为了体验到最优秀的安卓系统,不少人都会购买海外的手机,因为海外手机的安卓系统都比较精简,非 ...
- Jquery中val方法使用的坑
Jquery中val方法使用 val()// 取得第一个匹配元素的当前值 val(val)// 设置所有匹配元素的值 val([val1, val2])// 设置多选的checkbox.多选selec ...
- springcloud 学习
参考: spring cloud 入门系列一:初识spring cloud http://blog.didispace.com/Spring-Cloud%E5%9F%BA%E7%A1%80%E6%95 ...
- IDM下载神器
破解版IDM 个人评价:基本上能满足日常下载需求,除bt.磁力外. 不管是在线视频, 还是音乐, 电子书, 统统都能下载, 还附有爬虫功能~~ 直接附链接: 百度云链接: https://pan.ba ...