【PAT】1123. Is It a Complete AVL Tree (30)【模拟】

题目描述

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.
图片1图片2
图片3 在这里插入图片描述
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.

翻译:AVL树是一种自平衡的二叉搜索树。在AVL树中,任何节点的两个子树的高度最多相差一;如果在任何时候它们的差异超过一,就需要重新平衡以恢复此属性。图1-4展示了旋转规则。现在给定一个插入序列,您应该输出得到的AVL树的层次顺序遍历序列,并判断它是否是一个完全二叉树。

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.

翻译:每个输入文件包含一组测试数据。对于每组测试数据,第一行包含一个正整数N (≤20)。在下一行中给出N个不同的整数键值。一行内的所有数字都用空格隔开。

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.

翻译:对于每组测试数据,将键值一个个插入空初始化AVL树。然后首先在一行中输出生成的AVL树的层次顺序遍历序列。一行内所有数字必须用空格隔开,并且行尾不得有多余空格。然后在下一行中,如果是完全二叉树,则输出YES;如果不是,则输出NO。


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


解题思路

这道题需要用到一个平衡二叉树模板,详见平衡二叉树详解。然后就是对生成的平衡二叉树进行层次遍历,如果某节点的位置计算出大于N,则说明不是完全二叉树。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<queue>
using namespace std;
int N;
int p[10][1024];
struct node{
	int data;
	int height;
	node *lc;
	node *rc;
	node(int x):data(x),height(0),lc(NULL),rc(NULL){}
}; 
int GetHeight(node* &a){
	if(a==NULL) return -1;
	else return a->height;
}
void LL(node* &root){
	node *a;
	a=root->lc;
	root->lc=a->rc;
	a->rc=root;
	a->height=max(GetHeight(a->lc),GetHeight(a->rc))+1;
	root->height=max(GetHeight(root->lc),GetHeight(root->rc))+1;
	root=a; 
}
void RR(node* &root){
	node *a;
	a=root->rc;
	root->rc=a->lc;
	a->lc=root;
	a->height=max(GetHeight(a->lc),GetHeight(a->rc))+1;
	root->height=max(GetHeight(root->lc),GetHeight(root->rc))+1;
	root=a; 
}
void LR(node* &root){
	RR(root->lc);
	LL(root);
}
void RL(node* &root){
	LL(root->rc);
	RR(root);
}
int flag=0,Complete=0;
typedef pair<node*,int>P;
queue<P>q;
void Print(node* root){
	q.push(P(root,1));
	while(!q.empty()){
		P p=q.front();q.pop();
		node *tmp=p.first;
		int num=p.second;
		if(num>N)Complete=1;
		if(!flag)printf("%d",tmp->data),flag=1;
		else printf(" %d",tmp->data);
		if(tmp->lc!=NULL)q.push(P(tmp->lc,num*2));
		if(tmp->rc!=NULL)q.push(P(tmp->rc,num*2+1));
		
	} 
	printf("\n");
}
void Insert(node* &root,int val){
	if(root==NULL){
		root=new node(val);
		return ;
	}
	if(val<root->data){
		Insert(root->lc,val);
		if(GetHeight(root->lc)-GetHeight(root->rc)>1){
			if(val<root->lc->data) LL(root);
			else LR(root);
		}
	}
	if(val>root->data){
		Insert(root->rc,val);
		if(GetHeight(root->rc)-GetHeight(root->lc)>1){
			if(val>root->rc->data) RR(root);
			else RL(root);
		}
	}
	root->height = max(GetHeight(root->lc), GetHeight(root->rc)) + 1;
}
int main() {
	scanf("%d",&N);
	int a;
	node *root=NULL;
	for(int i=0;i<N;i++){
		scanf("%d",&a);
		Insert(root,a);
	}
	Print(root);
	if(Complete==1)printf("NO\n");
	else printf("YES\n");
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值