第4章第1节练习题10 判断某二叉树是否为完全二叉树

问题描述

试写一算法,完成判定某二叉树是否为完全二叉树的功能

算法思想

根据完全二叉树的定义,具有n个节点的完全二叉树与满二叉树中的编号从1~n的节点一一对应,可以考虑使用层次遍历的思想来实现。
将所有节点入队(包括空节点)。当遇到空节点时,查看其后是否还有不为空的节点:如果没有,则为完全二叉树;如果有,则不是完全二叉树;

算法描述

int IsComplete(BiTNode* T){
    SqQueue Q;
    InitQueue(&Q);

    BiTNode *p=T;
    EnQueue(&Q,p);

    while(IsEmptyQueue(&Q)!=0){
        p=DeQueue(&Q);
        if(p!=NULL){
            EnQueue(&Q,p->lchild);
            EnQueue(&Q,p->rchild);
        }else{
            while(IsEmptyQueue(&Q)!=0){
                p=DeQueue(&Q);
                if(p!=NULL){
                    return -1;
                }
            }
        }
    }
    return 0;
}

具体代码见附件。


附件

//AB#DF###C#E##
//ABD##E##CF###
#include<stdio.h>
#include<stdlib.h>

#define MaxSize 100
typedef char ElemType;

/*-----------------------------------------*/

typedef struct BiTNode{
    ElemType data;
    struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;

BiTNode* CreateBiTree(BiTNode*);
int IsComplete(BiTNode*);

/*-----------------------------------------*/

typedef struct SqQueue{
    BiTNode* data[MaxSize];
    int front, rear;
}SqQueue;

void InitQueue(SqQueue*);
void EnQueue(SqQueue*, BiTNode*);
BiTNode* DeQueue(SqQueue*);
int IsEmptyQueue(SqQueue*);

/*-----------------------------------------*/

int main(int argc, char* argv[]){
    BiTNode *T=(BiTNode*)malloc(sizeof(BiTNode));
    T=CreateBiTree(T);

    int flag=IsComplete(T);
    if(flag==0){
        printf("Commplete Bitreee!\n");
    }else{
        printf("No complete Bitree!\n");
    }
    return 0;
}

/*-----------------------------------------*/

//创建二叉树
BiTree CreateBiTree(BiTNode* T){
    ElemType x;
    scanf("%c",&x);
    if(x=='#'){
        return T;
    }else{
        T=(BiTNode*)malloc(sizeof(BiTNode));
        T->data=x;
        T->lchild=CreateBiTree(T->lchild);
        T->rchild=CreateBiTree(T->rchild);
    }
    return T;
}

int IsComplete(BiTNode* T){
    SqQueue Q;
    InitQueue(&Q);

    BiTNode *p=T;
    EnQueue(&Q,p);

    while(IsEmptyQueue(&Q)!=0){
        p=DeQueue(&Q);
        if(p!=NULL){
            EnQueue(&Q,p->lchild);
            EnQueue(&Q,p->rchild);
        }else{
            while(IsEmptyQueue(&Q)!=0){
                p=DeQueue(&Q);
                if(p!=NULL){
                    return -1;
                }
            }
        }
    }
    return 0;
}

/*-----------------------------------------*/

void InitQueue(SqQueue* Q){
    Q->front=0;
    Q->rear=0;
}

void EnQueue(SqQueue* Q, BiTNode* T){
    if((Q->rear+1)%MaxSize==Q->front){
        return;
    }
    Q->data[Q->rear++]=T;
}

BiTNode* DeQueue(SqQueue* Q){
    if(Q->front==Q->rear){
        return NULL;
    }
    return Q->data[Q->front++];
}

int IsEmptyQueue(SqQueue* Q){
    if(Q->front==Q->rear){
        return 0;
    }
    return -1;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值