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.

1123. Is It a Complete AVL Tree (30)    1123. Is It a Complete AVL Tree (30)

1123. Is It a Complete AVL Tree (30)    1123. Is It a Complete AVL Tree (30)

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 <queue>
#include <algorithm>
using namespace std;
struct Node{
int data, h;
Node *left, *right;
};
bool completeAVL = true;
int printFlag; Node *newNode(int x){
Node *p = new Node();
p->data = x;
p->left = p->right = NULL;
p->h = ;
return p;
}
int getHeight(Node *root){//获得高度
if(root == NULL) return ;
return root->h;
} int getBanance(Node *root){//获得平衡因子
if(root == NULL) return ;
return getHeight(root->left)-getHeight(root->right);
} void updateHeight(Node *root){//更新高度
if(root){
root->h = max(getHeight(root->left), getHeight(root->right))+;
}
} void R(Node *&root){//右旋
Node *p = root->left;
root->left = p->right;
p->right = root;
updateHeight(root);
updateHeight(p);
root = p;
} void L(Node *&root){//左旋
Node *p = root->right;
root->right = p->left;
p->left = root;
updateHeight(root);
updateHeight(p);
root = p;
} void insert(Node *&root, int x){
if(root == NULL){
root = newNode(x);
return;
}
if(x < root->data){
insert(root->left, x);
updateHeight(root);
if(getBanance(root) == ){
if(getBanance(root->left) == ){
R(root);
}else{//-1
L(root->left);
R(root);
}
}
}else{
insert(root->right, x);
updateHeight(root);
if(getBanance(root) == -){
if(getBanance(root->right) == -){
L(root);
}else{//
R(root->right);
L(root);
}
}
}
} void levelOrder(Node *root){
queue<Node *> q;
q.push(root);
int hasMeetLeaf = false;
while(!q.empty()){
Node* t = q.front(); q.pop();
if(t->left){
if(hasMeetLeaf) completeAVL = false;
q.push(t->left);
}else{
hasMeetLeaf = true;
}
if(t->right){
if(hasMeetLeaf) completeAVL = false;
q.push(t->right);
}else{
hasMeetLeaf = true;
}
if(-- printFlag) printf("%d ", t->data);
else printf("%d\n", t->data);
}
} int main()
{
int N;
Node *root = NULL;
scanf("%d", &N);
for(int i = ; i < N; i ++){
int x;
scanf("%d", &x);
insert(root, x);
}
printFlag = N; levelOrder(root);
if(completeAVL) printf("YES\n");
else printf("NO\n");
return ;
}
上一篇:html基础标签-1-pre预格式标签


下一篇:Bower安装