zoukankan      html  css  js  c++  java
  • 1123.(重、错)Is It a Complete AVL Tree

    题意:给定结点个数n和插入序列,判断构造的AVL树是否是完全二叉树?

    思路AVL树的建立很简单。而如何判断是不是完全二叉树呢?通过层序遍历进行判断:当一个结点的孩子结点为空时,则此后就不能有新的结点入队。若没有,则是完全二叉树,否则不是

    代码:

    #include <cstdio>
    #include <algorithm>
    #include <iostream>
    #include <vector>
    #include <queue>
    using namespace std;
    
    vector<int> layer;
    
    struct Node {
        int v, height;
        Node *lchild, *rchild;
    };
    
    
    Node* newNode(int v) {
        Node* pNode = new Node;
        pNode->v = v;
        pNode->height = 1;
        pNode->lchild = pNode->rchild = NULL;
        return pNode;
    }
    
    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 getBalanceFactor(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 = newNode(v);
            return;
        }
    
        if (v < root->v) {
            insert(root->lchild,v);
            updateHeight(root);
            if (getBalanceFactor(root) == 2) {
                    if(getBalanceFactor(root->lchild)==1){
                        R(root);
                    }else if(getBalanceFactor(root->lchild)==-1){
                        L(root->lchild);
                        R(root);
                    }
    
    
            }
        }
        else {
            insert(root->rchild,v);
            updateHeight(root);
            if (getBalanceFactor(root) == -2) {
                if(getBalanceFactor(root->rchild)==-1){
                    L(root);
                }
                else if(getBalanceFactor(root->rchild)==1){
                    R(root->rchild);
                    L(root);
                }
            }
        }
    }
    bool isComplete =true;
    int after=1;
    void layerOrder(Node* root){
        queue<Node*> Q;
        Q.push(root);
        while(!Q.empty()){
            Node* front=Q.front();
            Q.pop();
            layer.push_back(front->v);
    
            if(front->lchild!=NULL){
                if(after==0) isComplete=false;
                Q.push(front->lchild);
            }else{
                after=0;
            }
    
            if(front->rchild!=NULL){
                if(after==0) isComplete=false;
                Q.push(front->rchild);
            }else{
                after=0;
            }
        }
    
    }
    
    
    //vector<int> insertOrder;
    
    int main()
    {
        int n,data;
        scanf("%d",&n);
        Node* root=NULL;
        for(int i=0;i<n;i++){
            scanf("%d",&data);
            insert(root,data);
        }
        layerOrder(root);
    
        for(int i=0;i<layer.size()-1;i++){
            printf("%d ",layer[i]);
        }
        printf("%d
    ",layer[n-1]);
        printf("%s
    ",isComplete==true?"YES":"NO");
    
        return 0;
    }
  • 相关阅读:
    hdu1233
    zoj 3529
    hdu 2516 取石子游戏
    组合博弈理论
    博弈——sg函数的原理和优化
    博弈及sg函数
    poj2039
    hdu 1250
    C# 类的继承和访问
    C# 索引
  • 原文地址:https://www.cnblogs.com/fuqia/p/9506168.html
Copyright © 2011-2022 走看看