主题:那位高手能否看一下程序错在那里?
下面是一段创建二叉链表和先序和中序算法遍历二叉树的源程序。小弟数据结构学得不是很好,我找很久还是有错误。那位编程高手帮看一下,找出错误。最好能加上后续算法。顺便请教一下,怎样使用函数调用?特别是在数据结构算法中。
#define MAX_TREE_SIZE 100
#define TElemType int
#define ok 1
#define ERROR 0
#include"stdio.h"
typedef TElemType SqBiTree[MAX_TREE_SIZE];
SqBiTree bt;
typedef struct BiTNode{
TElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
Status CreatBiTree(BiTree *T)
{ scanf("%d",ch);
if(ch=='') T=NULL;
else{
if(!(T=(BiTNode *)malloc(sizeof(BiTNode))))
exit(OVERFLOW);
T->data=ch;
CreatBiTree(T->lchild);
CreatBiTree(T->rchild);
}
return T;
}
Status PreOrderTraverse(BiTree T, Status( *Vist)(TElemType e)){
Status PrintElement(TElementType e){
printf("%d",e);
return ok;
}
if(T)
{if(Vist(T->data))
if(PreOrderTraverse(T->lchild),Vist))
if(PreOrderTraverse(T->rchild),Vist))
return ok;
return ERROR;
}
else return ok;
}
Status InorderTraverse(BiTree T,Status(*Vist) (TElementType e)){
InitStack(S); p=T;
while(p||!StackEmpty(S)){
if((p) {Push(S,p); p=p->lchild;}
else{
Pop(S,p); if(!Vist(p->data))
return ERROR;
p=p->rchild;
}
}
return ok;
}
int main(viod)
{
int ch,e,t;
printf("please input the data:");
Status CreatBiTree(BiTree *T);
scanf("%d",&t);
printf("if you want to use PreOrder,press 1,use the InOder,press 2:");
switch(t){
case 1: Status PreOrderTraverse(BiTree T, Status( *Vist)(TElemType e)) ;break;
case 2: Status InorderTraverse(BiTree T,Status(*Vist) (TElementType e));break;
default:printf("ERROR!");break;
}
getch();
}
#define MAX_TREE_SIZE 100
#define TElemType int
#define ok 1
#define ERROR 0
#include"stdio.h"
typedef TElemType SqBiTree[MAX_TREE_SIZE];
SqBiTree bt;
typedef struct BiTNode{
TElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
Status CreatBiTree(BiTree *T)
{ scanf("%d",ch);
if(ch=='') T=NULL;
else{
if(!(T=(BiTNode *)malloc(sizeof(BiTNode))))
exit(OVERFLOW);
T->data=ch;
CreatBiTree(T->lchild);
CreatBiTree(T->rchild);
}
return T;
}
Status PreOrderTraverse(BiTree T, Status( *Vist)(TElemType e)){
Status PrintElement(TElementType e){
printf("%d",e);
return ok;
}
if(T)
{if(Vist(T->data))
if(PreOrderTraverse(T->lchild),Vist))
if(PreOrderTraverse(T->rchild),Vist))
return ok;
return ERROR;
}
else return ok;
}
Status InorderTraverse(BiTree T,Status(*Vist) (TElementType e)){
InitStack(S); p=T;
while(p||!StackEmpty(S)){
if((p) {Push(S,p); p=p->lchild;}
else{
Pop(S,p); if(!Vist(p->data))
return ERROR;
p=p->rchild;
}
}
return ok;
}
int main(viod)
{
int ch,e,t;
printf("please input the data:");
Status CreatBiTree(BiTree *T);
scanf("%d",&t);
printf("if you want to use PreOrder,press 1,use the InOder,press 2:");
switch(t){
case 1: Status PreOrderTraverse(BiTree T, Status( *Vist)(TElemType e)) ;break;
case 2: Status InorderTraverse(BiTree T,Status(*Vist) (TElementType e));break;
default:printf("ERROR!");break;
}
getch();
}