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 题意: 输入我们当前的位置和目的地,一个在线地图可以推荐几条路径.现在你的工作是向你的用户推荐两条路径:一条是最短的,另一条是最快的.确保任何请求存在路径. ...
随机推荐
- C# 、.NET、ASP.NET MVC积累
2016-10-27 给视图中的select赋值: 控制器: public ActionResult Add() { List<SelectListItem> ClassName = ne ...
- 第一章 –– Java基础语法
第一章 –– Java基础语法 span::selection, .CodeMirror-line > span > span::selection { background: #d7d4 ...
- SCUT - 223 - Maya - 构造
https://scut.online/p/223 给定两个数N,M,构造M个在[0,80000]以内的互不相同的数使之异或和为N. 首先特判一下M<=2的两个简单情况,还有坑爹的-1! 然后想 ...
- 51nod1428(优先队列)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1428 题意:中文题诶- 思路:贪心 问最少要多少教室就是求最多 ...
- [转]SE43 修改SAP标准菜单、登陆界面、背景图片
1.事务码se43 复制标准菜单S000 到 ZS000 2.按实际需要修改 ZS000 3.在事务码SSM2中用ZS000 代替 S000 4.注销后重新登陆 o 修改SAP登陆界面(在本博客 ...
- MySQL的高可用实现:MySQL系列之十四
MySQL的高可以有三种实现方式:多主模式(Multi Master MySQL),MHA(Master High Availability)和 Galera Cluster:wresp 一.MHA ...
- iOS无线真机调试
打开xcode,选择Window > Devices and Simulators 用数据线连接设备 选择 Connect via network
- 注意ie6的盒模型
浏览器版本多了,也是一个累,特别是ie家族的. 网上搜罗了一大堆,发现说的和我看到的不一样啊,结果才发现原来是对方表述有问题,省略了几个字就产生了歧义了. 按照网上说的ie6对盒模型解释不符合W3C标 ...
- jquery——属性操作、特殊效果
1. attr().prop() 取出或者设置某个属性的值 <!DOCTYPE html> <html lang="en"> <head> &l ...
- Tree and Queries CodeForces - 375D 树上莫队
http://codeforces.com/problemset/problem/375/D 树莫队就是把树用dfs序变成线性的数组. (原数组要根据dfs的顺序来变化) 然后和莫队一样的区间询问. ...



