1123 Is It a Complete AVL Tree (30 分)

知识点: AVL平衡二叉树

版本2

版本1是个很好的思路,但是那是从别人那里借鉴的。利用节点的变量来保存树的索引信息,同时层序遍历得到的结果保存的是节点指针而不是值。这样直接插最后的节点的索引是否是总节点的个数,就能判断是否是完全二叉树。
版本2是根据空节点出现的位置来判断是否是完全二叉树。如果是完全二叉树,则空节点一定是在所有节点输出完成才能得到。

#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
int n;
struct tree{
	int val, height;
	tree* left, * right;
	tree(int x){
		val = x;
		height = 1;
		left = right = nullptr;
	}
};
int getHeight(tree* root){
	if(!root) return 0;
 	return root->height;
}
void updateHeight(tree* root){
	root->height = max(getHeight(root->left), getHeight(root->right)) + 1;
}
void L(tree* &root){ //左旋  传引用 
	tree* temp = root->right;
	root->right = temp->left;
	temp->left = root;
	updateHeight(root);
	updateHeight(temp);
	root = temp;
}

void R(tree* &root){ //右旋 
	tree* temp = root->left;
	root->left = temp->right;
	temp->right = root;
	updateHeight(root);
	updateHeight(temp);
	root = temp;
}
int getBF(tree* root){
	return getHeight(root->left) - getHeight(root->right);
}
void insert(tree*& root, int x){
	if(!root) root = new tree(x);
	else if(root->val < x){ //插入右子树 
		insert(root->right, x);
		updateHeight(root);
		if(getBF(root) == -2){
			if(getBF(root->right) == -1){
				L(root);
			}else if(getBF(root->right) == 1){
				R(root->right);
				L(root);
			}
		} 
	}else{
		insert(root->left, x);
		updateHeight(root);
		if(getBF(root) == 2){
			if(getBF(root->left) == 1){
				R(root);
			}else if(getBF(root->left) == -1){
				L(root->left);
				R(root);
			} 
		}
	}
}

vector<int> res;
void levelorder(tree* root, int &flag){
	queue<tree*> q;
	q.push(root);
	int cnt = 0;
	while(q.size()){
		tree* t = q.front();
		q.pop();
		
		if(t) {
		 	cnt++;
			res.push_back(t->val);
			q.push(t->left);
			q.push(t->right);
			if(cnt == n) return;
		}else flag = false;
	}
} 
int main(){
	int x;
	scanf("%d", &n);
	tree* root = nullptr;
	for(int i = 0; i < n; i++){
		scanf("%d", &x);
		insert(root, x);
	}
	int flag = true; 
	levelorder(root, flag);
	for(int i = 0; i < res.size(); i++){
		printf("%d", res[i]);
		if(i!= res.size()-1) printf(" ");
		else printf("\n");
	}
	if(flag) puts("YES");
	else puts("NO");
	return 0;
} 

版本1

判断完全二叉树的方法:从上往下编号1~N,左孩子是父节点的2倍,右孩子是父节点的2倍加1,按照层序放入vector中,如果最后一个元素的索引不为节点的总数n,则说明这棵树不是完全二叉树。

#include<bits/stdc++.h>
using namespace std;
struct node{
	node* left;
	node* right;
	int data,height,index;
};
int getHeight(node* root){
	if(root == NULL) return 0;
	return root->height; 
}
int getBalanceFactor(node* root){
	return getHeight(root->left) - getHeight(root->right);
}
void updateHeight(node* root){
	root->height = max(getHeight(root->left), getHeight(root->right)) + 1;
}
void L(node* &root){ //左旋 
	node* tmp = root->right;
	root->right =  tmp->left;
	tmp->left = root;
	updateHeight(root);
	updateHeight(tmp);
	root = tmp;
}
void R(node* &root){ //左旋 
	node* tmp = root->left;
	root->left =  tmp->right;
	tmp->right = root;
	updateHeight(root);
	updateHeight(tmp);
	root = tmp;
}
void insert(node* &r, int v){
	if(r == NULL){
		r = new node;
		r->data = v;
		r->height = 1;
		r->left = r->right = NULL;
	}else if(v < r->data){ //左子树 
		insert(r->left,v);
		updateHeight(r);
		if(getBalanceFactor(r)==2){
			if(getBalanceFactor(r->left)== 1){ //LL型 
				R(r); 
			}else if(getBalanceFactor(r->left)==-1){ //LR型 
				L(r->left);
				R(r);
			}
		} 
	}else { //右子树 
		insert(r->right, v);
		updateHeight(r);
		if(getBalanceFactor(r)==-2){
			if(getBalanceFactor(r->right)==1){ //RL型 
				R(r->right);
				L(r);
			}else if(getBalanceFactor(r->right)==-1){//RR型
				L(r); 
			} 
		}
	}
}
vector<node*> v;
void levelorder(node* root){
	queue<node*> q;
	root->index = 1;
	q.push(root);
	while(!q.empty()){
		node* tmp = q.front();
		q.pop();
		v.push_back(tmp);
		if(tmp->left != NULL){
			q.push(tmp->left);
			tmp->left->index = 2*tmp->index;
		}
		if(tmp->right != NULL){
			q.push(tmp->right);
			tmp->right->index = 2* tmp->index + 1;
		}
	}
	
}
int main(){
	int n,num;
	cin>>n;
	node* root = NULL;
	for(int i=0;i<n;i++){
		cin>>num;
		insert(root, num);
	}	
	levelorder(root);
	for(int i=0;i<v.size();i++){
		cout<<v[i]->data;
		if(i!=v.size()-1) cout<<" ";
		else cout<<endl;
	}
	if(v[v.size()-1]->index != n) cout<<"NO"<<endl;
	else cout<<"YES"<<endl;
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值