1123 Is It a Complete AVL Tree (30分)
codeflysafe Lv5

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:

1
2
5
88 70 61 63 65

Output 1:

1
2
70 63 88 61 65
YES

##Sample

Input 2:

1
2
8
88 70 61 96 120 90 65 68

Output 2:

1
2
88 65 96 61 70 90 120 68
NO

Solution

主要分两步:

  1. 建立avl树 (过程参见 AvlTree)
  2. 进行层序遍历,遍历过程中判断是否为完全二叉树
  3. 完全二叉树的定义为 最多有一个只有一个叶子的节点

Code

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
141
142
143
144
145
146
147
148
149
#include <iostream>
#include<cmath>
#include <queue>
using namespace std;


struct avl_node
{
struct avl_node *left, *right;
int data, height;
};
typedef struct avl_node *AvlTree, *AvlNode;

int height(AvlNode k)
{
return k ? k->height : -1;
}

// lr 旋转
AvlNode left_right_rotate(AvlNode k2)
{
AvlNode k1 = k2->left;
k2->left = k1->right;
k1->right = k2;

k2->height = max(height(k2->left), height(k2->right)) + 1;
k1->height = max(height(k1->left), height(k1->right)) + 1;
return k1;
}

// rl 旋转
AvlNode right_left_rotate(AvlNode k1)
{
AvlNode k2 = k1->right;
k1->right = k2->left;
k2->left = k1;

k1->height = max(height(k1->left), height(k1->right)) + 1;
k2->height = max(height(k2->left), height(k2->right)) + 1;
return k2;
}

// ll 旋转
AvlNode left_left_rotate(AvlNode k3)
{
k3->left = right_left_rotate(k3->left);
return left_right_rotate(k3);
}

// rr 旋转
AvlNode right_right_rotate(AvlNode k1)
{
k1->right = left_right_rotate(k1->right);
return right_left_rotate(k1);
}

AvlNode insert(AvlNode t, int val)
{
if (!t)
{
t = new avl_node();
t->data = val;
t->height = 0;
t->left = t->right = nullptr;
return t;
}
// self-check
if (t->data > val)
{
t->left = insert(t->left, val);
if (height(t->left) - height(t->right) == 2)
{
// lr 旋转
if (t->left->data > val)
t = left_right_rotate(t);
else
t = left_left_rotate(t);
}
}
else
{
t->right = insert(t->right, val);
if (height(t->right) - height(t->left) == 2)
{
// rl 旋转
if (t->right->data < val)
t = right_left_rotate(t);
else
t = right_right_rotate(t);
}
}
t->height = max(height(t->left), height(t->right)) + 1;
return t;
}

bool level_loop(AvlNode tree)
{
queue<AvlNode> q;
q.push(tree);
int flag = 1;
bool complete = true, leaf = false, falg = true;
while (!q.empty())
{
AvlNode node = q.front();
q.pop();
if (!flag)
cout << " ";
else
flag = false;
cout << node->data;
if (node->left)
q.push(node->left);
if (node->right)
q.push(node->right);
if (!node->left && node->right)
complete = false;
if (leaf && (node->left || node->right))
complete = false;
if (!node->left || !node->right)
leaf = true;
}
cout << endl;
return complete;
}

void pat_1123()
{

int n, i, val;
cin >> n;
AvlTree root = nullptr;
for (i = 0; i < n; i++)
{
cin >> val;
root = insert(root, val);
}
bool complete = level_loop(root);
if (complete)
cout << "YES";
else
cout << "NO";
}

int main()
{
pat_1123();
return 0;
}

  • 本文标题:1123 Is It a Complete AVL Tree (30分)
  • 本文作者:codeflysafe
  • 创建时间:2020-03-12 21:39:22
  • 本文链接:https://codeflysafe.github.io/2020/03/12/2020-03-12-1123-Is-It-a-Complete-AVL-Tree-(30分)/
  • 版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
 评论