PAT 甲级 刷题日记|A 1123 Is It a Complete AVL Tree (30 分)

单词

complete binary tree 完全二叉树

restore 修复 恢复

题目

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.
1628848464776.png

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
结尾无空行

思路

平衡二叉树的题目模板固定。

判断一个树是否为完全二叉树,层次遍历,若出现空节点后又出现了节点,则不是完全二叉树。

代码

#include 
using namespace std;

vector ans;
int flag = 1;
int after = 0;

struct node{
    int v, height;
    node *lchild, *rchild;
    node(int data): v(data), height(1), lchild(NULL), rchild(NULL){
    }
};

int getHeight(node* root) {
    if (root == NULL) return 0;
    return root->height;
}

void updateHeight(node* root) {
    root->height = max(getHeight(root->lchild), getHeight(root->rchild)) + 1;
}

int getFactor(node* root) {
    return getHeight(root->lchild) - getHeight(root->rchild);
}

void L(node* &root) {
    node* temp = root->rchild;
    root->rchild = temp->lchild;
    temp->lchild = root;
    updateHeight(root);
    updateHeight(temp);
    root = temp;
}

void R(node* &root) {
    node* temp = root->lchild;
    root->lchild = temp->rchild;
    temp->rchild = root;
    updateHeight(root);
    updateHeight(temp);
    root = temp;
}

void insert(node* &root, int v) {
    if (root == NULL) {
        root = new node(v);
        return;
    }
    if (v < root->v) {
        insert(root->lchild, v);
        updateHeight(root);
        if (getFactor(root) == 2) {
            if (getFactor(root->lchild) == 1) {
                R(root);
            } else if(getFactor(root->lchild) == -1) {
                L(root->lchild);
                R(root);
            }
        }
    } else {
        insert(root->rchild, v);
        updateHeight(root);
        if (getFactor(root) == -2) {
            if (getFactor(root->rchild) == 1) {
                R(root->rchild);
                L(root);
            } else if(getFactor(root->rchild) == -1) {
                L(root);
            
            }
        }
    }
}

void level(node *root) {
    if (root == NULL) {
        flag = 2;
        return;
    }
    queue mq;
    mq.push(root);
    while (!mq.empty()) {
        node* now = mq.front();
        ans.push_back(now->v);
        mq.pop();
        if (flag == 1) {
            if (now->lchild != NULL || now->rchild != NULL) flag = 2;
        }
        if (now->lchild != NULL) {
            if (after) flag = 0;
            mq.push(now->lchild);
        } else {
            after = 1;
        }
        if (now->rchild != NULL) {
            if (after) flag = 0;
            mq.push(now->rchild);
        } else {
            after = 1;
        }
        
        
    }
}


int main() {
    int n;
    int data[10000];
    cin>>n;
    node* root = NULL;
    for (int i = 0; i < n; i++) {
        cin>>data[i];
        insert(root, data[i]);
    }
    level(root);
    for (int i = 0; i < n; i++) {
        cout<

你可能感兴趣的:(PAT 甲级 刷题日记|A 1123 Is It a Complete AVL Tree (30 分))