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树。

AC代码:

//#include <bits/stdc++.h>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <time.h>
#include <string.h>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define sdddd(x,y,z,k) scanf("%d%d%d%d", &x, &y, &z, &k)
#define sddd(x,y,z) scanf("%d%d%d", &x, &y, &z)
#define sdd(x,y) scanf("%d%d", &x, &y)
#define sd(x) scanf("%d", &x)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
//#define mp Debug(x) printf("%d\n", &x);
#define pb push_back
#define ms(x, y) memset(x, y, sizeof x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1046513837;
const int maxn = 5e4 + 50;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
//typedef vector<ll> vec;
//typedef vector<vec> mat;
template <class T>
inline bool scan_d(T &ret) {
	char c; int sgn;
	if (c = getchar(), c == EOF) return 0;
	while (c != '-' && (c<'0' || c>'9')) c = getchar();
	sgn = (c == '-') ? -1 : 1;
	ret = (c == '-') ? 0 : (c - '0');
	while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
	ret *= sgn;
	return 1;
}
struct Node {
	int val;
	int hei;
	Node *l = nullptr, *r = nullptr;
};
int height(Node *rt) {
	if (rt == nullptr) return 0;
	else {
		int lh = height(rt->l);
		int rh = height(rt->r);
		return rt->hei = max(lh, rh) + 1;
	}
}
Node *LL(Node *rt) {
	Node *nrt = rt->l;
	Node *tmp = nrt->r;
	nrt->r = rt;
	rt->l = tmp;
	return nrt;
}
Node *RR(Node *rt) {
	Node *nrt = rt->r;
	Node *tmp = nrt->l;
	nrt->l = rt;
	rt->r = tmp;
	return nrt;
}
Node *LR(Node *rt) {
	Node *nrt = rt->l->r;

	Node *tmpr = nrt->r;
	Node *tmpl = nrt->l;

	nrt->l = rt->l;
	nrt->r = rt;

	rt->l->r = tmpl;
	rt->l = tmpr;
	return nrt;
}
Node *RL(Node *rt) {
	Node *nrt = rt->r->l;

	Node *tmpr = nrt->r;
	Node *tmpl = nrt->l;

	nrt->l = rt;
	nrt->r = rt->r;

	rt->r->l = tmpr;
	rt->r = tmpl;
	return nrt;
}
Node *ist(Node *rt, Node* nrt) {
	if (rt == nullptr)
		return nrt;
	else {
		if (nrt->val > rt->val) {
			rt->r = ist(rt->r, nrt);

			if (abs(height(rt->l) - height(rt->r)) > 1) {
				if (nrt->val > rt->r->val) {
					return RR(rt);
				}
				else {
					return RL(rt);
				}
			}
			else {
				return rt;
			}
		}
		else {
			rt->l = ist(rt->l, nrt);

			if (abs(height(rt->l) - height(rt->r)) > 1) {
				if (nrt->val < rt->l->val) {
					return LL(rt);
				}
				else {
					return LR(rt);
				}
			}
			else {
				return rt;
			}
		}
	}
}
vector<int> ans;
int flag;
int n, tmp;
void levelOrder(Node *rt) {
	queue<Node *> que;
	que.push(rt);
	while (que.size()) {
		Node *p = que.front(); que.pop();
		ans.push_back(p->val);
		if (p->l)
			que.push(p->l);
		if (p->r)
			que.push(p->r);
	}
}

void judge(Node *rt) {
	queue<Node *> que;
	queue<int> ique;
	vector<int> vec(2*n+1, -1);
	que.push(rt);
    ique.push(1);
	while (que.size()) {
		Node *p = que.front(); que.pop();
		int rt = ique.front(); ique.pop();
		vec[rt] = p->val;
		if (p->l){
            que.push(p->l);
            ique.push(rt*2);
		}
		if (p->r){
            que.push(p->r);
            ique.push(rt*2+1);
		}
	}
	rep(i, 1, n){
        if(vec[i] == -1){
            flag = 0;
            break;
        }
	}
}
int main() {

	Node *root = nullptr;
	cin >> n;
	rep(i, 1, n) {
		cin >> tmp;
		Node *p = new Node;
		p->val = tmp;
		root = ist(root, p);
	}
	height(root);
	levelOrder(root);
	for (int i = 0; i < ans.size(); ++i) {
		printf("%d%c", ans[i], (ans.size() - 1 == i) ? '\n' : ' ');
	}
	flag = 1;
	judge(root);
	if (flag)
		printf("YES\n");
	else
		printf("NO\n");
	return 0;
}
/*

*/
发布了276 篇原创文章 · 获赞 21 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_40758751/article/details/103409544