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树,不断判断平衡因子是否<=1,若大于1则需要进行调整,然后层次遍历判断出现了⼀个孩⼦为空的结点之后是否还会出现不为空的孩⼦结点,若出现则不是完全二叉树,否则是完全二叉树
注意
插入新结点结点默认高度为1
//1123 Is It a Complete AVL Tree (30分)
#include <iostream>
#include <queue>
using namespace std;
struct node
{
int v,height;
node *lchild,*rchild;
};
int getHeight(node *root)
{
if(root==NULL)
return 0;
return root->height;
}
int getBalanceFactor(node *root)
{
return getHeight(root->lchild)-getHeight(root->rchild);
}
void updateHeight(node *root)
{
root->height=max(getHeight(root->lchild),getHeight(root->rchild))+1;
}
void L(node* &root)
{
node *temp=root->rchild;
root->rchild=temp->lchild;
temp->lchild=root;
updateHeight(root);
updateHeight(temp);
root=temp;
}
void R(node* &root)
{
node *temp=root->lchild;
root->lchild=temp->rchild;
temp->rchild=root;
updateHeight(root);
updateHeight(temp);
root=temp;
}
void insert(node* &root,int x)
{
if(root==NULL)
{
node *newNode=new node;
newNode->v=x;
newNode->height=1;//默认高度为1
newNode->lchild=newNode->rchild=NULL;
root=newNode;
return ;
}
if(x<root->v)
{
insert(root->lchild,x);
updateHeight(root);
if(getBalanceFactor(root)==2)
{
if(getBalanceFactor(root->lchild)==1)
{
R(root);
}
else if(getBalanceFactor(root->lchild)==-1)
{
L(root->lchild);
R(root);
}
}
}
else
{
insert(root->rchild,x);
updateHeight(root);
if(getBalanceFactor(root)==-2)
{
if(getBalanceFactor(root->rchild)==-1)
{
L(root);
}
else if(getBalanceFactor(root->rchild)==1)
{
R(root->rchild);
L(root);
}
}
}
}
int isComplete = 1, after = 0;
void layerorder(node *root)
{
queue<node*> q;
q.push(root);
int flag=0;
while(!q.empty())
{
node *front=q.front();
q.pop();
flag++;
if(flag==1)
cout<<front->v;
else
cout<<" "<<front->v;
if(front->lchild!=NULL)
{
if(after)
isComplete = 0;
q.push(front->lchild);
}
else
after=1;
if(front->rchild!=NULL)
{
if(after)
isComplete = 0;
q.push(front->rchild);
}
else
after=1;
}
}
int main()
{
node *tree=NULL;
int n;
cin>>n;
for(int i=0; i<n; i++)
{
int data;
cin>>data;
insert(tree,data);
}
layerorder(tree);
printf("\n%s", isComplete ? "YES" : "NO");
return 0;
}