1123.Is It a Complete AVL Tree

题目描述

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

考点

1.平衡二叉树的插入;
2.层序遍历;
3.完全二叉树。

思路

1.如何平衡二叉树
平衡二叉树的失衡调整主要是通过旋转最小失衡子树来实现的。总共有LL、LR、RL、RR四种情况,分别对应右旋、左旋、先右旋再左旋、先左旋再右旋。RL和LR都可以调用LL、RR的代码实现。
2.层序遍历
层序遍历一般用队列实现,可以利用c++中的queue,方便快捷。
3.完全二叉树的判断
如果有已经出现了孩子为空的结点,后续出现了孩子不为空结点,则非完全二叉树。

代码

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
struct node {
    int val;
    struct node *left, *right;
};
node* LR(node *tree) {
    node *temp = tree->right;
    tree->right = temp->left;
    temp->left = tree;
    return temp;
}
node* RR(node *tree) {
    node *temp = tree->left;
    tree->left = temp->right;
    temp->right = tree;
    return temp;
}
node *LRR(node *tree) {
    tree->left = LR(tree->left);
    return RR(tree);
}
node *RLR(node *tree) {
    tree->right = RR(tree->right);
    return LR(tree);
}
int getHeight(node *tree) {
    if (tree == NULL)return 0;
    return max(getHeight(tree->left), getHeight(tree->right)) + 1;
}
node* insert(node *tree, int val) {
    if (tree == NULL) {
        tree = new node();
        tree->val = val;
    }
    else if (tree->val > val) {
        tree->left = insert(tree->left, val);
        if (getHeight(tree->left) - getHeight(tree->right) >= 2) {
            if (val < tree->left->val) tree = RR(tree);
            else tree = LRR(tree);
        }
    }
    else {
        tree->right = insert(tree->right, val);
        if (getHeight(tree->right) - getHeight(tree->left) >= 2) {
            if (val > tree->right->val) tree = LR(tree);
            else tree = RLR(tree);
        }
    }
    return tree;
}
int after = 1, isComplete = 1;
void levelorder(node *tree) {
    queue<node *> q;
    int count = 0;
    q.push(tree);
    while (!q.empty()) {
        node *temp = q.front();
        q.pop();
        cout << (count == 0 ? "" : " ") << temp->val;
        count++;
        if (temp->left != NULL) {
            q.push(temp->left);
            if (after == 0) {
                isComplete = 0;
            }
        }
        else after = 0;
        if (temp->right != NULL) {
            q.push(temp->right);
            if (after == 0) {
                isComplete = 0;
            }
        }
        else after = 0;
    }
}
int main() {
    int n, i, t;
    cin >> n;
    node *tree = NULL;
    for (i = 0; i < n; i++) {
        cin >> t;
        tree = insert(tree, t);
    }
    levelorder(tree);
    cout << endl;
    cout << (isComplete==1? "YES" : "NO") << endl;
    return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,164评论 0 10
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,487评论 0 23
  • AVL树建立模板写好,之后层序遍历判断是否为完全二叉树:在出现空子节点之后仍有非空子节点出现则不为完全二叉树
    DaiMorph阅读 1,703评论 0 0
  • 我们精心策划,按部就班,等待某一个时刻的发生,也许它真的发生,却和我们想得并不一样。 既然总要有人当第一名,可是这...
    清清Echo阅读 3,544评论 0 1
  • “哐”的一声,萱萱把手机线从耳朵里拔出来,丢在了手机上。她不想这么生着气回去,而且路上一开就是两个小时,还饿着肚子...
    小easy阅读 2,695评论 1 1