题目
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.
:::hljs-center
![]() | ![]() |
---|---|
![]() | ![]() |
:::
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 ) N(\le 20) 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
题目大意
按照所给序列建立平衡二叉树,然后层次遍历这棵平衡二叉树,并且判断这棵平衡二叉树是否是完全二叉树。
思路
先按照序列建立平衡二叉树,平衡二叉树的建立过程可以见我的另一篇博客,具体也可见代码;判断完全二叉树利用完全二叉树的性质,将根节点编号为一,左右子树分别编号为2,3,依次类推,给所有节点编号,若所有节点编号为连续的 1 → N 1\rightarrow N 1→N,则为完全二叉树;
代码
#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
struct node{
int data;
node *left, *right;
int id;
};
int getHeight(node *root){
if(root == NULL)
return 0;
return max(getHeight(root->left), getHeight(root->right)) + 1;
}
int getBalance(node *root){
return getHeight(root->left) - getHeight(root->right);
}
void R(node* &root){
node *temp = root->left;
root->left = temp->right;
temp->right = root;
root = temp;
}
void L(node* &root){
node *temp = root->right;
root->right = temp->left;
temp->left = root;
root = temp;
}
void insert(node* &root, int x){
if(root == NULL){
root = new node;
root->data = x;
root->left = root->right = NULL;
return;
}
if(x < root->data){
insert(root->left, x);
if(getBalance(root) == 2){
if(getBalance(root->left) == 1)
R(root);
else if(getBalance(root->left) == -1){
L(root->left);
R(root);
}
}
}
else{
insert(root->right, x);
if(getBalance(root) == -2){
if(getBalance(root->right) == -1)
L(root);
else if(getBalance(root->right) == 1){
R(root->right);
L(root);
}
}
}
}
int main(){
int n;
node *root = NULL;
scanf("%d", &n);
for(int i=0, t; i<n; i++){
scanf("%d", &t);
insert(root, t);
}
queue<node*> que;
vector<int> ans;
int sum = 0;
root->id = 1;
que.push(root);
while(!que.empty()){
int t = que.size();
for(int i=0; i<t; i++){
node *temp = que.front();
que.pop();
sum += temp->id;
ans.push_back(temp->data);
if(temp->left != NULL){
temp->left->id = temp->id*2;
que.push(temp->left);
}
if(temp->right != NULL){
temp->right->id = temp->id*2+1;
que.push(temp->right);
}
}
}
for(int i=0; i<n; i++){
if(i)
printf(" ");
printf("%d", ans[i]);
}
printf("\n");
if(sum == (n+1)*n/2)
printf("YES\n");
else
printf("NO\n");
return 0;
}