题目描述
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
考点
1.平衡二叉树的插入;
2.层序遍历;
3.完全二叉树。
思路
1.如何平衡二叉树
平衡二叉树的失衡调整主要是通过旋转最小失衡子树来实现的。总共有LL、LR、RL、RR四种情况,分别对应右旋、左旋、先右旋再左旋、先左旋再右旋。RL和LR都可以调用LL、RR的代码实现。
2.层序遍历
层序遍历一般用队列实现,可以利用c++中的queue,方便快捷。
3.完全二叉树的判断
如果有已经出现了孩子为空的结点,后续出现了孩子不为空结点,则非完全二叉树。
代码
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
struct node {
int val;
struct node *left, *right;
};
node* LR(node *tree) {
node *temp = tree->right;
tree->right = temp->left;
temp->left = tree;
return temp;
}
node* RR(node *tree) {
node *temp = tree->left;
tree->left = temp->right;
temp->right = tree;
return temp;
}
node *LRR(node *tree) {
tree->left = LR(tree->left);
return RR(tree);
}
node *RLR(node *tree) {
tree->right = RR(tree->right);
return LR(tree);
}
int getHeight(node *tree) {
if (tree == NULL)return 0;
return max(getHeight(tree->left), getHeight(tree->right)) + 1;
}
node* insert(node *tree, int val) {
if (tree == NULL) {
tree = new node();
tree->val = val;
}
else if (tree->val > val) {
tree->left = insert(tree->left, val);
if (getHeight(tree->left) - getHeight(tree->right) >= 2) {
if (val < tree->left->val) tree = RR(tree);
else tree = LRR(tree);
}
}
else {
tree->right = insert(tree->right, val);
if (getHeight(tree->right) - getHeight(tree->left) >= 2) {
if (val > tree->right->val) tree = LR(tree);
else tree = RLR(tree);
}
}
return tree;
}
int after = 1, isComplete = 1;
void levelorder(node *tree) {
queue<node *> q;
int count = 0;
q.push(tree);
while (!q.empty()) {
node *temp = q.front();
q.pop();
cout << (count == 0 ? "" : " ") << temp->val;
count++;
if (temp->left != NULL) {
q.push(temp->left);
if (after == 0) {
isComplete = 0;
}
}
else after = 0;
if (temp->right != NULL) {
q.push(temp->right);
if (after == 0) {
isComplete = 0;
}
}
else after = 0;
}
}
int main() {
int n, i, t;
cin >> n;
node *tree = NULL;
for (i = 0; i < n; i++) {
cin >> t;
tree = insert(tree, t);
}
levelorder(tree);
cout << endl;
cout << (isComplete==1? "YES" : "NO") << endl;
return 0;
}