PAT (Advanced Level) 1123 Is It a Complete AVL Tree

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

Solution:

#include <bits/stdc++.h>
using namespace std;

const int maxn = 100;
int cnt, root;
struct node { int l, r, v, h; } avl[maxn];
vector<int> v;

inline const int read()
{
    int x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + ch - '0'; ch = getchar(); }
    return x * f;
}

inline void newNode(int& now, int val)
{
    avl[now = ++cnt].v = val;
}

inline void update(int& now)
{
    avl[now].h = max(avl[avl[now].l].h, avl[avl[now].r].h) + 1;
}

inline int factor(int now)
{
    return avl[avl[now].l].h - avl[avl[now].r].h;
}

inline void lrotate(int& now)
{
    int r = avl[now].r;
    avl[now].r = avl[r].l;
    avl[r].l = now;
    now = r;
    update(avl[now].l);
    update(now);
}

inline void rrotate(int& now)
{
    int l = avl[now].l;
    avl[now].l = avl[l].r;
    avl[l].r = now;
    now = l;
    update(avl[now].r);
    update(now);
}

inline void check(int& now)
{
    int nf = factor(now);
    if (nf > 1)
    {
        int lf = factor(avl[now].l);
        if (lf > 0) rrotate(now);
        else
        {
            lrotate(avl[now].l);
            rrotate(now);
        }
    }
    else if (nf < -1)
    {
        int rf = factor(avl[now].r);
        if (rf < 0) lrotate(now);
        else
        {
            rrotate(avl[now].r);
            lrotate(now);
        }
    }
    else if (now) update(now);
}

void ins(int& now, int val)
{
    if (!now) newNode(now, val);
    else if (val < avl[now].v) ins(avl[now].l, val);
    else ins(avl[now].r, val);
    check(now);
}

void traversal(int root)
{
    queue<int> q;
    q.push(root);
    while (!q.empty())
    {
        int now = q.front();
        q.pop();
        v.push_back(now);
        if (!now) continue;
        q.push(avl[now].l);
        q.push(avl[now].r);
    }
}

int main()
{
    int n = read();
    bool flag1 = false, flag2 = false;
    for (int i = 0; i < n; i++) ins(root, read());
    traversal(root);
    for (int i = 0; i < (int)v.size(); i++)
    {
        if (avl[v[i]].v)
        {
            printf("%s%d", i == 0 ? "" : " ", avl[v[i]].v);
            if (flag1) flag2 = true;
        }
        else flag1 = true;
    }
    printf("\n%s\n", flag2 ? "NO" : "YES");
    return 0;
}
发布了367 篇原创文章 · 获赞 148 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_35850147/article/details/103429965