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.
![](https://www.patest.cn/upload/mr_ohesevgnyny.jpg)
![](https://www.patest.cn/upload/mr_ohesfdbkaep.jpg)
![](https://www.patest.cn/upload/mr_oheseymzi13.jpg)
![](https://www.patest.cn/upload/mr_ohesfjddti2.jpg)
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 65Sample Output 1:
70 63 88 61 65 YESSample Input 2:
8 88 70 61 96 120 90 65 68Sample Output 2:
88 65 96 61 70 90 120 68 NO
#include <cstdio>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
const int Max = 40;
int n;
struct node{
int data,height;
node *lchild,*rchild;
}*root;
node * creat(int x)
{
node* p= new node;
p->data=x;
p->height=1;
p->lchild=p->rchild=NULL;
return p;
}
int getHeight(node* &root)
{
if(root==NULL) return 0;
return root->height;
}
void getBalance(node* &root)
{
root->height = max(getHeight(root->lchild),getHeight(root->rchild))+1;
}
int getSet(node* &root)
{
return getHeight(root->lchild)-getHeight(root->rchild);
}
void L (node* &root)
{
node *k = root->rchild;
root->rchild=k->lchild;
k->lchild=root;
getBalance(root);
getBalance(k);
root=k;
}
void R(node* &root)
{
node* k=root->lchild;
root->lchild=k->rchild;
k->rchild=root;
getBalance(root);
getBalance(k);
root=k;
}
void Insert(node* &root,int x)
{
if(root==NULL)
{
root=creat(x);
return ;
}
if(root->data>x)
{
Insert(root->lchild,x);
getBalance(root);
if(getSet(root)==2)
{
if(getSet(root->lchild)==1)
{
R(root);
}
else if(getSet(root->lchild)==-1)
{
L(root->lchild);
R(root);
}
}
}
else
{
Insert(root->rchild,x);
getBalance(root);
if(getSet(root)==-2)
{
if(getSet(root->rchild)==-1)
{
L(root);
}
else if(getSet(root->rchild)==1)
{
R(root->rchild);
L(root);
}
}
}
}
int main()
{
int ans;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&ans);
Insert(root,ans);
}
queue<node*> q;
q.push(root);
int u=1;
while(!q.empty())
{
node* p = new node;
p=q.front();
q.pop();
if(p->lchild==NULL&&p->rchild!=NULL ) u=0;
if(getSet(p)<0) u=0;
printf("%d",p->data);
if(p->lchild!=NULL) q.push(p->lchild);
if(p->rchild!=NULL) q.push(p->rchild);
if(!q.empty())
{
printf(" ");
}
}
printf("\n%s\n",u?"YES":"NO");
system("pause");
return 0;
}