1123.(重、错)Is It a Complete AVL Tree
题意:给定结点个数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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- Django中Ajax处理
1.大部分和Flask中相同. 2.Django处理JSON,主要是对于查询结果集处理. 使用Django提供的序列化的类来完成QuerySet到JSON字符串的转换 from django.core ...
- [LeetCode&Python] Problem 824. Goat Latin
A sentence S is given, composed of words separated by spaces. Each word consists of lowercase and up ...
- 《网页文档/文字复制方法大全》 - imsoft.cnblogs
<网页文档/文字复制方法大全> 一: 1.首先,找到自己要的文档. 2.文章题目复制,在搜索引擎的框框里输入:site:wenku.baidu.com "题目"/sit ...
- nginx负载均衡、nginx ssl原理及生成密钥对、nginx配制ssl
1.nginx负载均衡 新建一个文件:vim /usr/local/nginx/conf/vhost/load.conf写入: upstream abc_com{ip_hash;server 61.1 ...
- PTA——各位数之和
PTA 7-28 求整数的位数及各位数字之和 我的程序: #include<stdio.h> #include<math.h> int main(){ ,t; scanf(&q ...
- C++学习(三)(C语言部分)之 基本数据类型
基本数据类型 上期回顾 stdlib.h system,命令release MT导入ico文件 基本数据类型 整数 int浮点型(小数 实型) float double字符型 char 变量 常量速度 ...
- 更改MySQL数据库的编码为utf8mb4
原文:http://blog.csdn.net/woslx/article/details/49685111 utf-8编码可能2个字节.3个字节.4个字节的字符,但是MySQL的utf8编码只支持3 ...
- c# 多线程 读写分离
class Program { private static ReaderWriterLockSlim _LockSlim = new ReaderWriterLockSlim(); private ...
- Delphi中Json格式读写
Json是一种轻量级传输数据格式,广泛应用互联网和各应用中.json主要採用键值对来表示数据项.多个数据项之间用逗号分隔,也能够用于数组.以下注重介绍一下在delphi中使用json,在delphi中 ...
- Java单播、广播、多播(组播)---转
一.通信方式分类 在当前的网络通信中有三种通信模式:单播.广播和多播(组播),其中多播出现时间最晚,同时具备单播和广播的优点. 单播:单台主机与单台主机之间的通信 广播:当台主机与网络中的所有主机通信 ...