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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
vector<int> layer;

struct node{
int v,height;
int order;
node *lchild,*rchild;
};
node* newNode(int v){
node* Node = new node;
Node->v = v;
Node->height = 1;
Node->lchild = Node->rchild = NULL;
return Node;
}
int getHeight(node* root){
if(root == NULL) return 0;
return root->height;
}
int getBalanceFactor(node* root){
return getHeight(root->lchild)-getHeight(root->rchild);
}
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;
}
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);
}
}
}
}
node *last = new node;
void layerOrder(node* root,vector<int> &vi){
queue<node*> q;
q.push(root);
root->order = 1;
while (!q.empty()) {
node* now = q.front();
last = now;
q.pop();
vi.push_back(now->v);
if (now->lchild != NULL) {
q.push(now->lchild);
now->lchild->order = 2 * now->order;
}
if (now->rchild != NULL) {
q.push(now->rchild);
now->rchild->order = 2 * now->order + 1;
}
}
}
int main(){
int n, temp;
scanf("%d", &n);
node* root = NULL;
for (int i = 0; i < n; i++) {
scanf("%d", &temp);
insert(root, temp);
}
layerOrder(root, layer);
for (int i = 0; i < n; i++) {
printf("%d", layer[i]);
if (i < n - 1) printf(" ");
else printf("n");
}
if (last->order == n) {
printf("YESn");
}else{
printf("NOn");
}
return 0;
}