数据结构 二叉树其他操作都能运行为什么遍历是输不出来
求大神帮忙,问什么这程序遍历时输不出二叉树?#include<stdio.h>#include<stdlib.h>#defineERROR0#defineOK1typed...
求大神帮忙,问什么这程序遍历时输不出二叉树?
#include<stdio.h>
#include<stdlib.h>
#define ERROR 0
#define OK 1
typedef int status;
typedef char TElemType;
typedef struct BiTNode{
TElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
status CreateBTree(BiTree &BT )//根据先序序列a建立二叉链表存储结构
{
char a;
scanf("%c",&a);
if(a=='#')BT=NULL;
else
{
if(!(BT=(BiTNode*)malloc(sizeof(BiTNode))))
BT->data=a;
CreateBTree(BT->lchild);
CreateBTree(BT->rchild);
}
return OK;
}
void EmptyBTree(BiTree BT)//检查二叉树BT是否为空,空返回Yes,否则返回No
{
if(BT==NULL)printf("YES\n");
else printf("NO\n");
}
int DepthBTree(BiTree BT) //求二叉树BT的深度并返回该值
{int depthval,depthLeft,depthRight;
if(!BT)depthval=0;
else
{
depthLeft=DepthBTree(BT->lchild);
depthRight=DepthBTree(BT->rchild);
depthval=1+(depthLeft>depthRight?depthLeft:depthRight);
}
return depthval;
}
void PreOrder(BiTree BT) //先序遍历二叉树BT
{
if(BT!=NULL)
{
printf("%c",BT->data);
if(BT->lchild)
PreOrder(BT->lchild);
if(BT->rchild)
PreOrder(BT->rchild);
}
printf("\n");
}
void PostOrder(BiTree BT)
{
if(BT!=NULL)
{
if(BT->lchild)
PostOrder(BT->lchild);
if(BT->rchild)
PostOrder(BT->rchild);
printf("%c",BT->data);
}
printf("\n");
}
void main()
{
BiTree BT;
printf("请输入a:\n");
CreateBTree(BT);
printf("检查树是否为空(若为空则返回YES,否则为NO):");
EmptyBTree(BT);
printf("树的深度为:");
printf("%d",DepthBTree(BT));
printf("\n");
printf("二叉树先序遍历为:");
PreOrder(BT);
printf("二叉树后序遍历为:");
PostOrder(BT);
} 展开
#include<stdio.h>
#include<stdlib.h>
#define ERROR 0
#define OK 1
typedef int status;
typedef char TElemType;
typedef struct BiTNode{
TElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
status CreateBTree(BiTree &BT )//根据先序序列a建立二叉链表存储结构
{
char a;
scanf("%c",&a);
if(a=='#')BT=NULL;
else
{
if(!(BT=(BiTNode*)malloc(sizeof(BiTNode))))
BT->data=a;
CreateBTree(BT->lchild);
CreateBTree(BT->rchild);
}
return OK;
}
void EmptyBTree(BiTree BT)//检查二叉树BT是否为空,空返回Yes,否则返回No
{
if(BT==NULL)printf("YES\n");
else printf("NO\n");
}
int DepthBTree(BiTree BT) //求二叉树BT的深度并返回该值
{int depthval,depthLeft,depthRight;
if(!BT)depthval=0;
else
{
depthLeft=DepthBTree(BT->lchild);
depthRight=DepthBTree(BT->rchild);
depthval=1+(depthLeft>depthRight?depthLeft:depthRight);
}
return depthval;
}
void PreOrder(BiTree BT) //先序遍历二叉树BT
{
if(BT!=NULL)
{
printf("%c",BT->data);
if(BT->lchild)
PreOrder(BT->lchild);
if(BT->rchild)
PreOrder(BT->rchild);
}
printf("\n");
}
void PostOrder(BiTree BT)
{
if(BT!=NULL)
{
if(BT->lchild)
PostOrder(BT->lchild);
if(BT->rchild)
PostOrder(BT->rchild);
printf("%c",BT->data);
}
printf("\n");
}
void main()
{
BiTree BT;
printf("请输入a:\n");
CreateBTree(BT);
printf("检查树是否为空(若为空则返回YES,否则为NO):");
EmptyBTree(BT);
printf("树的深度为:");
printf("%d",DepthBTree(BT));
printf("\n");
printf("二叉树先序遍历为:");
PreOrder(BT);
printf("二叉树后序遍历为:");
PostOrder(BT);
} 展开
1个回答
展开全部
帮你该了一下,
主要问题是
if(!(BT=(BiTNode*)malloc(sizeof(BiTNode))))
改成
if((BT=(BiTNode*)malloc(sizeof(BiTNode))))
#include<stdio.h>
#include<stdlib.h>
#define ERROR 0
#define OK 1
typedef int status;
typedef char TElemType;
typedef struct BiTNode{
TElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
status CreateBTree(BiTree &BT )//根据先序序列a建立二叉链表存储结构
{
char a;
scanf("%c",&a);
if(a=='#')
BT=NULL;
else
{
if((BT=(BiTNode*)malloc(sizeof(BiTNode))))
BT->data=a;
CreateBTree(BT->lchild);
CreateBTree(BT->rchild);
}
return OK;
}
void EmptyBTree(BiTree BT)//检查二叉树BT是否为空,空返回Yes,否则返回No
{
if(BT==NULL)
printf("YES\n");
else
printf("NO\n");
}
int DepthBTree(BiTree BT) //求二叉树BT的深度并返回该值
{
int depthval,depthLeft,depthRight;
if(!BT)
depthval=0;
else
{
depthLeft=DepthBTree(BT->lchild);
depthRight=DepthBTree(BT->rchild);
depthval=1+(depthLeft>depthRight?depthLeft:depthRight);
}
return depthval;
}
void PreOrder(BiTree BT) //先序遍历二叉树BT
{
if(BT!=NULL)
{
printf("%c\t",BT->data);
if(BT->lchild)
PreOrder(BT->lchild);
if(BT->rchild)
PreOrder(BT->rchild);
}
}
void PostOrder(BiTree BT)
{
if(BT!=NULL)
{
if(BT->lchild)
PostOrder(BT->lchild);
if(BT->rchild)
PostOrder(BT->rchild);
printf("%c\t",BT->data);
}
}
void main()
{
BiTree BT;
printf("请输入a:\n");
CreateBTree(BT);
printf("检查树是否为空(若为空则返回YES,否则为NO):");
EmptyBTree(BT);
printf("树的深度为:");
printf("%d",DepthBTree(BT));
printf("\n");
printf("二叉树先序遍历为:");
PreOrder(BT);
printf("\n");
printf("二叉树后序遍历为:");
PostOrder(BT);
printf("\n");
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询