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<bits/stdc++.h>
using namespace std;
struct node{
int v,height;//v为结点的权值,height为结点的高度
int id=0;//层次遍历中用到的结点的编号
node *lchild;
node *rchild;
};
//生成一个新结点的方法
node *newNode(int v){
node *root=new node;
root->v=v;
root->height=1;
root->lchild=NULL;
root->rchild=NULL;
return root;
}
//获取以root为根结点的子树的当前的height
int getHeight(node *root){
if(root==NULL) return 0;//空结点的高度为0;
return root->height;
}
//计算root的平衡因子
int getBalanceFactor(node *root){
//左子树的高度-右子树的高度
return getHeight(root->lchild)-getHeight(root->rchild);
}
//更新root的高度
void updateHeight(node *root){
root->height=max(getHeight(root->lchild),getHeight(root->rchild))+1;
}
//左旋
void L(node *&root){
node *temp=root->rchild;
root->rchild=temp->lchild;
temp->lchild=root;
updateHeight(root);
updateHeight(temp);
root=temp;
}
//右旋
void R(node *&root){
node *temp=root->lchild;
root->lchild=temp->rchild;
temp->rchild=root;
updateHeight(root);
updateHeight(temp);
root=temp;
}
//插入权值为v的结点
void insert(node *&root,int v){
if(root==NULL){
root=newNode(v);
return;
}
if(v<root->v){
insert(root->lchild,v);
updateHeight(root);//更新树高
if(getBalanceFactor(root)==2){
if(getBalanceFactor(root->lchild)==1){
//LL型
R(root);
}else if(getBalanceFactor(root->lchild)==-1){
//LR型
L(root->lchild);
R(root);
}
}
}else{
insert(root->rchild,v);
updateHeight(root);
if(getBalanceFactor(root)==-2){
if(getBalanceFactor(root->rchild)==-1){
//RR型
L(root);
}else if(getBalanceFactor(root->rchild)==1){
//RL型
R(root->rchild);
L(root);
}
}
}
}
int n,ff;//ff用来标志是否是完全二叉树
void level_order(node *root){
queue<node *>q;
q.push(root);
int last=0,flag=0;//ff用来标志是否是一颗完全二叉树
root->id=1;
while(!q.empty()){
node *p=q.front();
q.pop();
if(flag) printf(" ");
printf("%d",p->v);
flag=1;
if((p->id)==last+1){
last++;
}else{
ff=0;
}
if(p->lchild!=NULL){
p->lchild->id=p->id*2;
q.push(p->lchild);
}
if(p->rchild!=NULL){
p->rchild->id=p->id*2+1;
q.push(p->rchild);
}
}
}
int main(){
scanf("%d",&n);
int x;
node *root=NULL;
for(int i=1;i<=n;i++){
scanf("%d",&x);
insert(root,x);
}
ff=1;
level_order(root);
printf("\n");
if(ff) printf("YES\n");
else printf("NO\n");
return 0;
}