数据结构复习(完全二叉树判定)

编写算法判别给定二叉树是否为完全二叉树。

要求实现下列函数:
Status CompleteBiTree(BiTree bt);
/* judge if the binary tree whose root is bt */
/* is a complete tree.                       */

二叉链表类型定义:
typedef struct BiTNode {
    TElemType data;
    BiTNode  *lchild, *rchild;
} BiTNode, *BiTree;

可用队列类型Queue的相关定义:
typedef BiTree QElemType; // 设队列元素为二叉树的指针类型
Status InitQueue(Queue &Q);
Status EnQueue(Queue &Q, QElemType e);
Status DeQueue(Queue &Q, QElemType &e);
Status GetHead(Queue Q, QElemType &e);
Status QueueEmpty(Queue Q);

Status CompleteBiTree(BiTree bt)
/* judge if the binary tree whose root is bt */
/*  is a complete tree.                      */
{
    Queue q;
    InitQueue(q);
    BiTree pb=bt;
    int flag=0;
    if(!bt)
        return TRUE;
    EnQueue(q,pb);
    while(!QueueEmpty(q)&&flag==0){
        DeQueue(q,pb);
        if(pb){
            EnQueue(q,pb->lchild);
            EnQueue(q,pb->rchild);
        }
        else
            flag=1;
    }
    while(!QueueEmpty(q)){
        DeQueue(q,pb);
        if(pb)
            flag=0;
    }
    if(flag==0)
        return FALSE;
    return TRUE;   
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值