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 YESif 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

分析:这道题考察AVL树和层序遍历以及完全二叉树
判断是不是完全⼆叉树,就看在出现了⼀个孩⼦为空的结点之后是否还会出现孩⼦结点不为空的结
点,如果出现了就不是完全⼆叉树。
AVL树⼀共有四种情况,这⾥我把发现树不平衡的那个结点叫做A结点,A发现树不平衡的情况有四
种:
新来的结点插⼊到A的左⼦树的左⼦树
新来的结点插⼊到A的左⼦树的右⼦树
新来的结点插⼊到A的右⼦树的左⼦树
新来的结点插⼊到A的右⼦树的右⼦树
发现不平衡时就需要处理,第1种情况只要简单的右旋,第4种情况只需左旋⼀下,
第2种情况需要先对A的左⼦树左旋⼀下,然后对A右旋,同理第3种情况需要对A的右⼦树右旋⼀下,然后对A左旋

 #include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
struct Node
{
int v;
Node *l, *r;
Node(int a = -) :v(a), l(nullptr), r(nullptr) {}
};
int n, a;
vector<int>res;
int getHeight(Node* root)
{
if (root == nullptr)
return ;
return max(getHeight(root->l), getHeight(root->r))+;
}
Node* rotateRight(Node* root)//右旋
{
Node*p = root->l;
root->l = p->r;
p->r = root;
return p;//新的根节点
}
Node* rotateLeft(Node* root)//左旋
{
Node*p = root->r;
root->r = p->l;
p->l = root;
return p;//新的根节点
}
Node* rotateLeftRight(Node* root)//左右旋
{
root->l = rotateLeft(root->l);//先左旋
return rotateRight(root);//再右旋
}
Node* rotateRightLeft(Node* root)//右左旋
{
root->r = rotateRight(root->r);//先右旋
return rotateLeft(root);//再左旋
}
Node* Insert(Node* root, int x)
{
if (root == nullptr)
{
root = new Node(x);
return root;
}
if (x < root->v)
{
root->l = Insert(root->l, x);
if (getHeight(root->l) - getHeight(root->r) >= )
root = x < root->l->v ? rotateRight(root) : rotateLeftRight(root);
}
else
{
root->r = Insert(root->r, x);
if (getHeight(root->r) - getHeight(root->l) >= )
root = x > root->r->v ? rotateLeft(root) : rotateRightLeft(root);
}
return root;
}
bool LevelOrder(Node* root)
{
bool flag = true;//是不是完全二叉树
if (root == nullptr)
return flag;
queue<Node*>q, temp;
q.push(root);
while (!q.empty())
{
Node*p = q.front();
q.pop();
temp.push(p);
res.push_back(p->v);
if (p->l != nullptr)
q.push(p->l);
else if (temp.size() + q.size() != n)//中间出现空节点,不是完全二叉树
flag = false;
if (p->r != nullptr)
q.push(p->r);
else if (temp.size() + q.size() != n)//中间出现空节点,不是完全二叉树
flag = false;
}
return flag;
}
int main()
{
cin >> n;
Node* root = nullptr;
for (int i = ; i < n; ++i)
{
cin >> a;
root = Insert(root, a);
}
bool flag = LevelOrder(root);
for (int i = ; i < res.size(); ++i)
cout << (i > ? " " : "") << res[i];
if (flag)
cout << endl << "YES" << endl;
else
cout << endl << "NO" << endl;
return ;
}

PAT甲级——A1123 Is It a Complete AVL Tree【30】的更多相关文章

  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树的判断)

    嫌排版乱的话可以移步我的CSDN:https://blog.csdn.net/weixin_44385565/article/details/89390802 An AVL tree is a sel ...

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

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

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

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

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

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

  8. PAT甲级题解-1123. Is It a Complete AVL Tree (30)-AVL树+满二叉树

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

  9. PAT 1123. Is It a Complete AVL Tree (30)

    AVL树的插入,旋转. #include<map> #include<set> #include<ctime> #include<cmath> #inc ...

随机推荐

  1. element-UI 点击一行,背景色变化

    代码: @row-click="rowClick" 当某一行被点击时会触发该事件 :row-class-name="tableRowClassName"  可以 ...

  2. 【代码工具】Lombok来优雅的编码

    前言 Lombok 是一种 Java™ 实用工具,可用来帮助开发人员消除 Java 的冗长,尤其是对于简单的 Java 对象(POJO).它通过注解实现这一目的. 正文 添加依赖 在 pom.xml ...

  3. Tomcat 调优的技巧

    转载:www.cnblogs.com/wangsen, https://mp.weixin.qq.com/s/WrIsOOyR7o4SwSXMT0Zecg 最近,看到一篇讲述 Tomcat 调优的文章 ...

  4. PHP setrawcookie() 函数

    定义和用法 setrawcookie() 函数不对 cookie 值进行 URL 编码,发送一个 HTTP cookie. cookie 是由服务器发送到浏览器的变量.cookie 通常是服务器嵌入到 ...

  5. bzoj 2257 (JSOI 2009) 瓶子与燃料

    Description jyy就一直想着尽快回地球,可惜他飞船的燃料不够了. 有一天他又去向火星人要燃料,这次火星人答应了,要jyy用飞船上的瓶子来换.jyy 的飞船上共有 N个瓶子(1<=N& ...

  6. NX二次开发-比较两个string是否相等

    NX11+VS2013 #include <uf.h> #include <uf_ui.h> UF_initialize(); string A = "ABC&quo ...

  7. python从入门到大神---1、初始化实例、类属性、方法

    python从入门到大神---1.初始化实例.类属性.方法 一.总结 一句话总结: 方法不加括号是代码段:感觉python方法和js,php很类似,不加括号是代码段,加括号变成方法,比如f,f() 1 ...

  8. Codeforces 166B - Polygon (判断凸包位置关系)

    Codeforces Round #113 (Div. 2) 题目链接:Polygons You've got another geometrical task. You are given two ...

  9. markdown开篇

    def show(): print("你好世界!") print("实习的日子还是得好好学习呀!") print("加油 各位!")

  10. 搭建jeecg-boot项目运行

    实验版本: 2.0.2(发布日期:20190708) 项目地址:https://github.com/zhangdaiscott/jeecg-boot 说明文档:http://jeecg-boot.m ...