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.
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
 
题意:
本题是1066题和1110题的综合,通过给定的插入序列,首先需要正确构建AVL树;其次判断该树是否为完全二叉树。
 
思路:
略。看1066题和1110题的题解即可。
 
代码:
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;

struct Node{
    int val;
    int height;//以该结点为根结点的子树高度
    Node *lchild,*rchild;
    Node(),lchild(nullptr),rchild(nullptr){}
};

int n;//结点个数

int getHeight(Node* pNode)
{
    ;
    else return pNode->height;
}

int getBalancedFactor(Node* pNode)
{
    return getHeight(pNode->lchild)-getHeight(pNode->rchild);
}

void updateHeight(Node* pNode)
{
    pNode->height=max(getHeight(pNode->lchild),getHeight(pNode->rchild))+;
}

void leftRotation(Node* &pNode)
{
    Node* temp=pNode->rchild;
    pNode->rchild=temp->lchild;
    temp->lchild=pNode;
    updateHeight(pNode);
    updateHeight(temp);
    pNode=temp;
}

void rightRotation(Node* &pNode)
{
    Node* temp=pNode->lchild;
    pNode->lchild=temp->rchild;
    temp->rchild=pNode;
    updateHeight(pNode);
    updateHeight(temp);
    pNode=temp;
}

void insert(Node* &root,int val)
{
    if(root==nullptr){
        root=new Node(val);
        return;
    }
    if(val < root->val){
        insert(root->lchild,val);
        updateHeight(root);
        ){
            ){//LL型
                rightRotation(root);
            }){//LR型
                leftRotation(root->lchild);
                rightRotation(root);
            }
        }
    }else{
        insert(root->rchild,val);
        updateHeight(root);
        ){
            ){//RR型
                leftRotation(root);
            }){//RL型
                rightRotation(root->rchild);
                leftRotation(root);
            }
        }
    }
}

//层序遍历,并判断是否为完全二叉树
bool levelOrderTraversal(Node* root)
{
    ;
    bool flag=true;
    queue<Node*> q;
    q.push(root);
    while(!q.empty()){
        Node* temp=q.front();
        q.pop();
        if(temp){
            printf("%d",temp->val);
            cnt++;
            if(cnt<n) printf(" ");
            q.push(temp->lchild);
            q.push(temp->rchild);
        }else{
            if(cnt<n) flag=false;
        }
    }
    return flag;
}

int main()
{
    int val;
    Node* root=nullptr;
    scanf("%d",&n);
    ;i<n;i++){
        scanf("%d",&val);
        insert(root,val);
    }
    bool flag=levelOrderTraversal(root);
    printf("\n%s",flag?"YES":"NO");
    ;
}

1123 Is It a Complete AVL Tree的更多相关文章

  1. PAT甲级1123. Is It a Complete AVL Tree

    PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了 ...

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

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

  4. PAT甲级——1123 Is It a Complete AVL Tree (完全AVL树的判断)

    嫌排版乱的话可以移步我的CSDN:https://blog.csdn.net/weixin_44385565/article/details/89390802 An AVL tree is a sel ...

  5. PAT 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 sub ...

  6. PAT Advanced 1123 Is It a Complete AVL Tree (30) [AVL树]

    题目 An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child ...

  7. PAT甲级1123 Is It a Complete AVL Tree【AVL树】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805351302414336 题意: 给定n个树,依次插入一棵AVL ...

  8. PAT甲级题解-1123. Is It a Complete AVL Tree (30)-AVL树+满二叉树

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6806292.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  9. PAT 1123. Is It a Complete AVL Tree (30)

    AVL树的插入,旋转. #include<map> #include<set> #include<ctime> #include<cmath> #inc ...

随机推荐

  1. MoreEffectiveC++Item35 条款26: 限制某个class所能产生的对象个数

    一 允许零个或一个对象 我们知道每当即将产生一个对象,我们有一个constructor被调用,那么我们现在想组织某个对象的产生,最简单的方法就是将其构造函数声明成private(这样做同事防止了这个类 ...

  2. sql密码修改

    首先,以window 认证登陆. 2 然后,在 安全性---->登录名---->sa.右击 选择属性.直接修改 星号密码即可. 3 然后 在右上角 文件--->链接对象资源管理器-- ...

  3. [置顶] 【机器学习PAI实践八】用机器学习算法评估学生考试成绩

    (本文数据为实验用例) 一.背景 母亲是老师反而会对孩子的学习成绩造成不利影响?能上网的家庭,孩子通常能取得较好的成绩?影响孩子成绩的最大因素居然是母亲的学历?本文通过机器挖掘算法和中学真实的学生数据 ...

  4. uglify-es 解决webpack 不能压缩es6 的问题

    https://www.npmjs.com/package/uglify-es

  5. 【剑指offer】输入一颗二叉树的根节点,求二叉树的深度,C++实现

    原创博文,转载请注明出处! # 题目 # 举例        下图二叉树的深度为4,最长路径为1-2-5-7. # 思路(递归)       如果一个树只有一个节点,它的深度为1: 如果根节点只有左子 ...

  6. CentOS下glibc更新

    使用java -agentpath=xxx时报该错,提示glibc版本过低:version `GLIBC_2.14' not found (required by /data/jjns/UEISecu ...

  7. threejs 世界坐标与屏幕坐标相互转换

    屏幕坐标转世界坐标: let pX = (screenPoint.x / this.scene.renderer.domElement.clientWidth) * 2 - 1; let pY = - ...

  8. JS实现浏览器打印、打印预览

    1.JS实现打印的方式方式一:window.print()window.print();会弹出打印对话框,打印的是window.document.body.innerHTML中的内容,下面是从网上摘到 ...

  9. 【Codeforces】Round #491 (Div. 2) 总结

    [Codeforces]Round #491 (Div. 2) 总结 这次尴尬了,D题fst,E没有做出来.... 不过还好,rating只掉了30,总体来说比较不稳,下次加油 A:If at fir ...

  10. BZOJ2588 SPOJ10628 Count on a tree 【主席树】

    BZOJ2588 Count on a tree 题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中l ...