生活会辜负努力的人,但不会辜负一直努力的人

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

题意:给定结点个数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\n",layer[n-1]);
    printf("%s\n",isComplete==true?"YES":"NO");

    return 0;
}

 

posted on 2018-08-20 16:11  何许亻也  阅读(294)  评论(0编辑  收藏  举报