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 ...
随机推荐
- Delphi之TStrings和TStringLists类
Delphi之TStrings和TStringLists类 有些类不是组件,但它们支持存在的组件.这些类是其他组件的典型属性,直接由TPersistent派生,如TStrings.TCanvas和TC ...
- 使用JAVA获取JSON报文
基本JSON格式: { "name": "liming", "age": "13", "array" ...
- 使用fastjson将对象和字符串进行转换
依赖包: <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</ar ...
- Js 常用字符串操作 API
常用的一些字符串操作 API 整理 1.str.charAt(index).str.charCodeAt(index) - 返回指定位置的字符 / 字符编码(0~65535) index - 必须,表 ...
- SQL Server 数据库try catch 存储过程
SQL Server 在生产环境中这样写存储过程的坑都避免了吗? 原文链接: http://www.cnblogs.com/chenmh/p/7856777.html 概述 最近因为业务的需求写了一段 ...
- javascript帧动画
前面的话 帧动画就是在“连续的关键帧”中分解动画动作,也就是在时间轴的每帧上逐帧绘制不同的内容,使其连续播放而成的动画.由于是一帧一帧的画,所以帧动画具有非常大的灵活性,几乎可以表现任何想表现的内容. ...
- git 解决二进制文件冲突
1.冲突的产生 当我们向远程git服务器提交某一个文件的修改时,恰巧这个文件相同的修改地方其他人也有修改,并且已经提交到服务器,这时冲突就产生了. 通常,当我们合并两个相同的地方都有修改的分支时,都会 ...
- float数组转字符串实施方案小记
float[] floats = {1.2f , 3.5f , 6.4f}; Double[] doubles = IntStream.range(0, floats.length).mapToDou ...
- UESTC1013-我的魔法栈-模拟/排列组合
有一个串,有黑色和白色两种元素.一次操作可以把最上面的白色元素变成黑色,同时把这个元素上面的所有元素变成白色. 给你一个30以内的串,计算变成全黑时,元素变化的总和. 我用的方法比较笨,打表处理了1- ...
- 洛谷P1983车站分级题解
题目 这个题非常毒瘤,只要还是体现在其思维难度上,因为要停留的车站的等级一定要大于不停留的车站的等级,因此我们可以从不停留的车站向停留的车站进行连边,然后从入度为0的点即不停留的点全都入队,然后拓扑排 ...