题意:给定结点个数n和插入序列,判断构造的AVL树是否是完全二叉树?

思路:AVL树的建立很简单。而如何判断是不是完全二叉树呢?通过层序遍历进行判断:当一个结点的孩子结点为空时,则此后就不能有新的结点入队。若没有,则是完全二叉树,否则不是。

代码:

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
using namespace std; vector<int> layer; struct Node {
int v, height;
Node *lchild, *rchild;
}; Node* newNode(int v) {
Node* pNode = new Node;
pNode->v = v;
pNode->height = ;
pNode->lchild = pNode->rchild = NULL;
return pNode;
} 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* temp = root->rchild;
root->rchild = temp->lchild;
temp->lchild = root;
updateHeight(root);
updateHeight(temp);
root = temp;
}
void R(Node* &root) {
Node* temp = root->lchild;
root->lchild = temp->rchild;
temp->rchild = root;
updateHeight(root);
updateHeight(temp);
root = temp;
} void insert(Node* &root, int v) {
if (root == NULL) {
root = newNode(v);
return;
} if (v < root->v) {
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);
}
}
}
}
bool isComplete =true;
int after=;
void layerOrder(Node* root){
queue<Node*> Q;
Q.push(root);
while(!Q.empty()){
Node* front=Q.front();
Q.pop();
layer.push_back(front->v); if(front->lchild!=NULL){
if(after==) isComplete=false;
Q.push(front->lchild);
}else{
after=;
} if(front->rchild!=NULL){
if(after==) isComplete=false;
Q.push(front->rchild);
}else{
after=;
}
} } //vector<int> insertOrder; int main()
{
int n,data;
scanf("%d",&n);
Node* root=NULL;
for(int i=;i<n;i++){
scanf("%d",&data);
insert(root,data);
}
layerOrder(root); for(int i=;i<layer.size()-;i++){
printf("%d ",layer[i]);
}
printf("%d\n",layer[n-]);
printf("%s\n",isComplete==true?"YES":"NO"); return ;
}

1123.(重、错)Is It a Complete AVL Tree的更多相关文章

  1. PAT甲级1123. Is It a Complete AVL Tree

    PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 sub ...

  10. 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 ...

随机推荐

  1. [LeetCode&Python] Problem 292. Nim Game

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...

  2. 学号 20155219 《Java程序设计》第1周学习总结

    学号 20155219 <Java程序设计>第1周学习总结 教材学习内容总结 JVM:是JAVA程序唯一认识的操作系统,其可执行文件为.class文档:具有让Java程序跨平台的功能.负责 ...

  3. FZU软工第六次作业-团队选题报告

    作业链接 队长博客:陈晓彬 团队选题报告 选题报告PPT 原型展示 前言 经过团队的讨论,我们对自己的选题进行了项目立意的进一步确定,后面有项目来源.同时,我们将自己的APP的名字改成了"一 ...

  4. ISCC的 Web——WP

    比赛已经结束了,自己做出来的题也不是很多,跟大家分享一下 第一题:比较数字大小 打开连接 在里面随意输入一个值,他会提示数字太小了 那么我们输入他允许的最大值试试 他还是提示太小了 我们知道做web‘ ...

  5. Eclipse和Intel idea的常用技巧

    使用Eclipse的几个必须掌握的快捷方式   “工若善其事,必先利其器”,感谢Eclipse,她 使我们阅读一个大工程的代码更加容易,在阅读的过程中,我发现掌握几个Eclipse的快捷键会使阅读体验 ...

  6. 使用nexus 管理pip 私有包

    nexus 已经支持了对于python pip 包的管理(支持group,host,proxy) 这个是一个简单的使用docker 运行的demo,同时集成了s3 存储,以及 一个为了测试简单的自定义 ...

  7. How to get checksum by IAR

  8. JS中encodeURIComponent函数用php解码的代码

    JS中encodeURIComponent函数给中文编码后,如何用php解码?? 前提:编码前的中文可能是gbk,gb2312,utf-8等. 复制代码 代码如下: urldecode() iconv ...

  9. oracle-sql系统学习-ddl-dml

    e41084-04 oracle database sql language reference 11g release 2 sql语句类型 ddl alter ...除了alter session和 ...

  10. 记录:.user.ini 使用

    记录:.user.ini 使用 可以用于防跨站配置. .user.ini 注意安全问题 动态加载,默认 5 分钟自动刷新. php 5.3 以后的版本支持. 修改完成后再将文件锁定. 相关链接: 神秘 ...