PAT甲级1123 【Is It a Complete AVL Tree】(30)

2 篇文章 0 订阅

补充:2018.8.27更新,再写一遍居然缩减了十几行代码量哈哈。

#include<iostream>
#include<cstdio>
#include<queue> 
#include<vector>
using namespace std;
struct Node{
	int v, lev, id;
	struct Node *l, *r;
};
vector<int> lev;
int vis[31];
int getH(Node *root){
	if(!root) return 0;
	return max(getH(root->l), getH(root->r))+1;
}
int getBF(Node *root){
	return getH(root->l)-getH(root->r);
}
void R(Node* &root){
	Node *tem = root->l;
	root->l = tem->r;
	tem->r = root;
	root = tem;
}
void L(Node* &root){
	Node *tem = root->r;
	root->r = tem->l;
	tem->l = root;
	root = tem;
}
Node* insert(Node *root, int v){
	if(!root){
		root = new Node();
		root->v = v;
		root->l = root->r = NULL;
	} else {
		if(v > root->v){
			root->r = insert(root->r, v);
			if(getBF(root) == -2){
				if(getBF(root->r) == -1){
					L(root);
				} else if(getBF(root->r) == 1){
					R(root->r);
					L(root);
				}
			}
		} else {
			root->l = insert(root->l, v);
			if(getBF(root) == 2){
				if(getBF(root->l) == 1){
					R(root);
				} else if(getBF(root->l) == -1){
					L(root->l);
					R(root);
				}
			}
		}
	}
	return root;
} 
void bfs(Node *root, int n){
	queue<Node *> q;
	root->lev = 1;
	q.push(root);
	while(!q.empty()){
		Node *top = q.front();
		q.pop();
		lev.push_back(top->v);
		vis[top->lev] = 1;
		if(top->l) {
			top->l->lev = top->lev*2;
			q.push(top->l);
		}
		if(top->r) {
			top->r->lev = top->lev*2+1;
			q.push(top->r);
		}
	}
	for(int i = 0; i<lev.size(); i++){
		cout << lev[i];
		i!=lev.size()-1 ? cout << " " : cout << "\n";
	}
	for(int i = 1; i<=n; i++){
		if(!vis[i]){
			cout << "NO\n";
			return ;
		}
	}
	cout << "YES\n";
}
int main(){
	//freopen("aa.txt", "r", stdin);
	int n, tem;
	Node *root = NULL;
	cin >> n;
	for(int i = 0; i<n; i++){
		cin >> tem;
		root = insert(root, tem);
	}
	bfs(root, n);
	return 0;
}

分析:AVL常规插入调整操作,至于判断是否完全二叉树,我在bfs过程中采用了给每个节点编号的方式,最后判断中间是否有节点没有vis,也可以参考柳神的做法:在出现了一个孩子为空的结点之后是否还会出现孩子结点不为空的结点,如果出现了就不是完全二叉树。

#include<stdio.h>
#include<queue> 
#include<math.h>
#include<algorithm>
using namespace std;

typedef struct Tree{
	int val, height, num; //值、高度和编号 
	struct Tree *l, *r;
}Tree;

int vis[30];
int getH(Tree *root){
	if(root == NULL) return 0;
	return root->height;
}

void updateH(Tree *root){
	root->height = max(getH(root->l), getH(root->r))+1;
}

int getBalanceFactor(Tree *root){
	return getH(root->l)-getH(root->r);
}
void R(Tree* &root){
	//右旋 
	Tree *tem = root->l;
	root->l = tem->r;
	tem->r = root;
	updateH(root);
	updateH(tem);
	root = tem;
}
void L(Tree* &root){
	//左旋
	Tree *tem = root->r;
	root->r = tem->l;
	tem->l = root;
	updateH(root);
	updateH(tem);
	root = tem; 
}
void insert(Tree* &root, int val){
	if(root == NULL){
		root = (Tree *)malloc(sizeof(Tree));
		root->val = val;
		root->height = 1;
		root->l = root->r = NULL;
		return ;
	}
	if(root->val > val){
		insert(root->l, val);
		updateH(root);
		if(getBalanceFactor(root) == 2){
			if(getBalanceFactor(root->l) == 1)
				R(root);
			else if(getBalanceFactor(root->l) == -1){
				L(root->l); //先左转root的左子树 
				R(root);
			}
		}
	}
	else {
		insert(root->r, val);
		updateH(root);
		if(getBalanceFactor(root) == -2){
			if(getBalanceFactor(root->r) == -1)
				L(root);
			else if(getBalanceFactor(root->r) == 1){
				R(root->r); //先右旋root的右子树 
				L(root);
			}
		}
	}
}

void bfs(Tree *root, int n){
	int cnt = 0, flag = 1, curnum = 0; //curnum为当前结点的编号 
	queue<Tree *> q;
	root->num = 0;
	q.push(root);
	while(!q.empty()){
		Tree *cur = q.front();
		q.pop();
		curnum = cur->num;
		vis[curnum] = 1;
		printf("%d", cur->val);
		cnt++;
		printf(cnt == n ? "\n" : " ");
		if(cur->l) {
			cur->l->num = 2*curnum + 1;
			q.push(cur->l);
		}
		if(cur->r) {
			cur->r->num = 2*curnum+2;
			q.push(cur->r);
		}
	}
	for(int i = 0; i<=curnum; i++)
		if(!vis[i]) flag = 0; //中间有结点没vis则非完全二叉树 
	if(flag) printf("YES\n");
	else printf("NO\n");
} 
int main(){
	//freopen("in.txt", "r", stdin);
	int n, i, val;
	Tree *root = NULL;
	scanf("%d", &n);
	for(i = 0; i<n; i++){
		scanf("%d", &val);
		insert(root, val);
	}
	bfs(root, n);
	
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值