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 ...
随机推荐
- Gym - 101806T: Touch The Sky(贪心)
Figure: The house floats up in the sky by balloons. This picture is also used in 2018 KAIST RUN Spri ...
- DES算法,JAVA,遇到的问题
(1)使用Based64编码时出现的问题. java.lang.IllegalArgumentException 这中情况出现在解密时,主要原因是based64加密时用了sun的内部包sun.misc ...
- Spring通知方法错误
错误提示,主要最后一句话 ,花了2个小时 org.springframework.beans.factory.BeanCreationException: Error creating bean ...
- mysql深入
使用存储过程 create procedure productpricing() begin select avg(prod_price) as priceaverage from products; ...
- java保留2位小数及BigDecimal使用
java保留两位小数的方法 import java.math.BigDecimal; import java.text.DecimalFormat; import java.text.NumberFo ...
- ESB雏形 -- 项目企业服务总线初始
今天要厚着脸皮给大家推荐一个自己做的通信中间件——ServiceAnt,目前已经在我们团队的两个产品线上投入了使用. ServiceAnt是什么 它最初的定位是ESB(企业服务总线),但目前还没有达到 ...
- SelectDataTable
项目地址 : https://github.com/kelin-xycs/SelectDataTable SelectDataTable 一个 用 C# 实现的 用 Sql select DataT ...
- enum和数据库entity互转
注意,code和desc都是string的,数据库的entity是integer,dto的是enum,所以需要一个转换 entity转dto EnumGender.getEnum(String.val ...
- amqp 和 exchange 详细解释
amqp 的 excange 字面意思是一个交换机.他的任务是吧 消息 分配给消息队列. amqp 的 exchange 有三种,分别是 Direct , fanout 和 toppic.三种. ...
- 查看JVM运行时参数
1.查看JVM运行时参数 -XX:+PrintFlagsInitial -XX:PrintFlagsFinal -XX:+UnlockExperimentalVMOptions 解锁实验参数 -XX: ...