题意:给定结点个数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 21. Merge Two Sorted Lists

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  2. com.sun.org.apache.xerces.internal.impl.dv.util.Base64出现的问题

    import com.sun.org.apache.xerces.internal.impl.dv.util.Base64; 出现的问题是这个在eclipse中无法使用,解决方法如下: (1)进入ec ...

  3. ACM 删数问题 SDUT 2072

    http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Index/problemdetail/pid/2072.html 删数问题 Time Limit ...

  4. CodeForces 727C

    zsy: Guess the Array Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submi ...

  5. SQL-表-003

    注:红色代表关键字,绿色代表解释说明,蓝色代表重点: 什么是数据表? 数据表是数据库中最重要的组成部分,可以将数据表分解成字段(列)和记录(行): 数据表的增加:约束同时创建 create table ...

  6. webpack中hash、chunkhash、contenthash区别

    webpack中对于输出文件名可以有三种hash值: 1. hash 2. chunkhash 3. contenthash 这三者有什么区别呢? hash 如果都使用hash的话,因为这是工程级别的 ...

  7. 异构环境oracle数据库迁移dmp文件之exp和imp以及expdp和impdp

    exp/imp可在以下情况下使用 两个数据库之间传送数据 1.同一个oracle数据库的版本之间 2.不同oracle数据库的版本之间 3.相同或不相同的操作系统之间的oracle数据库 用于数据库的 ...

  8. HotSpot VM GC 的种类

    collector种类 GC在 HotSpot VM 5.0里有四种: incremental (sometimes called train) low pause collector已被废弃,不在介 ...

  9. LoadRunner 压测场景制定以及报告分析

    这里,我们利用 LoadRunner 来制定场景,且以测试 tps 值为导向,主要介绍手工场景 单服务器的业务请求处理能力 tps 值在 10~200 是合理的:如果是访问单接口不走关系型数据库的,访 ...

  10. FastAdmin 数据库备份插件更新到 v1.0.4

    FastAdmin 数据库备份插件更新到 v1.0.4 下载地址: https://www.fastadmin.net/store/database.html 更新如下: 修复了忽略列表无效的 Bug ...