pat 甲级 树 A1123 Is It a Complete AVL Tree (30分)完全+平衡二叉树+层序遍历

该博客介绍了如何处理PAT甲级考试中的一道题目,涉及构建AVL树并进行层序遍历。通过输入一组整数,构建AVL树后,输出树的层序遍历序列,并判断该AVL树是否为完全二叉树。内容包含了题目的输入输出规格以及样例数据。
摘要由CSDN通过智能技术生成

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.

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

平衡二叉搜索树(英语:Balanced Binary Tree)介绍:https://www.cnblogs.com/john1015/p/13204615.html

题意:构造avl树(自平衡二叉搜索树),输出它的层序序列,并判断它是否是一颗完全二叉树

#include<iostream>
#include<queue>
#include<vector>
using namespace std;
typedef struct Node* node;
struct Node{
	int val;
	node left;
	node right;
};
node ll(node root){
	node temp=root->right ;
	root->right =temp->left ;
	temp->left=root;
	return temp;
}
node rr(node root){
	node temp=root->left ;
	root->left=temp->right;
	temp->right=root;
	return temp;
}
node lr(node root){
	root->left=ll(root->left);
	return rr(root);
}
node rl(node root){
	root->right=rr(root->right); 
	return ll(root);
}
int getHeight(node tree){
	if(tree==NULL) return 0;
	int l=getHeight(tree->left);
	int r=getHeight(tree->right);
	return max(l,r)+1;
}
node insert(node tree,int val){
	if(tree==NULL){
		tree=new Node();
		tree->val=val;
		tree->left=tree->right=NULL;
	}else if(val<tree->val){
		tree->left=insert(tree->left,val);
		int l=getHeight(tree->left),r=getHeight(tree->right);
		if(l-r>=2){
			if(val<tree->left->val) tree=rr(tree);
			else tree=lr(tree);
		}
	}else{
		tree->right=insert(tree->right,val);
		int l=getHeight(tree->left),r=getHeight(tree->right);
		if(r-l>=2){
			if(val>tree->right->val) tree=ll(tree);
			else tree=rl(tree);
		}
	}
    return tree;
}
int after=0,isComplete=1;
vector<int> v;
void levelOrder(node root){
	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;
	}
}
int main(){
	int n,va;
	scanf("%d",&n);
	node tree=NULL;
	for(int i=0;i<n;i++){
		scanf("%d",&va);
		tree=insert(tree,va);
	}
    levelOrder(tree);
	for(int i=0;i<v.size();i++){
		printf("%d",v[i]);
		if(i!=v.size()-1) printf(" ");
		else printf("\n");
	}
	if(isComplete==1) printf("YES\n");
	else printf("NO\n");
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值