1123. Is It a Complete AVL Tree (30)解题报告

  1. AVL插入操作、包括RR、LL、RL、LR操作。
  2. 如果一个结点有右孩子但没有左孩子,则不是完全树。
  3. 如果出现一个孩子数少于2的结点后,又出现有孩子的结点,则不是完全树。

#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <queue>
using namespace std;

struct node {
	int key, height;
	node *left, *right;
};
inline int getheight(node *root) {
	if (root) {
		return root->height;
	}
	else {
		return -1;
	}
}
inline int max(int h1, int h2) {
	return h1 > h2 ? h1 : h2;
}

node *LL(node *root) {
	node *a, *b, *c;
	a = root;
	b = root->left;
	c = b->left;
	a->left = b->right;
	b->right = a;
	if (a) {
		a->height = max(getheight(a->left), getheight(a->right)) + 1;
	}
	if (c) {
		c->height = max(getheight(c->left), getheight(c->right)) + 1;
	}
	return b;
}

node *RR(node *root) {
	node *a, *b, *c;
	a = root;
	b = a->right;
	c = b->right;
	a->right = b->left;
	b->left = a;
	if (a) {
		a->height = max(getheight(a->left), getheight(a->right)) + 1;
	}
	if (c) {
		c->height = max(getheight(c->left), getheight(c->right)) + 1;
	}
	return b;
}

node *LR(node *root) {
	root->left = RR(root->left);
	return LL(root);
}

node *RL(node *root) {
	root->right = LL(root->right);
	return RR(root);
}

node *insert(node *root, int key) {
	if (!root) {
		root = new node;
		root->key = key;
		root->height = 0;
		root->left = root->right = nullptr;
	}
	else {
		if (root->key > key) {
			root->left = insert(root->left, key);
		}
		else {
			root->right = insert(root->right, key);
		}
		int h1, h2;
		h1 = getheight(root->left);
		h2 = getheight(root->right);
		if (h1 > h2 + 1) {
			int h3, h4;
			h3 = getheight(root->left->left);
			h4 = getheight(root->left->right);
			if (h3 > h4) {
				root = LL(root);
			}
			else {
				root = LR(root);
			}
		}
		else if(h1 + 1 < h2) {
			int h3, h4;
			h3 = getheight(root->right->left);
			h4 = getheight(root->right->right);
			if (h4 > h3) {
				root = RR(root);
			}
			else {
				root = RL(root);
			}
		}
		root->height = max(getheight(root->left), getheight(root->right)) + 1;
	}
	return root;
}

bool levelorder(node *root) {
	queue<node *> q;
	node *tmp;
	bool leave = false, complete = true;
	q.push(root);
	printf("%d", root->key);
	while (!q.empty()) {
		tmp = q.front();
		q.pop();
		if (tmp->left) {
			q.push(tmp->left);
			printf(" %d", tmp->left->key);
		}
		if (tmp->right) {
			q.push(tmp->right);
			printf(" %d", tmp->right->key);
		}
		if (!tmp->left && tmp->right) {
			complete = false;
		}
		if (leave && (tmp->right || tmp->left)) {
			complete = false;
		}
		if (!tmp->right || !tmp->left) {
			leave = true;
		}
	}
	putchar('\n');
	return complete;
}

int main(void)
{
	int n, i, tmp;
	node *root = nullptr;
	scanf("%d", &n);
	for (i = 0; i < n; i++) {
		scanf("%d", &tmp);
		root = insert(root, tmp);
	}
	bool complete;
	complete = levelorder(root);
	if (complete) {
		puts("YES");
	}
	else {
		puts("NO");
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值