编写算法,判断一个二叉树链存储的二叉树是否为完全二叉树

论坛 期权论坛 期权     
jianshu01   2018-4-26 13:41   5294   1
int num(BiTreeNode *t,int i,int *max)
//求出二叉树的最大编号m
//t为二叉树的根结点指针,i为根结点编号,m为当前结点的最大编号
{
if(t==NULL)return 0;
if(*maxleftChild,2*i+1,max)+num(t->rightChild,2*i+2,max))...int num(BiTreeNode *t,int i,int *max)
//求出二叉树的最大编号m
//t为二叉树的根结点指针,i为根结点编号,m为当前结点的最大编号
{
if(t==NULL)return 0;
if(*maxleftChild,2*i+1,max)+num(t->rightChild,2*i+2,max));
}
int checkTree(BiTreeNode *t)
//判断二叉树t是否为完全二叉树,是返回1,否则返回0
{
int n,maxno=0;
n=num(t,0,&maxno);  //求出二叉树t的最大编号 maxno
if(n-1==maxno)
  return 1;
else
  return 0;
}

能帮我完成吗?展开
分享到 :
0 人收藏

1 个回复

倒序浏览
2#
bearxiaocong  1级新秀 | 2018-4-30 02:32:11
#include
#include
#define Max 100

typedef struct Node
{
char data;
struct Node * LChild,*RChild;
}BiTNode,*BiTree;

void CreateBiTree(BiTree * bt)
{
char ch;
ch=getchar();
if(ch==10)ch=getchar();//如果为 回车换行  读取下一个字符
if(ch=='.') *bt=NULL; //如果为 .  代表此节点为空
else
{
  * bt=(BiTree)malloc(sizeof(BiTNode));
  (* bt)->data=ch; //赋值
  CreateBiTree(&((* bt)->LChild));
  CreateBiTree(&((* bt)->RChild));
}
}

bool fullBiTree(BiTree b)
{
if(b->LChild==NULL && b->RChild==NULL)return true;// 如果左右子树为空,返回真
if(b->LChild==NULL || b->RChild==NULL)return false;// 如果左右子树只有一个为空,返回假
return fullBiTree(b->LChild) && fullBiTree(b->RChild);// 通过递归,返回
}

void main()
{
printf("请依次输入字符\n");
BiTree b;
CreateBiTree(&b); //创建二叉树
bool cm=fullBiTree(b);
if(cm)printf("麓此二叉树为完全二叉树\n");
else printf("麓此二叉树不是完全二叉树\n");
}
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:
帖子:
精华:
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP