1123 Is It a Complete AVL Tree (30 分)

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

根据输入的序列,建立二叉平衡树,同时层次遍历输出,同时判断是否为完全树,一道相当考验树知识的题目,为此先去看了课本,复习了下平衡树的知识,同时又在网上查了下柳神的博客,然后根据柳神的思路将整到题推了一遍,最后自己又敲了一遍。

下面是代码及相关注释

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
vector<int> v;
int flag1 = 0,flag2 = 0;
struct tree{
	int data;
	struct tree *lchild,*rchild;
};
int height(tree *root){   //获取高度 
	if(root == NULL){
		return 0;
	}
	int l = height(root->lchild);
	int r = height(root->rchild);
	return max(l,r)+1;
}
tree* ll(tree *root){  //左旋 
	tree *temp = root->rchild;
	root->rchild = temp->lchild;
	temp->lchild = root;
	return temp;
}
tree* rr(tree *root){   //右旋 
	tree *temp = root->lchild;
	root->lchild = temp->rchild;
	temp->rchild = root;
	return temp;
}
tree* lr(tree *root){  //先左旋再右旋 
	root->lchild = ll(root->lchild);
	return rr(root);
}
tree* rl(tree *root){  //先右旋再左旋 
	root->rchild = rr(root->rchild);
	return ll(root);
}
tree* create(tree *root,int a){
	if(root==NULL){
		root = new tree();
		root->data = a;
	}else if(root->data > a){
		root->lchild = create(root->lchild,a); //左插入 
		int l = height(root->lchild);
		int r = height(root->rchild);
		if(l-r >= 2){
			if(root->lchild->data > a){
				root = rr(root); //左子树的左子树不平衡,右旋 
			}else{
				root = lr(root); //左子树的右子树不平衡,先左旋再右旋 
			}
		}
	}else{                                //右插入 
		root->rchild = create(root->rchild,a); 
		int l = height(root->lchild);
		int r = height(root->rchild);
		if(r-l >= 2){
			if(root->rchild->data < a){  //右子树的右子树不平衡,左旋 
				root= ll(root);
			}else{
				root = rl(root);      //右子树的左子树不平衡,右旋再左旋 
			}
		}
	}
	return root;
}
void level(tree *root){ //层次存储节点 
	queue<tree*> q;
	q.push(root);
	while(!q.empty()){
		tree *temp = q.front();
		q.pop();
		v.push_back(temp->data);
		if(temp->lchild!=NULL){
			if(flag1==1) flag2 = 1;  //两次判断,flag1标记是否有空的节点,
			                         //当有空的节点标记flag1为1,然后如果后面有不为空的节点
									 //此时标记flag2为1,这样是为了判断此树是否为完全数 
			q.push(temp->lchild);
		}else{
			flag1 = 1;
		}
		if(temp->rchild!=NULL){
			if(flag1==1) flag2 = 1;
			q.push(temp->rchild);
		}else{
			flag1=1;
		}
	}
}
int main(){
	int n;
	struct tree *root=NULL;
	cin >> n;
	for(int i = 0;i < n;i++){  //建立平衡二叉树 
		int a;
		cin >> a;
		root = create(root,a);
	}
	//cout << "   dfasfd" << endl;
	level(root);
	//cout << "  dsfas" << endl;
	for(int i = 0;i < v.size();i++){
		if(i == 0){
			cout << v[i];
		}else{
			cout << " " << v[i];
		}
	}
	cout << endl;
	if(flag2==1){
		cout << "NO" << endl;
	}else{
		cout << "YES" << endl;
	}
	return 0;
	
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值