PAT 甲级 1123 Is It a Complete AVL Tree

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a845717607/article/details/87901794

1123 Is It a Complete AVL Tree (30 point(s))

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.

F1.jpg F2.jpg
F3.jpg F4.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 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 <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int maxn=30;
int n,level[maxn],maxnum=-1,num=0,t;
struct node
{
	int data,height,no;
	node * lchild,* rchild;
};
int getHeight(node * root)
{
	if(root==NULL)
		return 0;
	return root->height;
}
void update(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;
	update(root);
	update(temp);
	root=temp;
}
void R(node * &root)
{
	node * temp=root->lchild;
	root->lchild=temp->rchild;
	temp->rchild=root;
	update(root);
	update(temp);
	root=temp;
}
int getBalance(node * root)
{
	return getHeight(root->lchild)-getHeight(root->rchild);
}
void insert(node * &root,int data)
{
	if(root==NULL)
	{
		root=new node();
		root->rchild=root->lchild=NULL;
		root->data=data;
		root->height=1;
		return ;
	}
	if(data<=root->data)
	{
		insert(root->lchild,data);
		update(root);
		if(getBalance(root)==2)
		{
			if(getBalance(root->lchild)==1)
				R(root);
			else if(getBalance(root->lchild)==-1)
			{
				L(root->lchild);
				R(root);
			}
		}
	}
	else
	{
		insert(root->rchild,data);
		update(root);
		if(getBalance(root)==-2)
		{
			if(getBalance(root->rchild)==-1)
				L(root);
			else if(getBalance(root->rchild)==1)
			{
				R(root->rchild);
				L(root);
			}
		}
	}
}
void BFS(node * root)
{
	queue<node *> q;
	root->no=1;
	maxnum=maxnum>root->no?maxnum:root->no;
	q.push(root);
	while(q.size())
	{
		node *x=q.front();
		level[num++]=x->data;
		q.pop();
		if(x->lchild!=NULL)
		{
			int lev=x->no*2;
			x->lchild->no=lev;
			maxnum=maxnum>lev?maxnum:lev;
			q.push(x->lchild);
		}
		if(x->rchild!=NULL)
		{
			int lev=x->no*2+1;
			x->rchild->no=lev;
			maxnum=maxnum>lev?maxnum:lev;
			q.push(x->rchild);
		}
	}
}
int main()
{
	scanf("%d",&n);
	node *root=NULL;
	for(int i=0;i<n;++i)
	{
		scanf("%d",&t);
		insert(root,t);
	}
	BFS(root);
	for(int i=0;i<n;++i)
		printf("%d%c",level[i],i<n-1?' ':'\n');
	printf("%s",maxnum==n?"YES\n":"NO\n");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/a845717607/article/details/87901794