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. 使用Log4j2,打包后提示ERROR StatusLogger Log4j2 could not find a logging implementation.

    从Log4j切换到Log4j2,没有打包之前日志输出正常,但是打包后总是提示下面内容: 错误一: ERROR StatusLogger Log4j2 could not find a logging ...

  2. C++基础之继承类和派生类

    (1)继承是创建一个具有某个类的属性和行为的新类的能力.原有的类称为基类,新创建的类称为派生类.派生类将基类中的所有成员作为自己的成员,同时派生类本身可以定义新的成员(2)派生类只有一个基类的继承称单 ...

  3. SAS笔记(6) PROC MEANS和PROC FREQ

    PROC MEANS和PRC FREQ在做描述性分析的时候很常用,用法也比较简单,不过这两个过程步的某些选项容易忘记,本文就梳理一下. 在进入正文前,我们先创建所需的数据集TEST_SCORES: D ...

  4. SAS笔记(2) RETAIN语句

    本文重点: 使用RETIAN,INPUT在每次循环执行时保留上一次PDV中的变量值. SUM语句和SET语句会自动RETAIN变量. 1. RETAIN语句 1.1 Example 1 先来看看在DA ...

  5. django的models字段介绍

    #增加数据库数据方法#方法1CalcData.objects.create(var1 ='21',var2='22')#方法2obj =CalcData(var1 ='21',var2='22')jo ...

  6. Spring AOP 自定义注解实现统一日志管理

    一.AOP的基本概念: AOP,面向切面编程,常用于日志,事务,权限等业务处理.AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容(Spring核心之一),是函数式编程 ...

  7. [WebShow系列] Web浏览器最大化满屏及比例缩放方法

    如果要在大屏上展示,大屏所带电脑的浏览器应该处于满屏,此时就不会显示浏览器软件的边框了.个别浏览器在满屏状态下,某些边栏等还继续保留,此时应设置此浏览器的显示选项方可消除. 如果屏幕中的显示对象过小或 ...

  8. JS事件之鼠标悬浮窗(鼠标悬浮窗抖动问题的解决)

    鼠标进入显示悬浮窗,思路有简单有困难. 首先要注意的是我们要给悬浮窗设置position为absolute,不然我们改了 style.left style.top发现没有变化很尴尬.其余的内容看起来就 ...

  9. saltstack执行结果存储到MySQL

    saltstack执行结果保存到MySQL中,以便进行命令安全审计必须是python2.7以上的环境安装相关模块ubuntu系统安装 apt-get install -y python-mysqldb ...

  10. libaudit_plugin.so安装

    #上传audit到mysql的plugin目录vim /etc/my.cnfplugin-load=AUDIT=libaudit_plugin.soaudit_json_file=1audit_jso ...