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
模板题
平衡二叉树的旋转基础知识:
待写。
代码:
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
struct node{
int data,height=0;
node *lchild,*rchild;
};
int count_height(node* &root){
if(root==NULL) return -1;
else return root->height;
}
void LL(node* &root){
node *tmp=root->lchild;
root->lchild=tmp->rchild;
tmp->rchild=root;
root->height=max(count_height(root->lchild),count_height(root->rchild))+1;
tmp->height=max(count_height(tmp->lchild),count_height(tmp->rchild))+1;
root=tmp;
}
void RR(node* &root){
node *tmp=root->rchild;
root->rchild=tmp->lchild;
tmp->lchild=root;
root->height=max(count_height(root->lchild),count_height(root->rchild))+1;
tmp->height=max(count_height(tmp->lchild),count_height(tmp->rchild))+1;
root=tmp;
}
void RL(node* &root){
LL(root->rchild);
RR(root);
}
void LR(node* &root){
RR(root->lchild);
LL(root);
}
void insert(int val,node* &root){
if(root==NULL){
root=new node();
root->data=val;
return;
}
if(root->data < val){
insert(val,root->rchild);
if(count_height(root->rchild)-count_height(root->lchild) > 1){
if(val > root->rchild->data) RR(root);
else RL(root);
}
}
else{
insert(val,root->lchild);
if(count_height(root->lchild)-count_height(root->rchild) > 1){
if(val > root->lchild->data) LR(root);
else LL(root);
}
}
root->height=max(count_height(root->rchild),count_height(root->lchild))+1;
}
int main(){
int n,val;
scanf("%d", &n);
node* root = NULL;
for (int i = 0; i < n; i++){
scanf("%d", &val);
insert(val, root);
}
queue<node*> q;
q.push(root);
vector<node*> level_order(1,NULL);
while (!q.empty()){
node* top = q.front();
q.pop();
level_order.push_back(top);
if (top->lchild) q.push(top->lchild);
if (top->rchild) q.push(top->rchild);
}
for (int i = 1; i < n; i++){
printf("%d ", level_order[i]->data);
}
printf("%d\n", level_order[n]->data);
bool flag = true;
for (int i = 1; i <= n; i++){
if (2 * i <= n && (!level_order[i]->lchild || level_order[i]->lchild->data != level_order[2 * i]->data)){
flag = false;
break;
}
else if(2 * i + 1 <= n && (!level_order[i]->rchild || level_order[i]->rchild->data != level_order[2 * i + 1]->data)){
flag = false;
break;
}
}
if (flag) printf("YES\n");
else printf("NO\n");
return 0;
}