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 ...
随机推荐
- python爬虫之正则表达式
一.简介 正则表达式,又称正规表示式.正规表示法.正规表达式.规则表达式.常规表示法(英语:Regular Expression,在代码中常简写为regex.regexp或RE),计算机科学的一个概念 ...
- 创建安全客户端Socket
SocketFactory factory = SSLSocketFactory.getDefault(); Socket socket = factory.create("localhos ...
- Django--CRM--一级, 二级 菜单表
一. 一级菜单表 1. 首先要修改权限表的字段, 在权限表下面加上icon和 is_menu 的字段 2. 展示结果 # 我们既然想要动态生成一级菜单,那么就需要从数据库中拿出当前登录的用户的菜单表是 ...
- Java 8 函数式接口
函数式接口(Functional Interface)就是一个有且仅有一个抽象方法,但是可以有多个非抽象方法的接口. 函数式接口可以被隐式转换为 lambda 表达式. Lambda 表达式和方法引用 ...
- 二、Docker部署应用
一.有关Docker的安装请参考docker官网 Docker 提供了两个版本:社区版 (CE) 和企业版 (EE). Docker 社区版 (CE) 是开发人员和小型团队开始使用 Docker 并 ...
- hibernate主配置文件中指定session与当前线程绑定
配置一条属性 <property name="hibernate.current_session_context_class">thread</property& ...
- hdu-2717(基础搜索bfs)
题意:给你n和k,问你n最少花费多少代价能得到k: 有两种变换:1.n++或者n--: 2.n=n*2: 两种代价每次的花费都是1: 思路:一维的bfs,每次入队三个点,一个是n+1,一个是n-1,一 ...
- kubernetes 一个服务的基本组成
1. service Service是kubernetes最核心的概念,通过创建Service,可以为一组具有相同功能的容器应用提供一个统一的入口地址,并且将请求进行负载分发到后端的各个容器应用上 k ...
- HDU4349-Xiao Ming's Hope-找规律
打表输出前100之后,找到规律. 不过正确规律是1<<(二进制中1的个数). #include <cstdio> #include <algorithm> usin ...
- Mysql partition by
一,看原表 select * from `user`; 二,查询同组年级最大的 select username ,SUBSTRING_INDEX( GROUP_CONCAT(age order by ...