您的位置:首页 > 其它

PAT (Advanced Level) 1123. Is It a Complete AVL Tree (30)

2017-03-12 12:42 543 查看


1123. Is It a Complete AVL Tree (30)

时间限制

400 ms

内存限制

65536 kB

代码长度限制

16000 B

判题程序

Standard

作者

CHEN, Yue

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树的概念;

刚开始提交一直有一个测试点过不去,想了一晚上呢,都不知道问题在哪里,后来终于忍不住了跑去问大神,大神一秒钟就指出了我的问题所在:在倒数第二层(除了整棵树只有一个节点的情况)有可能会出现没有填满的情况~~~这种烨满足AVL树,且不是完全的AVL树~~

详情可参见这个博客(真的写的非常好):http://blog.csdn.net/u011372705/article/details/49891599

说AVL树的问题复杂,主要是记得那些函数模版怎么写的,

然后这道题明白完全AVL树的概念,就可以做出来了

#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>

using namespace std;

const int maxn = 25;
int flag = 0;
int n,h;

struct node {
int data;
node* lchild;
node* rchild;
int height;
int layer;
};

queue<node*> tt;

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

node* newNode(int v) {
node* Node = new node;
Node->data = v;
Node->lchild = Node->rchild = NULL;
Node->height = 1;
return Node;
}

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->data) {
insert(root->lchild, v);
updateHeight(root);
if (getBalanceFactor(root) == 2) {
if (getBalanceFactor(root->lchild) == 1) {
R(root);
}
else if (getBalanceFactor(root->lchild) == -1) {
L(root->lchild);
R(root);
}
}
}
else {
insert(root->rchild, v);
updateHeight(root);
if (getBalanceFactor(root) == -2) {
if (getBalanceFactor(root->rchild) == -1) {
L(root);
}
else if (getBalanceFactor(root->rchild) == 1) {
R(root->rchild);
L(root);
}
}
}
}

node* Create(int data[], int n) {
node* root = NULL;
for (int i = 0; i < n; i++) {
insert(root, data[i]);
}
return root;
}

void DFS(node* &root,int ll) {
if (root->lchild == NULL && root->rchild == NULL) {
root->layer = ll;
return;
}
root->layer = ll;
if (root->lchild != NULL) { DFS(root->lchild, ll + 1); }
if (root->rchild != NULL) { DFS(root->rchild, ll + 1); }
}

void BFS(node* root) {
queue<node*> q;
q.push(root);

while (!q.empty()) {
node* N = q.front();
q.pop();

if (N->lchild != NULL) {
q.push(N->lchild);
}

if (N->rchild != NULL) {
q.push(N->rchild);
}

if (N->layer == h - 1) {
tt.push(N);
}

if (!q.empty()) {
printf("%d ", N->data);
}
else {
printf("%d\n", N->data);
}
}
}

void judge() {
if (h == 1) {
return ;
}
if (tt.size() < (1 << h - 2)) {
flag = 1;
return;
}
while (!tt.empty()) {
node* temp = tt.front();
tt.pop();

if (temp->lchild == NULL && temp->rchild != NULL) {
flag = 1;
return;
}

if (temp->lchild == NULL || temp->rchild == NULL) {
while (!tt.empty()) {
temp = tt.front();
tt.pop();
if (temp->lchild != NULL || temp->rchild != NULL) {
flag = 1;
return;
}
}
}
}
return;
}

int main() {
int data[maxn];
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &data[i]);
}
node* root=Create(data, n);
h = getHeight(root);

DFS(root,1);
BFS(root);
judge();

if (flag == 1) {
printf("NO\n");
}
else {
printf("YES\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: