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.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树和完全二叉树的判定和层序遍历。

#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
struct node {
    int data;
    node *left, *right;
    node(int d):data(d), left(NULL), right(NULL) {}
}*root = NULL;
int getHeight(node* n) {
    if(n == NULL) return 0;
    return max(getHeight(n->left), getHeight(n->right)) + 1;
}
node* singleLeftRotation(node* root) {
    node* t = root->left;
    root->left = t->right;
    t->right = root;
    return t;
}
node* singleRightRotation(node* root) {
    node* t = root->right;
    root->right = t->left;
    t->left = root;
    return t;
}
node* doubleLeftRightRotation(node* root) {
    root->left = singleRightRotation(root->left);
    return singleLeftRotation(root);
}
node* doubleRightLeftRotation(node* root) {
    root->right = singleLeftRotation(root->right);
    return singleRightRotation(root);
}
node* insert(node* root, int v) {
    if(root == NULL) root = new node(v);
    else if(v > root->data) {
        root->right = insert(root->right, v);
        if(getHeight(root->right) - getHeight(root->left) == 2) {
            if(v > root->right->data) root = singleRightRotation(root);
            else root = doubleRightLeftRotation(root);
        }
    } else {
        root->left = insert(root->left, v);
        if(getHeight(root->left) - getHeight(root->right) == 2) {
            if(v < root->left->data) root = singleLeftRotation(root);
            else root = doubleLeftRightRotation(root);
        }
    }
    return root;
}
vector<int> ans;
bool level(node* root) {
    queue<node*> que;
    que.push(root);
    bool flag = true, judge = true;
    while(!que.empty()) {
        node* tmp = que.front();
        que.pop();
        ans.push_back(tmp->data);
        if(tmp->left) que.push(tmp->left);
        if(tmp->right) que.push(tmp->right);
        if(flag) {
            if((!tmp->right && tmp->left) || (!tmp->left && !tmp->right)) flag = false;
            else if(!tmp->left && tmp->right) judge = false;
        } else if(tmp->left || tmp->right) judge = false;
    }
    return judge;
}
int main() {
    int N, val;
    scanf("%d", &N);
    for(int i = 0; i < N; i++) {
        scanf("%d", &val);
        root = insert(root, val);
    }
    bool judge = level(root);
    printf("%d", ans[0]);
    for(int i = 1; i < ans.size(); i++) 
        printf(" %d", ans[i]);
    printf("
%s
", judge ? "YES": "NO");
    return 0;
}
原文地址:https://www.cnblogs.com/littlepage/p/12842714.html