一个二叉树的非第归遍历程序。错在哪里???

gengpengfei 2004-05-06 10:57:11
这个程序基本上是用C写的,但是其中使用了引用。所以就发在这个板上了。
请高手查错。最好把修改好的程序贴出来。谢谢。

#include<stdio.h>
#include<stdlib.h>

typedef struct BitNode
{
char data;
struct BitNode * lchild,* rchild;
}BitNode,*BitTree;

typedef struct
{
BitNode * base;
BitNode * top;
int stacksize;
}SqStack;

int InitStack(SqStack &S)
{
S.base=(BitNode*)malloc(100*sizeof(BitNode));
if(!S.base) exit(-1);
S.top=S.base;
S.stacksize=100;
return 1;
}

int GetTop(SqStack &S,BitNode &e)
{
if(S.top==S.base)
return -1;
e=*(S.top-1);
return 1;
}

int StackEmpty(SqStack &S)
{
if(S.top==S.base)
return 1;
else
return 0;
}
int Push(SqStack &S,BitNode &e)
{
if(S.top-S.base>=S.stacksize)
{
S.base=(BitNode*)realloc(S.base,(S.stacksize+10)*sizeof(BitNode));
if(!S.base)
exit(-1);
S.top=S.base+S.stacksize;
S.stacksize+=10;
}
*S.top=e;
S.top++;
return 1;
}

int Pop(SqStack &S,BitNode &e)
{
if(S.top==S.base)
return -1;
e=*(--S.top);
return 1;
}

int CreateBitTree(BitTree &T)
{
char ch;
scanf("%c",&ch);
if(ch==' ')
T=NULL;
else
{
if(!(T=(BitNode*)malloc(sizeof(BitNode))))
exit(-1);
T->data=ch;
CreateBitTree(T->lchild);
CreateBitTree(T->rchild);
}
return 1;
}

int InOrderTraverse(BitTree &T,int (*Visit)(int e))
{
BitNode p;
SqStack S;
InitStack(S);
Push(S,*T);
while(!StackEmpty(S))
{
while(GetTop(S,p)&&(p.data!=NULL))
Push(S,*(p.lchild));
Pop(S,p);
if(!StackEmpty(S))
{
Pop(S,p);
if(!Visit(p.data))
return -1;
Push(S,*(p.rchild));
}
}
return 1;
}

int PrintElement(int e)
{
printf("%d",e);
return 1;
}

void main()
{
BitTree T;
CreateBitTree(T);
InOrderTraverse(T,PrintElement);
}

...全文
49 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
gengpengfei 2004-05-09
  • 打赏
  • 举报
回复
太谢谢了。这几天没有上网,现在结帐。呵呵!!!
sboom 2004-05-06
  • 打赏
  • 举报
回复
你把没关系的代码去掉好吗?这么长懒得看了。
PageYi 2004-05-06
  • 打赏
  • 举报
回复
你這個函數要實現甚麼功能InOrderTraverse
Carl2000 2004-05-06
  • 打赏
  • 举报
回复
ding
zhouqingyuan 2004-05-06
  • 打赏
  • 举报
回复
上面的程序只是把它改成能够运行,当然其中还有很多地方可以改的更好一点的。
zhouqingyuan 2004-05-06
  • 打赏
  • 举报
回复
改成这样是可以的,不过始终觉得这样构造树在输入节点的时候是一种痛苦,还一直要考虑空格输入几个。
#include<stdio.h>
#include<stdlib.h>

typedef struct BitNode
{
char data;
struct BitNode * lchild,* rchild;
}BitNode,*BitTree;

typedef struct
{
BitNode * base;
BitNode * top;
int stacksize;
}SqStack;

int InitStack(SqStack &S)
{
S.base=(BitNode*)malloc(100*sizeof(BitNode));
if(!S.base) exit(-1);
S.top=S.base;
S.stacksize=100;
return 1;
}

void GetTop(SqStack &S,BitNode &e)
{
if(S.top==S.base)
return ;
e=*(S.top-1);
return ;
}

int StackEmpty(SqStack &S)
{
if(S.top==S.base)
return 1;
else
return 0;
}
int Push(SqStack &S,BitNode &e)
{
if(S.top-S.base>=S.stacksize)
{
S.base=(BitNode*)realloc(S.base,(S.stacksize+10)*sizeof(BitNode));
if(!S.base)
exit(-1);
S.top=S.base+S.stacksize;
S.stacksize+=10;
}
*(S.top)=e;
(S.top)++;
return 1;
}

int Pop(SqStack &S,BitNode &e)
{
if(S.top==S.base)
return 0;
e=*(--S.top);
return 1;
}

BitTree CreateBitTree()
{
BitTree T;
char ch;
scanf("%c",&ch);
if(ch==' ')
return NULL;
else
{
if(!(T=(BitNode*)malloc(sizeof(BitNode))))
exit(-1);
T->data=ch;
T->lchild=CreateBitTree();
T->rchild=CreateBitTree();
}
return T;
}

int InOrderTraverse(BitTree &T,int (*Visit)(char e))
{
BitNode p;
SqStack S;
InitStack(S);
Push(S,*T);
GetTop(S,p);
BitNode *q=&p;
while(!StackEmpty(S))
{
while(q&&q->lchild)
{
Push(S,*(q->lchild));
q=q->lchild;
}
if(!StackEmpty(S))
{
Pop(S,p);
if(!Visit(p.data))
return -1;
q=p.rchild;
if(q)
Push(S,*q);
}
}

return 1;
}

int PrintElement(char e)
{
printf("%c",e);
return 1;
}

void main()
{
BitTree T;
T=CreateBitTree();
InOrderTraverse(T,PrintElement);
}



64,641

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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