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. leetcood学习笔记-88-合并两个有序数组

    题目描述: 第一次提交: class Solution: def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -&g ...

  2. Servlet - Tomcat服务器相关

    1. 服务器 : 服务器其实就是代码编写的一个程序, 可以根据用户发送的请求, 调用执行对应的逻辑代码 2. Tomcat目录结构说明 : \bin : 存放启动和关闭Tomcat的可执行文件 \co ...

  3. bzoj1013题解

    [解题思路] 初看以为是二次方程组,但这些方程有相同的右值r2,于是可以化为一次方程组,高斯消元即可.复杂度O(n3). 化简过程: 假设第i个方程和第j个方程联立,得:  ∑(a[i,k]-a[0, ...

  4. delphi 备注一些函数

    Delphi的StringReplace 字符串替换函数 function StringReplace (const S, OldPattern, NewPattern: string; Flags: ...

  5. APIO 2017 商旅 洛谷3778

    Description 在广阔的澳大利亚内陆地区长途跋涉后,你孤身一人带着一个背包来到了科巴.你被这个城市发达而美丽的市场所 深深吸引,决定定居于此,做一个商人.科巴有个集市,集市用从1到N的整数编号 ...

  6. NX二次开发-UFUN批量操作图层状态UF_LAYER_set_many_layers_status

    NX11+VS2013 #include <uf.h> #include <uf_ui.h> #include <uf_layer.h> UF_initialize ...

  7. js的线程和同步异步以及console.log机制

    项目上线了,闲下来就写写东西吧.积累了好多东西都没有做笔记~挑几个印象深刻的记录一下吧. js的同步异步以及单线程问题: 都知道单线程是js的一大特性.但是通常io(ajax获取服务器数据).用户/浏 ...

  8. (转)ab(apachebench)测试与loadrunner

    转:http://blog.csdn.net/gzh0222/article/details/7172341 ab的全称是ApacheBench,是 Apache 附带的一个小工具,专门用于 HTTP ...

  9. 测试VPS

    wget freevps.us/downloads/bench.sh -O - -o /dev/null|bash

  10. oracle 管理表空间

    表空间:是oracle数据库中最大的逻辑存储结构,与操作系统中的数据文件相对应,用于存储数据库中用户创建的所有内容 表空间>数据文件 4.1创建一个永久性表空间myspace create ta ...