PAT甲级1123 Is It a Complete AVL Tree【AVL树】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805351302414336
题意:
给定n个树,依次插入一棵AVL树,按照层序遍历输出,最后判断这棵AVL树是不是完全二叉树。
思路:
这道题过段时间还要再来手搓一发。AVL模板要记住。
判断是不是完全二叉树的话只用看,如果有一个节点儿子是空,而他之后又出现了至少有一个儿子的节点的话,就不是完全二叉树。【蛮巧妙的】
#include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stack>
#include<queue> #define inf 0x7fffffff
using namespace std;
typedef long long LL;
typedef pair<string, string> pr; int n;
const int maxn = ;
vector<int>level[maxn];
typedef struct AvlNode{
int val;
AvlNode *left;
AvlNode *right;
int height;
}*AvlTree, AvlNode; int Max(AvlTree a, AvlTree b)
{
int x = , y = ;
if(a)x = a->height;
if(b)y = b->height;
if(x > y)return x;
else return y;
} AvlTree singleRotateWithRight(AvlTree T)
{
AvlTree L = T->left;
T->left = L->right;
L->right = T;
T->height = Max(T->left, T->right) + ;
L->height = Max(L->left, L->right) + ;
return L;
} AvlTree singleRotateWithLeft(AvlTree T)
{
AvlTree R = T->right;
T->right = R->left;
R->left = T;
T->height = Max(T->left, T->right) + ;
R->height = Max(R->left, R->right) + ;
return R;
} AvlTree doubleRotateWithLeft(AvlTree T)
{
T->left = singleRotateWithLeft(T->left);
return singleRotateWithRight(T);
} AvlTree doubleRotateWithRight(AvlTree T)
{
T->right = singleRotateWithRight(T->right);
return singleRotateWithLeft(T);
} AvlTree Insert(AvlTree T, int val)
{
if(T == NULL){
T = (AvlNode *)malloc(sizeof(struct AvlNode));
if(T){
T->val = val;
T->left = NULL;
T->right = NULL;
T->height = ;
}
}
else if(val < T->val){
T->left = Insert(T->left, val);
int l = , r = ;
if(T->left){
l = T->left->height;
}
if(T->right){
r = T->right->height;
}
if(l - r == ){
if(val < T->left->val){
T = singleRotateWithRight(T);
}
else{
T = doubleRotateWithLeft(T);
}
}
}
else if(val > T->val){
T->right = Insert(T->right, val);
int l = , r = ;
if(T->left)l = T->left->height;
if(T->right)r = T->right->height;
if(r - l == ){
if(val > T->right->val){
T = singleRotateWithLeft(T);
}
else{
T = doubleRotateWithRight(T);
}
}
}
T->height = Max(T->left, T->right) + ;
return T;
} bool after = false, iscomplete = true;
bool first = false;
void levelOrder(AvlTree T)
{
queue<AvlTree>que;
que.push(T);
while(!que.empty()){
AvlTree now = que.front();que.pop();
if(first)printf(" ");
else first = true;
printf("%d", now->val);
level[now->height].push_back(now->val);
if(now->left){
if(after)iscomplete = false;
que.push(now->left);
}
else{
after = ;
}
if(now->right){
if(after)iscomplete = false;
que.push(now->right);
}
else{
after = ;
}
}
} int main()
{
scanf("%d", &n);
AvlTree Tree = NULL;
for(int i = ; i < n; i++){
int x;
scanf("%d", &x);
Tree = Insert(Tree, x);
}
levelOrder(Tree);
printf("\n");
if(iscomplete)printf("YES\n");
else printf("NO\n");
return ;
}
PAT甲级1123 Is It a Complete AVL Tree【AVL树】的更多相关文章
- PAT甲级1123. Is It a Complete AVL Tree
PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了 ...
- 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甲级——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 Advanced 1123 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 ...
- pat甲级1123
1123 Is It a Complete AVL Tree(30 分) An AVL tree is a self-balancing binary search tree. In an AVL t ...
- PAT 1066 Root of AVL Tree[AVL树][难]
1066 Root of AVL Tree (25)(25 分) An AVL tree is a self-balancing binary search tree. In an AVL tree, ...
- 04-树5 Root of AVL Tree + AVL树操作集
平衡二叉树-课程视频 An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the tw ...
- 【PAT 甲级】1151 LCA in a Binary Tree (30 分)
题目描述 The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has bo ...
- PAT 甲级 1043 Is It a Binary Search Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805440976633856 A Binary Search Tree ( ...
随机推荐
- linux 只查看目录下文件夹
只显示目录文件夹 ls -F |grep "/$" 显示 目录权限 ls -al |grep "^d" 只显示文件 ls -al |grep "^-& ...
- luogu P5288 [HNOI2019]多边形
传送门 这是什么神仙操作... 首先要注意一些性质.首先每一个\((x,n)\)的边可以把当前多边形分成两半,这两半的操作是独立的.然后对于某一个没有\((x,n)\)的边的多边形,最优操作是唯一的. ...
- android:shape 设置圆形
组件高度和宽度设置为相同的值即可<?xml version="1.0" encoding="utf-8"?><shape xmlns:andr ...
- Mac环境下的mongodb的安装
1.安装MongoDB brew install mongodb 这个是默认安装最新版本的 mogodb,如果想安装指定版本可以先查看 mongodb 版本 brew search mongodb m ...
- c++基础学习
1.输入输出函数(cout,cin) #include<iostream> int main() { using namespace std; cout<<"Come ...
- mysql join on and
2018-6-4 10:28:50 星期一 开发中一直在用 left join, 心中只有一丝丝的了解, 还都是学校里学的, 今天看了几遍文章这里记录一下 sql的left join .right j ...
- Everything工具使用
一.简介 Everything : Windows下的文件名搜索引擎 二.Everything工具下载 官方最新版本下载 Everything下载 三.Everything快捷搜索 Java*.doc ...
- laravel whereDoesntHave
select * from `feeds` where not exists (select * from `black_lists` where `feeds`.`user_id` = `black ...
- Tomcat 下4个配置文件详解
Tomcat 的配置文件由4个 xml 文件构成,context.xml.web.xml.server.xml.tomcat-users.xml 这4个文件.每个文件都有自己的功能与配置方法,下列将逐 ...
- c++ ignore用法
转自 http://blog.sina.com.cn/s/blog_4b3336c50102v45n.html std::cin.ignore() can be called three diffe ...