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
 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef struct NODE{
struct NODE* lchild, *rchild;
int data, lev;
}node;
int N, cnt = ;
int height(node* root){
if(root == NULL)
return ;
else return root->lev;
}
void update(node* root){
root->lev = max(height(root->lchild), height(root->rchild)) + ;
}
void L(node* &root){
node* temp = root;
root = root->rchild;
temp->rchild = root->lchild;
root->lchild = temp;
update(temp);
update(root);
}
void R(node* &root){
node* temp = root;
root = root->lchild;
temp->lchild = root->rchild;
root->rchild = temp;
update(temp);
update(root);
}
void insert(node* &root, int x){
if(root == NULL){
root = new node;
root->lchild = NULL;
root->rchild = NULL;
root->data = x;
root->lev = ;
return;
}
if(x <= root->data){
insert(root->lchild, x);
update(root);
if(abs(height(root->lchild) - height(root->rchild)) == ){
if(height(root->lchild->lchild) - height(root->lchild->rchild) == ){
R(root);
}else if(height(root->lchild->lchild) - height(root->lchild->rchild) == -){
L(root->lchild);
R(root);
}
}
}else{
insert(root->rchild, x);
update(root);
if(abs(height(root->lchild) - height(root->rchild)) == ){
if(height(root->rchild->rchild) - height(root->rchild->lchild) == ){
L(root);
}else if(height(root->rchild->rchild) - height(root->rchild->lchild) == -){
R(root->rchild);
L(root);
}
}
}
} int levelOrder(node* root){
int tag = , prt = ;
queue<node*> Q;
Q.push(root);
while(Q.empty() == false){
node* temp = Q.front();
Q.pop();
cnt++;
if(temp == NULL){
if(cnt < N + )
tag = ;
}else{
prt++;
if(prt == N)
printf("%d\n", temp->data);
else printf("%d ", temp->data);
Q.push(temp->lchild);
Q.push(temp->rchild);
}
}
return tag;
}
int main(){
scanf("%d", &N);
int num;
node* root = NULL;
for(int i = ; i < N; i++){
scanf("%d", &num);
insert(root, num);
}
int isCom = levelOrder(root);
if(isCom == )
printf("YES\n");
else printf("NO\n");
cin >> N;
return ;
}

总结:

1、按插入顺序建立平衡二叉树,然后再判断该树是否是完全二叉树。

2、建立平衡二叉树: 在左子树插入后,先更新根节点高度,再求平衡因子。求平衡因子判断平衡应判断左减右是否等于2,而不是绝对值。

3、左旋右旋共三步。旋转完成之后必须更新temp和root的高度,由于temp会成为root的子树,所以先更新temp高度,再更新root。

4、判断完全二叉树: 将null节点也加入队列。设置计数器记录访问节点个数,当访问时遇到空节点看计数器是否大于N,如果否,则不是完全二叉树。

A1123. Is It a Complete AVL Tree的更多相关文章

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

  2. PAT甲级——A1123 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 ...

  3. PAT_A1123#Is It a Complete AVL Tree

    Source: PAT A1123 Is It a Complete AVL Tree (30 分) Description: An AVL tree is a self-balancing bina ...

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

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

  5. 1123 Is It a Complete AVL Tree

    1123 Is It a Complete AVL Tree(30 分) An AVL tree is a self-balancing binary search tree. In an AVL t ...

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

  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 (完全AVL树的判断)

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

  9. PAT 1123 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 ...

随机推荐

  1. 基于vue-cli,sass,vant的移动端项目

    项目架构 开始 vue init webpack    项目名称         //新建项目,cd进入新项目 npm install axios                    //先安装! ...

  2. MT4用EA测试历史数据时日志出现:stopped because of stop out

    今天用嘉盛的MT4测试一个EA,谁知道才走了十几天数据就完 了,看结果本金也没亏完啊,才亏了一半,而且我测的是1年的时间. 查看日志一有条警告:stopped because of stop out, ...

  3. SpringBoot 4.SpringBoot 整合 devtools 实现热部署

    一.添加 devtools 依赖 <!-- Spring boot 热部署 : 此热部署会遇到 java.lang.ClassCastException 异常 --> <!-- op ...

  4. Python 中关于 round 函数的小坑

    参考: http://www.runoob.com/w3cnote/python-round-func-note.html

  5. python爬虫之pandas

    一.简介: Python Data Analysis Library 或 pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的.Pandas 纳入了大量库和一些标准的数据模 ...

  6. nginx 负载均衡(默认算法)

    使用 nginx 的upstream模块只需要几步就可以实现一个负载均衡: 在 nginx 配置文件中添加两个server server { listen ; server_name 192.168. ...

  7. NetScope脱机(localhost)使用[转】

    https://blog.csdn.net/jiwu999/article/details/79626773 方法: step1:git clone https://github.com/ethere ...

  8. oracle复习(二)

    十一.replace 替换格式:(原字符串,要查找的字符或字符串,替换的字符或字符串)select replace('hello world','o','a') from dual; //替换时区分大 ...

  9. Ubuntu开发用新机安装流程

    1.SSH安装 Ubuntu缺省已安装客户端,此处安装服务端 sudo apt-get install openssh-server 确认sshserver是否启动 netstat -tlp | gr ...

  10. python爬虫requests模块

    requests库的七个主要方法 1. requests.requests(method, url, **kwargs) 构造一个请求,支撑以下各方法的基础方法 method:请求方式,对应get/p ...