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
题目大意及思路
根据所给数据,依次插入,建立AVL树,输出其层次遍历序列,并判断该树是否为完全二叉树。
建立AVL树的方法参考算法笔记,完全二叉树的判断思路:当遍历到第一个空结点时,后面的结点若有不为空结点的,则不能构成完全二叉树。
AC代码
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef struct node{
int data, height;
node *lchild, *rchild;
}*BTree, BNode;
//创建新结点
BNode* newNode(int v){
BNode* Node = new node;
Node->data = v;
Node->height = 1;//高度初始为1
Node->lchild = NULL;
Node->rchild = NULL;
return Node;
}
//获取该根节点子树当前高度
int getHeight(BTree &BT){
if(BT == NULL) return 0;
return BT->height;
}
//计算结点平衡因子
int getBalanceFactor(BNode* &Node){
//左子树高度-右子树高度
return getHeight(Node->lchild) - getHeight(Node->rchild);
}
//更新结点高度
int updateHeight(BNode* &Node){
Node->height = max(getHeight(Node->lchild),getHeight(Node->rchild)) + 1;
}
//左旋
void L(BTree &BT){
BNode* temp = BT->rchild;
BT->rchild = temp->lchild;
temp->lchild = BT;
updateHeight(BT);//从下往上更新
updateHeight(temp);
BT = temp;
}
//右旋
void R(BTree &BT){
BNode* temp = BT->lchild;
BT->lchild = temp->rchild;
temp->rchild = BT;
updateHeight(BT);
updateHeight(temp);
BT = temp;
}
//插入
void insert(BNode* &Node, int v){
if(Node == NULL){
Node = newNode(v);
return;
}
if(v < Node->data){
insert(Node->lchild, v);
updateHeight(Node);//更新该子树高度
if(getBalanceFactor(Node) == 2){//失衡
if(getBalanceFactor(Node->lchild) == 1) //LL型
R(Node);
else if(getBalanceFactor(Node->lchild) == -1){ //LR型
L(Node->lchild);
R(Node);
}
}
}
else{
insert(Node->rchild, v);
updateHeight(Node);//更新该子树高度
if(getBalanceFactor(Node) == -2){//失衡
if(getBalanceFactor(Node->rchild) == -1) //RR型
L(Node);
else if(getBalanceFactor(Node->rchild) == 1){ //RL型
R(Node->rchild);
L(Node);
}
}
}
}
//建立
BTree create(int data[], int n){
BTree BT = NULL;
for(int i = 0; i < n; i++)
insert(BT, data[i]);
return BT;
}
//层序遍历并判断是否为完全二叉树
vector<int> vc; //层序序列
bool flag = true;
bool after = false; //是否遍历到第一个空结点的父亲结点
void levelorder(BTree &BT){
queue<node*> Q;
Q.push(BT);
while(!Q.empty()){
node* t = Q.front();
Q.pop();
vc.push_back(t->data);
if(t->lchild != NULL){
Q.push(t->lchild);
if(after) flag = false;
}
else after = true;
if(t->rchild != NULL){
Q.push(t->rchild);
if(after) flag = false;
}
else after = true;
}
}
int main(){
int n, data[21];
cin>>n;
for(int i = 0; i < n; i++)
cin>>data[i];
BTree BT = create(data, n);
levelorder(BT);
for(int i = 0; i < n; i++){
if(i == n - 1) cout<<vc[i]<<endl;
else cout<<vc[i]<<" ";
}
if(flag) cout<<"YES";
else cout<<"NO";
return 0;
}