A1123. Is It a Complete AVL Tree

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.

A1123. Is It a Complete AVL Tree    A1123. Is It a Complete AVL Tree

A1123. Is It a Complete AVL Tree    A1123. Is It a Complete AVL Tree

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<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef struct NODE{
struct NODE* lchild, *rchild;
int data, lev;
}node;
int N, cnt = ;
int height(node* root){
if(root == NULL)
return ;
else return root->lev;
}
void update(node* root){
root->lev = max(height(root->lchild), height(root->rchild)) + ;
}
void L(node* &root){
node* temp = root;
root = root->rchild;
temp->rchild = root->lchild;
root->lchild = temp;
update(temp);
update(root);
}
void R(node* &root){
node* temp = root;
root = root->lchild;
temp->lchild = root->rchild;
root->rchild = temp;
update(temp);
update(root);
}
void insert(node* &root, int x){
if(root == NULL){
root = new node;
root->lchild = NULL;
root->rchild = NULL;
root->data = x;
root->lev = ;
return;
}
if(x <= root->data){
insert(root->lchild, x);
update(root);
if(abs(height(root->lchild) - height(root->rchild)) == ){
if(height(root->lchild->lchild) - height(root->lchild->rchild) == ){
R(root);
}else if(height(root->lchild->lchild) - height(root->lchild->rchild) == -){
L(root->lchild);
R(root);
}
}
}else{
insert(root->rchild, x);
update(root);
if(abs(height(root->lchild) - height(root->rchild)) == ){
if(height(root->rchild->rchild) - height(root->rchild->lchild) == ){
L(root);
}else if(height(root->rchild->rchild) - height(root->rchild->lchild) == -){
R(root->rchild);
L(root);
}
}
}
} int levelOrder(node* root){
int tag = , prt = ;
queue<node*> Q;
Q.push(root);
while(Q.empty() == false){
node* temp = Q.front();
Q.pop();
cnt++;
if(temp == NULL){
if(cnt < N + )
tag = ;
}else{
prt++;
if(prt == N)
printf("%d\n", temp->data);
else printf("%d ", temp->data);
Q.push(temp->lchild);
Q.push(temp->rchild);
}
}
return tag;
}
int main(){
scanf("%d", &N);
int num;
node* root = NULL;
for(int i = ; i < N; i++){
scanf("%d", &num);
insert(root, num);
}
int isCom = levelOrder(root);
if(isCom == )
printf("YES\n");
else printf("NO\n");
cin >> N;
return ;
}

总结:

1、按插入顺序建立平衡二叉树,然后再判断该树是否是完全二叉树。

2、建立平衡二叉树: 在左子树插入后,先更新根节点高度,再求平衡因子。求平衡因子判断平衡应判断左减右是否等于2,而不是绝对值。

3、左旋右旋共三步。旋转完成之后必须更新temp和root的高度,由于temp会成为root的子树,所以先更新temp高度,再更新root。

4、判断完全二叉树: 将null节点也加入队列。设置计数器记录访问节点个数,当访问时遇到空节点看计数器是否大于N,如果否,则不是完全二叉树。

上一篇:c++中sizeof的用法


下一篇:Bootstrap列表组