这个程序错哪里了

LUYAOTIAN 2008-09-09 02:49:27
程序编译运行无错,结果却不对。
#include <stdio.h>
#include <stdlib.h>
#define num 100
typedef char DataType;
typedef struct node {
DataType data;
struct node *lchild,*rchild;
}BinTNode;
typedef BinTNode * BinTree;
int found;
BinTNode *p;
BinTree CreateBinTree(BinTree bt)
{
BinTNode * Q[num];
BinTNode * s;
int front ,rear;
char ch;
ch=getchar();bt=NULL;
front=1;rear=0;
while(ch!='#'){
s=NULL;
if(ch!='@'){
s=(BinTNode *)malloc(sizeof(BinTNode));
s->data=ch;
s->lchild=s->rchild=NULL;
}
rear++;
Q[rear]=s;
if(rear==1)
bt=s;
else
{
if(s!=NULL && Q[front]!=NULL)
if(rear % 2==0)
Q[front]->lchild=s;
else
Q[front]->rchild=s;
if(rear % 2 !=0)
front ++;
}
ch=getchar();
}
return bt;
}
void Preorder(BinTree bt)
{
BinTNode * stack[num];
int top=0;
BinTNode * s;
stack[top]=bt;
while(top>=0)
{
s=stack[top];
top--;
if(s!=NULL)
{ printf("%c",s->data);
top++;
stack[top]=s->rchild;
top++;
stack[top]=s->lchild;
}
}
}
void Inorder(BinTree bt)
{
BinTNode * stack[num];
int top=0;
stack[top]=bt;
do
{
while(stack[top]!=NULL)
{
top=top+1;
stack[top]=stack[top-1]->lchild;
}
top=top-1;
if(top>=0)
{
printf("%c",stack[top]->data);
stack[top]=stack[top]->rchild;
}
}while(top>=0);
}
void Postorder(BinTree bt)
{
BinTNode * stack[num];
int tag[num];
int top;
BinTNode * s;
top=0;
s=bt;
do
{
while(s!=NULL)
{
top++;
stack[top]=s;
tag[top]=0;
s=s->lchild;
}
if(top>0)
{
s=stack[top];
if(top>0)
{
printf("%c",stack[top]->data);
top--;
s=stack[top];
}
if(top>0)
{
if(tag[top]!=1){
s=s->rchild;
tag[top]=1;
}
else s=NULL;
}
}
}while(top!=0);
}
void main()
{
BinTree bt;
int xz=1;
char ch1;
while(xz){
printf(" 二叉树的遍历 \n");
printf("================================\n");
printf(" 1.建立二叉树的存储结构 \n");
printf(" 2.求二叉树的前序遍历 \n");
printf(" 3.求二叉树的中序遍历 \n");
printf(" 4.求二叉树的后序遍历 \n");
printf(" 0.退 出 系 统 \n");
printf("================================\n");
printf(" 请 选 择 :0—4 ");
scanf("%d",&xz);
switch(xz){
case 1: printf("输入二叉树的按层结点值,用@表示空节点,#表示结束(按完全二叉树): \n");
bt=CreateBinTree(bt);
printf("二叉树的链式存储结构建立完成! \n");
break;
case 2: printf("该二叉树的前序遍历序列为: ");
Preorder(bt);
printf("\n");
break;
case 3: printf("该二叉树的中序遍历序列是 ");
Inorder(bt);
printf("\n");
break;
case 4: printf("该二叉树的后序遍历序列是 ");
Postorder(bt);
printf("\n");
break;
case 0: return;
}
}
}
...全文
89 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunnywyg 2008-09-12
  • 打赏
  • 举报
回复
问题无比的多
首先,main函数的第一句
BinTree bt;
指针未初始化就敢拿来用?
fgwsed 2008-09-12
  • 打赏
  • 举报
回复
这么强悍!!
LUYAOTIAN 2008-09-12
  • 打赏
  • 举报
回复
恳请帮忙了,结构还是清晰的
oo 2008-09-11
  • 打赏
  • 举报
回复
这么长的代码,没几个人有时间来调试的。

你还是自己单步跟踪看看哪里有问题吧
或者多打印一些log信息来查问题。
LUYAOTIAN 2008-09-11
  • 打赏
  • 举报
回复
请专家帮忙给出呀

33,010

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧