A1123 Is It a Complete AVL Tree (30 point(s))

AVL树建立+完全二叉树判断

1. 原文

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.

F1.jpg F2.jpg
F3.jpg F4.jpg

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:

1
2
5
88 70 61 63 65

Sample output 1:

1
2
70 63 88 61 65
YES

Sample Input 2:

1
2
8
88 70 61 96 120 90 65 68

Sample output 2:

1
2
88 65 96 61 70 90 120 68
NO

2. 解析

固定套路

  • 建树
  • 旋转
  • 层序

3. AC代码

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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
struct node{
int data,height;
node *lchild,*rchild;
};
node* newNode(int x){
node* root=new node;
root->data=x;
root->height=1;
root->lchild=root->rchild=NULL;
return root;
}
int getHeight(node* root)
{
if (root==NULL)
{
return 0;
}else{
return root->height;
}
}
int getFactor(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 create(int x,node* &root){
if (root==NULL)
{
root=newNode(x);
return;
}else if(root->data>x){
create(x,root->lchild);
updateHeight(root);
if (getFactor(root)==2)
{
if (getFactor(root->lchild)==1)
{
R(root);
}else if (getFactor(root->lchild)==-1)
{
L(root->lchild);
R(root);
}
}
}else{
create(x,root->rchild);
updateHeight(root);
if (getFactor(root)==-2)
{
if (getFactor(root->rchild)==-1)
{
L(root);
}else if (getFactor(root->rchild)==1)
{
R(root->rchild);
L(root);
}
}
}
}
int complete=0; int key=0; int flag=0;
void bfs(node* root)
{
queue<node*> q;
q.push(root);
while(!q.empty()){
node* temp=q.front();
q.pop();
if (key==0)
{
printf("%d",temp->data);
key++;
}else{
printf(" %d",temp->data);
}
if (temp->lchild!=NULL)
{
q.push(temp->lchild);
if (flag==1)
{
complete=1;
}
}else{
flag=1;
}
if (temp->rchild!=NULL)
{
q.push(temp->rchild);
if (flag==1)
{
complete=1;
}
}else{
flag=1;
}
}
}
int main()
{
int n;
scanf("%d",&n);
int a;
node* root=NULL;
for (int i = 0; i < n; ++i)
{
scanf("%d",&a);
create(a,root);
}
bfs(root);
printf("\n");
if (complete==0)
{
printf("YES\n");
}else{
printf("NO\n");
}
return 0;
}
本文结束  感谢您的阅读
  • 本文作者: Wang Ting
  • 本文链接: /zh-CN/2019/09/03/A1123/
  • 发布时间: 2019-09-03 08:34
  • 更新时间: 2021-10-29 14:09
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!