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 ...
随机推荐
- s21day05 python笔记
s21day05 python笔记 一.昨日内容回顾及补充 回顾 补充 列表独有功能 extend:循环添加到一个列表中 1.users = ['张三',66],people = ['王五',99] ...
- HDU 2036 叉乘求三角形面积
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s) ...
- 回收机制GC
.NET 之 垃圾回收机制GC 一.GC的必要性 1.应用程序对资源操作,通常简单分为以下几个步骤:为对应的资源分配内存 → 初始化内存 → 使用资源 → 清理资源 → 释放内存. 2.应用程序对资源 ...
- 02 JDBC相关
====================================================================================JDBC JAVA Databa ...
- 20165313Java实验四 Android程序设计
实验报告封面 课程:Java程序设计 班级:1653班 姓名:张晨晖 学号:20165313 指导教师:娄嘉鹏 实验日期:2018年5月14日 实验时间:13:45 - 15:25 实验序号:实验四 ...
- 滚动加载图片(懒加载)实现原理(这是旧实现,仅做为获取元素宽高api的参考)
https://www.cnblogs.com/flyromance/p/5042187.html 本文主要通过以下几方面来说明懒加载技术的原理,个人前端小菜,有错误请多多指出 一.什么是图片滚动加载 ...
- csvn使用入门
在前面我们已经配置好了csvn服务器,直达链接http://blog.csdn.net/qq_34829953/article/details/78285647 现在我们在win10环境下使用我们搭建 ...
- Singer 学习八 运行&&开发taps、targets (三 开发tap)
如何没有找到适合的tap,那么我们可以自己开发一个 hello world tap 仅仅是一个程序,我们可以使用任何语言进行编写,根据singer 指南,输出数据到stdout 即可,实际上一个简单的 ...
- UWA 转载
性能优化,进无止境-内存篇 https://blog.uwa4d.com/archives/optimzation_memory_1.html https://blog.uwa4d.com/archi ...
- 应用端连接MySQL数据库报Communications link failure
事情的起因: 某项目的开发同学突然Q我们组的某同学,要求我们调整MySQL的连接等待超时参数wait_timeout.要求我们从28800s调整到31536000s(也就是一年) 应用端测试环境的to ...