判断一棵二叉树是否是完全二叉树

层次遍历一个二叉树,扫描到某个结点左孩子或右孩子为空时即停止,检查此时队列内所有结点是否均为叶子结点

#include <stdio.h>
#include <malloc.h>
#define false 0
#define true 1

typedef int status;
typedef int elemtype;
typedef struct BiTNode {
    elemtype data;
    struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;

BiTree creat(){ //按扩展前序建二叉树;
    BiTree t;int x;
    scanf("%d",&x);
    if (x==0) t = NULL;
    else 
    { 
        t = (BiTree)malloc(sizeof(BiTNode));
        t->data = x;
        t->lchild = creat();
        t->rchild = creat();
    }
    return t;
}


status judge(BiTree bt)
{//判断一个给定的二叉树是否是完全二叉树
    if(bt == NULL)
    {
        printf("该二叉树是完全二叉树!\n");
        return true;
    }
        
    else
    {
        BiTree qu[100]; int front = -1, rear = -1;
        BiTree p;
        qu[++rear] = bt;
        while(front != rear)
        {
            p = qu[++front];
            if(p->lchild !=NULL)
                qu[++rear] = p->lchild;
            if(p->rchild != NULL)
                qu[++rear] = p->rchild;
            if(p->lchild == NULL || p->rchild == NULL)
                break;
        }
        while(front != rear)
        {
            p = qu[++front];
            if(p->lchild != NULL || p->rchild != NULL)
            {
                printf("该二叉树不是完全二叉树!\n");
                return false;
            }
        }
        if(front == rear)
        {
            printf("该二叉树是完全二叉树!\n");
            return true;
        }
            
            
    }
    
}

int main()
{
    printf("正在按扩展前序建二叉树:\n");
    BiTree bt = creat();
    judge(bt);
    
    return 0;
}

或者王道上的做法:层次遍历二叉树,将非空结点(包括空指针)入队,遇到空结点时,开始检查队其后是否有非空结点(即队列内是否有非空结点)(还是王道的简洁一点==)

#include <stdio.h>
#include <malloc.h>
#define false 0
#define true 1

typedef int status;
typedef int elemtype;
typedef struct BiTNode {
    elemtype data;
    struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;

BiTree creat(){ //按扩展前序建二叉树;
    BiTree t;int x;
    scanf("%d",&x);
    if (x==0) t = NULL;
    else 
    { 
        t = (BiTree)malloc(sizeof(BiTNode));
        t->data = x;
        t->lchild = creat();
        t->rchild = creat();
    }
    return t;
}


status judge(BiTree bt)
{//判断一个给定的二叉树是否是完全二叉树
//层次遍历一个二叉树,扫描到空指针处即停止,检查此时队列内所有结点是否均为叶子结点
    if(bt == NULL)
    {
        printf("该二叉树是完全二叉树!\n");
        return true;
    }
        
    else
    {
        BiTree qu[100]; int front = -1, rear = -1;
        BiTree p;
        qu[++rear] = bt;
        while(front != rear)
        {
            p = qu[++front];
            if(p->lchild !=NULL || p->rchild != NULL)
            {
                qu[++rear] = p->lchild;
                qu[++rear] = p->rchild;
            }
            else
            {
                while(front != rear)
                {
                    p = qu[++front];
                    if(p!=NULL)
                    {
                        printf("该二叉树不是完全二叉树!\n");
                        return false;
                    }
                }
            }
        }
        
        printf("该二叉树是完全二叉树!\n");
        return true;
    }
    
}

void print(BiTree bt)
{
    if (bt!=NULL)
    {
        printf ("%d", bt->data);
        print(bt->lchild);
        print(bt->rchild);
    }
}

int main()
{
    printf("正在按扩展前序建二叉树:\n");
    BiTree bt = creat();
    judge(bt);
    
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值