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
题目大意:判断是否是一颗完全avl树,先建avl树,然后bfs输出,在输出的时候记录最大的位置判断是否完全树。
#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
typedef struct Node *Position;
queue<Position> q;
int maxIdx = 0;
struct Node
{
int x;
Position left;
Position right;
int height;
};
int Height(Position node)
{
if(node == NULL)
return -1;
return node->height;
}
Position RotateToRight(Position root)
{
Position newRoot;
newRoot = root->left;
root->left = newRoot->right;
newRoot->right = root;
root->height = max(Height(root->left), Height(root->right)) + 1;
newRoot->height = max(Height(newRoot->left), root->height) + 1;
return newRoot;
}
Position RotateToLeft(Position root)
{
Position newRoot;
newRoot = root->right;
root->right = newRoot->left;
newRoot->left = root;
root->height = max(Height(root->left), Height(root->right)) + 1;
newRoot->height = max(Height(newRoot->right), root->height) + 1;
return newRoot;
}
Position RotateToLeftRight(Position root)
{
root->left = RotateToLeft(root->left);
return RotateToRight(root);
}
Position RotateToRightLeft(Position root)
{
root->right = RotateToRight(root->right);
return RotateToLeft(root);
}
Position insert(Position node, int x)
{
if(node == NULL){
node = new Node();
if(node == NULL)
printf("Out of space!");
else {
node->x = x;
node->left = NULL;
node->right = NULL;
node->height = 0;
}
} else if(x < node->x) {
node->left = insert(node->left, x);
if(Height(node->left) - Height(node->right) == 2){
if(x < node->left->x)
node = RotateToRight(node);
else
node = RotateToLeftRight(node);
}
} else if(node->x < x){
node->right = insert(node->right, x);
if(Height(node->right) - Height(node->left) == 2){
if(node->right->x < x)
node = RotateToLeft(node);
else
node = RotateToRightLeft(node);
}
}
node->height = max(Height(node->left), Height(node->right)) + 1;
return node;
}
void bfs(Position tree)
{
int idx = 0;
while(1){
if(tree->left != NULL){
q.push(tree->left);
maxIdx = 2 * idx + 1;
}
if(tree->right != NULL){
q.push(tree->right);
maxIdx = 2 * idx + 2;
}
++idx;
printf("%d", q.front()->x);
q.pop();
if(q.empty())
break;
else {
printf(" ");
tree = q.front();
}
}
}
int main()
{
int n, x;
scanf("%d", &n);
Position avlTree = NULL;
for(int i = 0; i < n; ++i){
scanf("%d", &x);
avlTree = insert(avlTree, x);
}
q.push(avlTree);
bfs(avlTree);
printf("\n");
if(maxIdx == n - 1)
printf("YES");
else
printf("NO");
return 0;
}