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 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
作者: CHEN, Yue
单位: 浙江大学
时间限制: 400 ms
内存限制: 64 MB
#include <iostream>
#include <vector>
#include<algorithm>
#include <cmath>
#include<map>
#include<cstring>
#include<queue>
#include<string>
#include<set>
#include<stack>
using namespace std;
typedef long long ll;
const int maxn=1010,inf=100000000;
struct node{
int data,height;
node *lchild,*rchild;
};
int n,data;
int get_h(node *root){
if(!root) return 0; //好重要呢;
return root->height;
}
void update(node *&root){
root->height=max(get_h(root->lchild),get_h(root->rchild))+1;
}
void l(node *&root){
node *temp=root->rchild;
root->rchild=temp->lchild;
temp->lchild=root;
update(root);update(temp);
root=temp;
}
void r(node *&root){
node *temp=root->lchild;
root->lchild=temp->rchild;
temp->rchild=root;
update(root);update(temp);
root=temp;
}
int fac(node *root){
return get_h(root->lchild)-get_h(root->rchild );
}
void insert(int data,node* &root){ //又想坑我;
if(!root){
root=new node;root->height=1;
root->data=data;
root->lchild=root->rchild=NULL;
}
if(data<root->data){
insert(data,root->lchild);
update(root);
if(fac(root)==2){
if(fac(root->lchild)==1) r(root);
else if(fac(root->lchild)==-1){
l(root->lchild);r(root);
}
}
}
else if(data>root->data){
insert(data,root->rchild);
update(root);
if(fac(root)==-2){
if(fac(root->rchild)==-1) l(root);
else if(fac(root->rchild)==1){
r(root->rchild);l(root);
}
}
}
}
void bfs(node *root){
queue<node*>q;
q.push(root);int cnt=0;
while(!q.empty()){
node* now=q.front();
if(cnt!=0) cout<<" ";
cnt++;cout<<now->data;
if(now->lchild){
q.push(now->lchild);
}
if(now->rchild){
q.push(now->rchild);
}
q.pop();
}
}
bool flag=1;
void dfs(node *root,int idx){
if(!root) return;
dfs(root->lchild,idx*2);
if(idx>n) flag=0;
dfs(root->rchild,idx*2+1);
}
int main(){
cin>>n;
node* root=NULL;
for(int i=0;i<n;i++){
cin>>data;
insert(data,root);
}
bfs(root);cout<<endl;
dfs(root,1);
if(flag) cout<<"YES";
else cout<<"NO";
}

浙公网安备 33010602011771号