栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

PAT (Advanced Level) 1123 Is It a Complete AVL Tree——平衡二叉树+完全二叉树判断

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PAT (Advanced Level) 1123 Is It a Complete AVL Tree——平衡二叉树+完全二叉树判断

题目传送门

之前我们写过完全二叉树的判断
实际上还有一个更简单的判断方法(这也是完全二叉树的一个性质)
判断一棵树是否为完全二叉树,可以给每个节点编号
例如某个节点编号为id,它左儿子为2id,右儿子为2id+1
如果最大的编号为n,即为完全二叉树

平衡二叉树的板子,要好好背过啊

#include
#include
#include
#include

using namespace std;

const int N = 25;
struct node {
	int data;
	node* lc;
	node* rc;
	int height;
	node(int x) {
		data = x;
		height = 1;
		lc = rc = NULL;
	}
};

//获取树的高度
int getTreeHeight(node* root) {
	if (root == NULL) return 0;
	else return root->height;
}

//更新树的高度
void updateTreeHeight(node* root) {
	root->height = max(getTreeHeight(root->lc), getTreeHeight(root->rc)) + 1;
}

//获取平衡因子
int getBalanceFactor(node* root) {
	return getTreeHeight(root->lc) - getTreeHeight(root->rc);
}

void leftRotate(node* &root) {
	node* tmp = root->rc;
	root->rc = tmp->lc;
	tmp->lc = root;
	updateTreeHeight(root);
	updateTreeHeight(tmp);
	root = tmp;
}

void rightRotate(node* &root) {
	node* tmp = root->lc;
	root->lc = tmp->rc;
	tmp->rc = root;
	updateTreeHeight(root);
	updateTreeHeight(tmp);
	root = tmp;
}

//插入新节点
//因为有new操作所以root需要传引用
void insert(node* &root, int x) {
	if (root == NULL) {
		root = new node(x);
		return;
	}
	if (x < root->data) {
		insert(root->lc, x);
		//更新高度
		updateTreeHeight(root);
		if (getBalanceFactor(root) == 2) {  //左子树插入
			if (x < root->lc->data) {  //LL
				rightRotate(root);
			}
			else {  //LR
				leftRotate(root->lc);
				rightRotate(root);
			}
		}
	}
	else {
		insert(root->rc, x);
		updateTreeHeight(root);
		if (getBalanceFactor(root) == -2) {  //右子树插入
			if (x > root->rc->data) {  //RR
				leftRotate(root);
			}
			else {  //RL
				rightRotate(root->rc);
				leftRotate(root);
			}
		}
	}
}

node* Q[N * 2];
int level[N];
void printLevel(node* root) {
	if (root == NULL) return;
	int tou = 0;
	int wei = 0;
	int flag = 0;
	Q[++wei] = root;
	while (tou < wei) {
		node* tmp = Q[++tou];
		if (!flag) {
			printf("%d", tmp->data);
			flag = 1;
		}
		else printf(" %d", tmp->data);
		if (tmp->lc) Q[++wei] = tmp->lc;
		if (tmp->rc) Q[++wei] = tmp->rc;
	}
	printf("n");
}

int maxid = 0;
void solve(node* root, int id) {
	maxid = max(maxid, id);
	if (root->lc) solve(root->lc, id * 2);
	if (root->rc) solve(root->rc, id * 2 + 1);
}

int checkCompleteTree(node* root, int n) {
	solve(root, 1);
	if (maxid != n) return 0;
	else return 1;
}

int main()
{
	int n;
	scanf("%d",&n);
	node* root = NULL;
	for (int i = 0; i < n; ++i) {
		int x;
		scanf("%d", &x);
		insert(root, x);
	}
	if (n == 0) {
		printf("NO");
	}
	else {
		printLevel(root);
		if (checkCompleteTree(root, n)) printf("YES");
		else printf("NO");
	}
	return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/917035.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号