题意:给定结点个数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. 利用SIFT进行特征匹配

    SIFT算法是一种基于尺度空间的算法.利用SIFT提取出的特征点对旋转.尺度变化.亮度变化具有不变性,对视角变化.仿射变换.噪声也有一定的稳定性. SIFT实现特征的匹配主要包括四个步骤: 提取特征点 ...

  2. Mypwd 的解读与实现 20155208

    Mypwd 的解读与实现 20155208 linux下pwd命令的编写 实验要求: 1 .学习pwd命令 2 . 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 3 .实现my ...

  3. MySQL数据库安装和介绍

    一.概述 1.什么是数据库 ? 答:数据的仓库,称其为数据库 2.什么是 MySQL.Oracle.SQLite.Access.MS SQL Server等 ? 答:他们均是一种软件,都有两个主要的功 ...

  4. hdu3605 Escape 二分图多重匹配/最大流

    2012 If this is the end of the world how to do? I do not know how. But now scientists have found tha ...

  5. pycharm汉化 (ubuntu版)

    终端依次输入 cd  /tmp git clone https://github.com/ewen0930/PyCharm-Chinese cd Pycharm-Chinese bash packag ...

  6. python与系统做交互常用的模块和使用方法

    1.使用os模块与系统做简单命令的交互 >>>import os >>>os.popen('pwd') <open file 'pwd', mode 'r' ...

  7. html2canvas用法的总结(转载)

    最近做h5网页,有个功能是用户能长按页面保存为图片,在我们理解就是网页要生成图片然后再让用户长按保存,然后就发现了html2canvas这个框架了,效果挺不错了,但是有几个坑说一下(用的最新版): h ...

  8. 算法设计与分析基础 (Anany Levitin 著)

    第1章 绪论 1.1 什么是算法 1.2 算法问题求解基础 1.2.1 理解问题 1.2.2 了解计算设备的性能 1.2.3 在精确解法和近似解法之间做出选择 1.2.4 算法的设计技术 1.2.5 ...

  9. 持续集成--Jenkins--2

    安装sonar Scanner 打开http://www.sonarqube.org/官网 找到下面扫描器 通过这个扫描器可以分析代码分析 因此你也的安装这个扫描器 上传sonar-scanner-2 ...

  10. 第一个项目:Python pygame——外星人大战(心得)

    2018年12月,作为一个大学专业是物联网工程,毕业后在一家石油行业国企干了近三年,但内心依然有着一颗技术之心的我,通过一次偶然的机会(也许并不偶然),接触到了python.当时抱着玩一玩的心态开始通 ...