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 <stdio.h>
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
#include <queue>
#include <set>
using namespace std;
struct node{
int data,height;
node *lchild,*rchild;
};
node* newNode(int v){
node* root = new node;
root->data = v;
root->lchild = root->rchild = NULL;
root->height = ;
return root;
}
int getHeight(node* root){
if(root==NULL)return ;
return root->height;
}
void updateHeight(node* root){
root->height = max(getHeight(root->lchild),getHeight(root->rchild))+;
}
int getBalanceFactor(node *root){
return getHeight(root->lchild)-getHeight(root->rchild);
}
void L(node* &root){
node* tmp=root->rchild;
root->rchild = tmp->lchild;
tmp->lchild = root;
updateHeight(root);
updateHeight(tmp);
root=tmp;
}
void R(node* &root){
node* tmp=root->lchild;
root->lchild = tmp->rchild;
tmp->rchild = root;
updateHeight(root);
updateHeight(tmp);
root = tmp;
}
void insert(node* &root,int v){
if(root==NULL){
root = newNode(v);
return;
}
if(v<root->data){
insert(root->lchild,v);
updateHeight(root);
if(getBalanceFactor(root)==){
if(getBalanceFactor(root->lchild)==){
R(root);
}
else if(getBalanceFactor(root->lchild)==-){
L(root->lchild);
R(root);
}
}
}
else{
insert(root->rchild,v);
updateHeight(root);
if(getBalanceFactor(root)==-){
if(getBalanceFactor(root->rchild)==-){
L(root);
}
else if(getBalanceFactor(root->rchild)==){
R(root->rchild);
L(root);
}
}
}
}
node* create(int data[],int n){
node* root = NULL;
for(int i=;i<n;i++){
insert(root,data[i]);
}
return root;
}
int flag=,after=;
void levelOrder(node* root,int n){
queue<node*> q;
int num=;
q.push(root);
while(!q.empty()){
node* now = q.front();
num++;
printf("%d",now->data);
if(num!=n)printf(" ");
else printf("\n");
q.pop();
if(now->lchild!=NULL){
if(after==)flag=;
q.push(now->lchild);
}
else after=;
if(now->rchild!=NULL){
if(after==)flag=;
q.push(now->rchild);
}
else after=;
}
}
int main(){
int n;
scanf("%d",&n);
int data[];
for(int i=;i<n;i++){
scanf("%d",&data[i]);
}
node* root = create(data,n);
levelOrder(root,n);
printf("%s",flag==?"YES":"NO");
}

注意点:第一次做到平衡二叉树和完全二叉树的判定的题目,重新看了一遍算法笔记,还是很生疏。AVL的插入左旋右旋要熟练记住,考前再看一眼。

完全二叉树的判定:层序遍历时,出现了有子节点为空的节点,后面的节点还出现子节点非空的情况,这就不是完全二叉树

最新文章

  1. day7_subprocess模块和面向对象,反射
  2. 各种解析漏洞获取Webshell
  3. 英文分词算法(Porter stemmer)
  4. JAVA调用动态链接库DLL之JNative学习
  5. php Internal Server Error
  6. linux常用命令之--目录与文件的操作命令
  7. cmder使用手册
  8. iOS学习之懒加载
  9. hdu4756 Install Air Conditioning(MST + 树形DP)
  10. asp于Server.MapPath用法
  11. EasyUI学习笔记---Datagrid真分页
  12. APP自动化框架LazyAndroid使用手册(4)--测试模板工程详解
  13. HTML5-2
  14. 7.2.4 else与if配对
  15. Hadoop优化 第一篇 : HDFS/MapReduce
  16. ORACLE 归档日志打开关闭方法
  17. 页面可见性判断:document.hidden与visibilitychange事件
  18. VMWare虚拟机安装Linux系统时安装界面显示不全的解决、Linux分区
  19. Cloud Resource
  20. IdentityServer4登陆中心

热门文章

  1. java基础-配置java的环境变量
  2. Docker compose 与 Docker swarm
  3. 【代码笔记】Web-HTML-段落
  4. Intent调用常见系统组件
  5. Django ModelForm 校验数据格式
  6. NB-IOT模块 M5310-A接入百度开放云IOT Hub MQTT
  7. 3.3Python数据处理篇之Numpy系列(三)---数组的索引与切片
  8. asp在线压缩和解压缩文件(文件夹)
  9. MATLAB数值分析实验
  10. Leviticus