1123 Is It a Complete AVL Tree (30 分)【AVL树建立和层序遍历bfs】

本文介绍如何构建AVL自平衡二叉搜索树,并实现插入操作后的旋转调整。通过示例展示了层序遍历的过程及如何判断最终的AVL树是否为完全二叉树。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.jpgF2.jpg
F3.jpgF4.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

题意:给出n和n个数,建立AVL树并且输出层序遍历,在最后时还要判断是否是完全二叉树

解题思路:建AVL树套模板即可,层序遍历用bfs,在bfs中判断是否是完全二叉树,用了2个变量,isComplete和after,如果左孩子或者右孩子有一个为空,就将after设置为1,在有左右孩子的时候判断after为1还是0,如果是1,说明之前有一个为空,就将isComplete赋值为0,表示不是完全二叉树

#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
struct node{
	int val;
	struct node *left,*right;
}; 
node *rotateLeft(node *root)
{
	node *t=root->right;
	root->right=t->left;
	t->left=root;
	return t;
}
node *rotateRight(node *root)
{
	node *t=root->left;
	root->left=t->right;
	t->right=root;
	return t;
}
node *rotateLeftRight(node *root)
{
	root->left=rotateLeft(root->left);
	return rotateRight(root);
}
node *rotateRightLeft(node *root)
{
	root->right=rotateRight(root->right);
	return rotateLeft(root);
}
int getHeight(node *root)
{
	if(root==NULL) return 0;
	return max(getHeight(root->left),getHeight(root->right))+1;
}
node *insert(node *root,int val)
{
	if(root==NULL)
	{
		root=new node();
		root->val=val;
		root->left=root->right=NULL;
	}else if(val<root->val)
	{
		root->left=insert(root->left,val);
		if(getHeight(root->left)-getHeight(root->right)==2)
		root=val<root->left->val? rotateRight(root):rotateLeftRight(root);
	}else{
		root->right=insert(root->right,val);
		if(getHeight(root->right)-getHeight(root->left)==2)
		root=val>root->right->val?rotateLeft(root):rotateRightLeft(root);
	}
	return root;
}
int isComplete = 1, after = 0;
vector<int> levelOrder(node *root)
{
	vector<int> v;
	queue<node*>q;
	q.push(root);
	while(!q.empty())
	{
	    node *temp=q.front();
		q.pop();
		v.push_back(temp->val);
		if(temp->left!=NULL)
		{
		    if(after) isComplete=0;
			q.push(temp->left);	
		}else after=1;
		if(temp->right!=NULL)
		{
	        if(after) isComplete=0;
			q.push(temp->right);		
		}else after=1;
	} 
	return v;
}
int main(void)
{
	int n,val;
	scanf("%d",&n);
	node *root=NULL;
	for(int i=0;i<n;i++)
	{
		scanf("%d",&val);
		root=insert(root,val);
	}
//	printf("%d",root->val);
	vector<int> v=levelOrder(root);
	for(int i=0;i<v.size();i++)
	{
	    printf("%d",v[i]);	
	    if(i!=v.size()-1) printf(" ");
	}
	printf("\n%s",isComplete==1?"YES":"NO");
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值