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

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.jpg F2.jpg
F3.jpg F4.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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章