1123 Is It a Complete AVL Tree (30分)
作者:CHEN, Yue
单位:浙江大学
代码长度限制:16 KB
时间限制:400 ms
内存限制:64 MB
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
题意:
给出一数列,按照数列顺序将点插入进平衡二叉树,将所构建的数用层序遍历输出,并判断这棵树是否是完全二叉树。
思路:
按照平衡二叉树的模板默写,左旋,右旋,插入,平衡因子,高度更新等信息。用bfs输出层序,由于完全二叉树(1-n)满足,左子树的下标是结点的2倍,右子树的下标是结点的2倍+1,按照完全二叉树的性质去给结点定下标,当有下标大于n时,那么它不是完全二叉树。
参考代码:
#include <iostream>
#include <queue>
using namespace std;
struct Node {
int val, height;
Node *left, *right;
Node(int v) : val(v), height(1), left(NULL), right(NULL) {}
};
int getheight(Node *root) {
if (root == NULL)return 0;
return root->height;
}
void updateheight(Node *&root) {
root->height = max(getheight(root->left), getheight(root->right)) + 1;
}
int balanceheight(Node *root) {
return getheight(root->left) - getheight(root->right);
}
void L(Node *&root) {
Node *temp = root->right;
root->right = temp->left;
temp->left = root;
updateheight(root);
updateheight(temp);
root = temp;
}
void R(Node *&root) {
Node *temp = root->left;
root->left = temp->right;
temp->right = root;
updateheight(root);
updateheight(temp);
root = temp;
}
void insert(Node *&root, int v) {
if (root == NULL) {
root = new Node(v);
return;
}
if (v < root->val) {
insert(root->left, v);
updateheight(root);
if (balanceheight(root) == 2) {
if (balanceheight(root->left) == 1)R(root);
else if (balanceheight(root->left) == -1) {
L(root->left);
R(root);
}
}
} else {
insert(root->right, v);
updateheight(root);
if (balanceheight(root) == -2) {
if (balanceheight(root->right) == -1)L(root);
else if (balanceheight(root->right) == 1) {
R(root->right);
L(root);
}
}
}
}
int n, num, cnt = 0, flag = 1;
void levelorder(Node *root, int index) {
queue<pair<Node *, int>> q;
q.push({root, index});
while (!q.empty()) {
Node *top = q.front().first;
int ind = q.front().second;
if (ind > n)flag = 0;
q.pop();
if (cnt != 0)printf(" ");
cnt++;
printf("%d", top->val);
if (top->left != NULL)q.push({top->left, ind * 2});
if (top->right != NULL)q.push({top->right, ind * 2 + 1});
}
printf("\n%s\n", flag == 1 ? "YES" : "NO");
}
int main() {
Node *root = NULL;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &num);
insert(root, num);
}
levelorder(root, 1);
return 0;
}
如有错误,欢迎指正