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的更多相关文章

  1. PAT甲级1123. Is It a Complete AVL Tree

    PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了 ...

  2. PAT甲级1123 Is It a Complete AVL Tree【AVL树】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805351302414336 题意: 给定n个树,依次插入一棵AVL ...

  3. 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 ...

  4. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  5. PAT甲级1131. Subway Map

    PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...

  6. PAT甲级1127. ZigZagging on a Tree

    PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...

  7. PAT甲级1119. Pre- and Post-order Traversals

    PAT甲级1119. Pre- and Post-order Traversals 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二进制树可以通过给定的一对后序和顺序遍历序列来确定,也可以通 ...

  8. PAT甲级1114. Family Property

    PAT甲级1114. Family Property 题意: 这一次,你应该帮我们收集家族财产的数据.鉴于每个人的家庭成员和他/她自己的名字的房地产(房产)信息,我们需要知道每个家庭的规模,以及他们的 ...

  9. PAT甲级1111. Online Map

    PAT甲级1111. Online Map 题意: 输入我们当前的位置和目的地,一个在线地图可以推荐几条路径.现在你的工作是向你的用户推荐两条路径:一条是最短的,另一条是最快的.确保任何请求存在路径. ...

随机推荐

  1. System.Threading.Thread的使用及传递参数等总结

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  2. JetBrains Rider 自定义项目编译路径

    在项目开发过程中有时可能想要自定义 Rider 的编译输出路径,它的具体设置方式如下: 1.选择需要更改配置的项目,右键选择 Properties 菜单.   2.在 Configurations 菜 ...

  3. Java中的变量传递机制以及JS中的参数传递机制

    JAVA: 传递基本类型是 就是基本的值传递 不会影响值本身. package com.wuqi.p1; public class ValuePassTest { public static void ...

  4. java 调用SAP RFC函数错误信息

    RFC接口调用SAP如果有异常会通过com.sap.mw.jco.JCO$Exception: 抛出异常 在开发中遇到的异常有如下 用户名密码可能是错误或者用户无权限,确认用户,必要时联系SAP负责人 ...

  5. HTML基本标签问题总结

    一.常见的内联元素和块元素 块级元素: div, form, (列表相关ul,dl,ol,li,) h1-6,p,table 内联元素: a, br, u,(b , em, strong, i,) i ...

  6. lynis检测

    https://www.cnblogs.com/ssooking/p/6034402.html Usage: lynis command [options] Command: audit audit ...

  7. 2018年12月25&26日

    小结:昨天因为整理课件,调代码耗费了大量时间,所以没来得及整理作业,这两天主要做的题目是关于树链剖分和线段树的,难度大约都是省选难度,毕竟只要涉及到树链剖分难度就肯定不低. 一. 完成的题目: 洛谷P ...

  8. 在startup中遍历程序集

    在aspnetcore中是可以使用AppDomain的,如:在ConfigureServices中,可以使用以下代码获取项目引用的所有dll, var assemblies = AppDomain.C ...

  9. 才知道 Windows Live Writer Source Code plugin for SyntaxHighlighter 更新到2.0了

    这是我用 Windows Live Writer 发布的第一篇文章! 在官方网站看到 Windows Live Writer Source Code plugin for SyntaxHighligh ...

  10. 【ACM】最长公共子序列 - 动态规划

    最长公共子序列 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 咱们就不拐弯抹角了,如题,需要你做的就是写一个程序,得出最长公共子序列.tip:最长公共子序列也称作最 ...