高分求救二叉树中序遍历问题

熊云昆 2004-11-20 09:51:41
小弟今天写了个程序,在电脑上运行却怎么也实现不了,大家帮忙看看。
typedef struct BiTNode{
TElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*Bitree;
typedef struct{
Bitree *base;
Bitree *top;
int stacksize;
}SqStack;
status InitStack(SqStack &S){
//构造一个空栈
S.base=(Bitree*)malloc(STACK_INIT_SIZE*sizeof(BiTNode));
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}
status StackEmpty(SqStack S){
if(S.top==S.base) return OK;
else return ERROR;
}
status push(SqStack &S,Bitree e){
//插入元素e为新的栈顶元素
if(S.top-S.base>=S.stacksize){//栈满,追加存储空间
S.base=(Bitree*)realloc(S.base,(S.stacksize+STACKCREMENT)*sizeof(BiTNode));
if(!S.base)exit(OVERFLOW);
S.top=S.base+S.stacksize;
S.stacksize+=STACKCREMENT;
}
*S.top++=e;
return OK;
}
status pop(SqStack &S,Bitree &e){
//若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK,否则返回ERROR
if(S.top==S.base)return ERROR;
e=--*S.top;
return OK;
}
status CreateBitree(Bitree &T){
//创建二叉树
LinkQueue Q;
Bitree p,q,r;
int a[STACK_INIT_SIZE];
int i=1,t=1;
cout<<"请输入二叉树的元素并以100结束\n";
while(t<STACK_INIT_SIZE&&a[t-1]!=100)
{cin>>a[t]; t++;}
if(!(T=(BiTNode*)malloc(sizeof(BiTNode))))exit(OVERFLOW);
T->data=a[1];p=T;
InitQueue(Q);
while(i<t){
Dequeue(Q,p);
if((2*i+1)<=t){if(a[2*i]!=0)
{if(!(q=(BiTNode*)malloc(sizeof(BiTNode))))exit(OVERFLOW);
q->data=a[2*i];p->lchild=q;
EnQueue(Q,q);}
else p->lchild=NULL;
if(a[2*i+1]!=0){
if(!(r=(BiTNode*)malloc(sizeof(BiTNode))))exit(OVERFLOW);
r->data=a[2*i+1]; p->rchild=r;
EnQueue(Q,r);}
else p->rchild=NULL;}
else{p->lchild=NULL;p->rchild=NULL;}
i++;}
return OK;
}
status visit(TElemType e){
cout<<e;
return OK;
}
status InorderTraverse(Bitree T){
//中序遍历二叉树
int e;
Bitree p;
SqStack S;
InitStack(S);p=T;
while(p||!StackEmpty(S)){
if(p){push(S,p);
p=p->lchild;
}
else{
pop(S,p);
e=p->data;
visit(e);
p=p->rchild;
}
}
return OK;
}
...全文
147 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
熊云昆 2004-11-23
  • 打赏
  • 举报
回复
帮帮忙啊
Eddie005 2004-11-22
  • 打赏
  • 举报
回复
呵呵~ CSDN问功课............
寻开心 2004-11-22
  • 打赏
  • 举报
回复
粗略看了一下,中序算法没有问题
问题应该在你的堆栈的push或者pop里面
熊云昆 2004-11-22
  • 打赏
  • 举报
回复
这是老师布置的作业,她要求我们不能用递归来实现遍历
CloseWind 2004-11-21
  • 打赏
  • 举报
回复
晕啊~~~~看的眼都花了! 小弟的水平不是很高呀,不过要没什么特殊要求的话,二叉树的遍历还是用递归比较好,逻辑比较清楚!!
jp1984 2004-11-21
  • 打赏
  • 举报
回复
[james@Mathematcs james]$ cc -o test love.c
[james@Mathematcs james]$ ./test
4 2 5 1 6 3 7
[james@Mathematcs james]$ cat love.c
#include <stdio.h>

struct btree
{
int value;
struct btree* lchild;
struct btree* rchild;
};

/*
build a tree like
1
2 3
4 5 6 7

as a example and the in order traversing will be

4 2 5 1 6 3 7
*/
/*
we just build the tree using hand
cuz it is just for showing purpose
when node get more use recurrsive
to do this job...
*/
struct btree* getNode(struct btree* newNode,int v)
{
newNode = (struct btree*) malloc(1);
if(newNode == NULL)
exit(0);
newNode -> value = v;
newNode -> lchild = NULL;
newNode -> rchild = NULL;
return newNode;
}
struct btree* buildTree(struct btree* lc,struct btree* pa,struct btree* rc)
{
pa -> lchild = lc;
pa -> rchild = rc;
return pa;
}
void inOrder(struct btree* love)
{
if(love != NULL)
{
inOrder(love -> lchild);
printf("%d ",love -> value);
inOrder(love -> rchild);
}
}
int main()
{
struct btree* root = getNode(root,1); /* create the tree's root */
struct btree* a = getNode(a,2);
struct btree* b = getNode(b,3);
struct btree* c = getNode(c,4);
struct btree* d = getNode(d,5);
struct btree* e = getNode(e,6);
struct btree* f = getNode(f,7);
buildTree(a,root,b);
buildTree(c,a,d);
buildTree(e,b,f);
inOrder(root);
putchar('\n');
return 0;
}

33,007

社区成员

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

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