1123 Is It a Complete AVL Tree(30 分)(cj)

本文介绍AVL树的基本概念及其平衡调整方法,并通过一个示例程序演示如何实现AVL树的插入操作及判断其是否为完全二叉树。

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

新知识:AVL树用数组来实现比较困难,因为涉及整个树的数据转移,会占用不必要内存时间,难度也会很大。

树的平衡在每一次插入后进行平衡。全部插入完后进行平衡虽然也能得到AVL树,但是元素的位置是不好预测的,结果也可能会与测试数据不同。 

数组处理完全二叉树真的好用。

code

#pragma warning(disable:4996)
#include <iostream>
#include <algorithm>
using namespace std;
class avltree {
public:
	int val;
	avltree* left = NULL, *right=NULL;
	int getheight(avltree* t) {
		if (t==NULL) return 0;
		return max(getheight(t->left), getheight(t->right)) + 1;
	}
	int getbf(avltree* t) {
		int f = getheight(t->left) - getheight(t->right);
		return f;
	}
	void doll(avltree*& t) {
		avltree* pa = t->left, *p = t;
		p->left = pa->right;
		pa->right = p;
		t = pa;
	}
	void dorr(avltree*& t) {
		avltree * pa = t->right, *p = t;
		p->right = pa->left;
		pa->left = p;
		t = pa;
	}
	void dolr(avltree*& t) {
		avltree *p = t, *pa = t->left, *pb = t->left->right;
		pa->right = pb->left;
		pb->left = pa;
		p->left = pb->right;
		pb->right = p;
		t = pb;
	}
	void dorl(avltree*& t) {
		avltree* p = t, *pa = t->right, *pb = t->right->left;
		pa->left = pb->right;
		pb->right = pa;
		p->right = pb->left;
		pb->left = p;
		t = pb;
	}
	void balance(avltree*& t) {
		if (t == NULL) return;
		balance(t->left);
		balance(t->right);
		if (getbf(t) > 1) {
			if (getbf(t->left) > 0) doll(t);
			else dolr(t);
		}
		else if (getbf(t) < -1) {
			if (getbf(t->right) < 0) dorr(t);
			else dorl(t);
		}
	}
};
void avlinsert(avltree*& t, int x) {
	if (t == NULL) {
		t = new avltree;
		t->val = x;
		return;
	}
	if (x > t->val) {
		avlinsert(t->right, x);
	}
	else avlinsert(t->left, x);
}
int treearr[100];
void print(avltree* t,int pos) {
	if (t == NULL) return;
	treearr[pos] = t->val;
	print(t->left, pos * 2);
	print(t->right, pos * 2 + 1);
}
int main() {
	int n,x;
	cin >> n;
	avltree* head = NULL;
	for (int i = 0; i < n; ++i) {
		cin >> x;
		avlinsert(head, x);
		head->balance(head);
	}
	print(head, 1);
	bool f = 1, ff = 1;
	for (int i = 1; i < 100; ++i) {
		if (i <= n && treearr[i] == 0) f = 0;
		if (treearr[i] != 0) {
			if (ff) ff = 0;
			else cout << ' ';
			cout << treearr[i];
		}
	}
	cout << endl;
	if (f) cout << "YES" << endl;
	else cout << "NO" << endl;
	system("pause");
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值