pat甲级1123
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 subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Figures 1-4 illustrate the rotation rules.
![]() |
![]() |
|---|---|
![]() |
![]() |
Now given a sequence of insertions, you are supposed to output the level-order traversal sequence of the resulting AVL tree, and to tell if it is a complete binary tree.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤ 20). Then N distinct integer keys are given in the next line. All the numbers in a line are separated by a space.
Output Specification:
For each test case, insert the keys one by one into an initially empty AVL tree. Then first print in a line the level-order traversal sequence of the resulting AVL tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line. Then in the next line, print YES if the tree is complete, or NO if not.
Sample Input 1:
5
88 70 61 63 65
Sample Output 1:
70 63 88 61 65
YES
Sample Input 2:
8
88 70 61 96 120 90 65 68
Sample Output 2:
88 65 96 61 70 90 120 68
NO
判断是否完全二叉树:
层序遍历时,若前面已经出现过空子树,而后面又发现了非空子树,则不是完全二叉树。
平衡二叉树的旋转:
1、右单旋

2、左单旋

3、左右双旋

4、右左双旋

#include <iostream>
#include <vector>
#include <queue>
using namespace std; typedef struct Node *Tree;
struct Node
{
int data;
Tree left;
Tree right;
}; Tree insert(Tree T, int data);
int getHeight(Tree T);
Tree LL(Tree T);
Tree LR(Tree T);
Tree RR(Tree T);
Tree RL(Tree T);
vector<int> levelOrder(Tree T, bool& isComplete); int main()
{
int N, i, t;
cin >> N;
Tree root = NULL;
for (i = ; i < N; i++)
{
cin >> t;
root = insert(root, t);
}
bool isComplete;
vector<int> v = levelOrder(root, isComplete);
cout << v[];
for (i = ; i < v.size(); i++)
cout << " " << v[i];
printf("\n%s", isComplete ? "YES" : "NO");
return ;
} Tree insert(Tree T, int data)
{
if (T == NULL)
{
T = new Node;
T->data = data;
T->left = T->right = NULL;
}
else if (data < T->data)
{
T->left = insert(T->left, data);
if (getHeight(T->left) - getHeight(T->right) == )
{
if (data < T->left->data)
T = LL(T);
else
T = LR(T);
}
}
else
{
T->right = insert(T->right, data);
if (getHeight(T->right) - getHeight(T->left) == )
{
if (data > T->right->data)
T = RR(T);
else
T = RL(T);
}
}
return T;
} int getHeight(Tree T)
{
if (T == NULL) return ;
int a = getHeight(T->left);
int b = getHeight(T->right);
return (a > b) ? (a + ) : (b + );
} Tree LL(Tree T)
{
Tree tmp = T->left;
T->left = tmp->right;
tmp->right = T;
return tmp;
} Tree LR(Tree T)
{
T->left = RR(T->left);
return LL(T);
} Tree RR(Tree T)
{
Tree tmp = T->right;
T->right = tmp->left;
tmp->left = T;
return tmp;
} Tree RL(Tree T)
{
T->right = LL(T->right);
return RR(T);
} vector<int> levelOrder(Tree T, bool& isComplete)
{
vector<int> v;
isComplete = true;
queue<Tree> q;
q.push(T);
bool hasEmpty = false;
while (!q.empty())
{
Tree T = q.front();
q.pop();
v.push_back(T->data);
if (T->left != NULL)
{
q.push(T->left);
//已经出现过空子树但是现在子树又不空,那么不是完全二叉树
if (hasEmpty) isComplete = false;
}
else
hasEmpty = true;
if (T->right != NULL)
{
q.push(T->right);
if (hasEmpty) isComplete = false;
}
else
hasEmpty = true;
}
return v;
}
pat甲级1123的更多相关文章
- PAT甲级1123. Is It a Complete AVL Tree
PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了 ...
- PAT甲级1123 Is It a Complete AVL Tree【AVL树】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805351302414336 题意: 给定n个树,依次插入一棵AVL ...
- 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甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT甲级1131. Subway Map
PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...
- PAT甲级1127. ZigZagging on a Tree
PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...
- PAT甲级1119. Pre- and Post-order Traversals
PAT甲级1119. Pre- and Post-order Traversals 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二进制树可以通过给定的一对后序和顺序遍历序列来确定,也可以通 ...
- PAT甲级1114. Family Property
PAT甲级1114. Family Property 题意: 这一次,你应该帮我们收集家族财产的数据.鉴于每个人的家庭成员和他/她自己的名字的房地产(房产)信息,我们需要知道每个家庭的规模,以及他们的 ...
- PAT甲级1111. Online Map
PAT甲级1111. Online Map 题意: 输入我们当前的位置和目的地,一个在线地图可以推荐几条路径.现在你的工作是向你的用户推荐两条路径:一条是最短的,另一条是最快的.确保任何请求存在路径. ...
随机推荐
- sqlserver2012——游标
游标:一种数据访问机制,允许用户访问单独的数据行而不是对整个行集进行操作.用户可以通过单独处理每一行逐条收集信息并对数据逐行进行操作,这样可以将降低系统开销. 游标主要有以下两部分: 游标结果集:由定 ...
- 关于nginx的使用感想
感慨: 最近老板说要转型NodeJs,所以接连用ThinkJS框架写了2个项目(不过通篇弱类型不用考虑class是真的爽啊...).那天老板又正好让我看看Ngxin的负载均衡和静态资源处理,那现在就写 ...
- OpenStack基础知识-tox的详解介绍
1.tox简介 tox是通用的虚拟环境管理和测试命令行工具.tox能够让我们在同一个Host上自定义出多套相互独立且隔离的python环境,每套虚拟环境中可能使用了不同的 Python 拦截器/环境变 ...
- Zjoi2011 看电影
最近在学习一些概率的东西.. 一个随机试验称为 Laplace 试验,当且仅当它满足如下两个条件: (ⅰ) 试验结果 (样本点) 的个数是有限的.(Ω 是有限集) (ⅱ) 任意两个基本事件的概率均相等 ...
- docker 推送镜像到私有地址
下面针对的都是docker官网的地址 先登录 docker login 输入docker ID ID不是你的注册邮箱,指的是你登录后显示的ID,然后输入密码 ....此时认为你已经登陆成功了 接着看下 ...
- Android OpenGLES2.0(十七)——球形天空盒VR效果实现
在3D游戏中通常都会用到天空盒,在3D引擎中也一般会存在天空盒组件,让开发者可以直接使用.那么天空盒是什么?天空盒又是如何实现的呢?本篇博客主要介绍如何在Android中利用OpenGLES绘制一个天 ...
- String类、static、Arrays类、Math类
String类.static.Arrays类.Math类 String类.static.Arrays类.Math类 String类.static.Arrays类.Math类 String类.stati ...
- thinkphp实现登录后返回原界面
主要思路还是用session记录原地址,在登录后再跳转回原界面 先保存请求login方法界面的url public function savelogin(){ session('returnUrl', ...
- myeclipse svn 在线安装
https://www.cnblogs.com/sxdcgaq8080/p/6000446.html http://subclipse.tigris.org/update_1.8.x
- 读取文件名称cmd命令
操作步骤: 1.进入命令提示符窗口 开始→运行,键入“CMD”,确定. 开始→程序→附件→C:\命令提示符 2.进入驱动器d: C:\Documents and Settings>d:(回车) ...



