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. 5432. 【NOIP2017提高A组集训10.28】三元组

    题目 题目大意 给你\(X+Y+Z\)个三元组\((x_i,y_i,z_i)\). 然后选\(X\)个\(x_i\),选\(Y\)个\(y_i\),选\(Z\)个\(z_i\). 每个三元组只能选择其 ...

  2. luoguP1273 有线电视网 [树形dp]

    题目描述 某收费有线电视网计划转播一场重要的足球比赛.他们的转播网和用户终端构成一棵树状结构,这棵树的根结点位于足球比赛的现场,树叶为各个用户终端,其他中转站为该树的内部节点. 从转播站到转播站以及从 ...

  3. poj1637Sightseeing tour

    传送门 网络流解混合图欧拉回路,以前xy讲过,但是我一直没写. 把无向边随意定向,每个点权值为出度减入度,权值为奇数无解,权值大于0的从s向其连权值/2的边,小于0的向t连-权值/2的边,原图中无向图 ...

  4. ES6 箭头函数this指向

    箭头函数有几个使用注意点. (1)函数体内的this对象,就是定义时所在的对象,而不是使用时所在的对象. (2)不可以当作构造函数,也就是说,不可以使用new命令,否则会抛出一个错误. (3)不可以使 ...

  5. js Date.parse() format.

    date format android chrome linux chrome Mobile safari ios chrome windows safari linux firefox window ...

  6. P1831 杠杆数

    P1831 杠杆数 题目描述 如果把一个数的某一位当成支点,且左边的数字到这个点的力矩和等于右边的数字到这个点的力矩和,那么这个数就可以被叫成杠杆数. 比如4139就是杠杆数,把3当成支点,我们有这样 ...

  7. LED 发光二极管压降

    常用发光二极管的压降 1. 直插超亮发光二极管压降 主要有三种颜色,然而三种发光二极管的压降都不相同,具体压降参考值如下: 红色发光二极管的压降为2.0--2.2V  黄色发光二极管的压降为1.8—2 ...

  8. Spring AOP之注解实现

    在自定义个注解之后,通过这个注解,标注需要切入的方法,同时把需要的参数传到切面去.那么我们怎么在切面使用这个注解.我们使用这个自定义注解一方面是为了传一些参数,另一方面也是为了省事.具体怎么省事,看我 ...

  9. org.apache.jasper.JasperException: Unable to compile class for JSP: Invalid character constant

    这里不能用单引号,只能为双引号 request.setCharacterEncoding('gb2312');    String user = request.getParameter(" ...

  10. [转载]HTTPS的工作原理

    HTTPS在传输数据之前需要客户端(浏览器)与服务端(网站)之间进行一次握手,在握手过程中将确立双方加密传输数据的密码信息.TLS/SSL协议不仅仅是一套加密传输的协议,更是一件经过艺术家精心设计的艺 ...