二叉树的非递归遍历请高手检查错误
#include <stdio.h>#define STACKINITSIZE 100
#define STACK 10
#define OK 1
#define ERROR 0
#define OVERFLOW -1
typedef struct
{
char *base;
char *top;
int stacksize;
}Sqstack;
typedef struct BiTNode
{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
char Initstack(Sqstack *S)
{
S->base=(char *)malloc(STACKINITSIZE*sizeof(char));
if(!S->base) exit(OVERFLOW);
S->top=S->base;
S->stacksize=STACKINITSIZE;
return OK;
}
char GetTop(Sqstack S,char *e)
{
if(S.top==S.base) return ERROR;
*e=*S.top-1;
return *e;
}
char Push(Sqstack *S,char *e)
{
if(S->top-S->base>=S->stacksize)
{
S->base=(char *)realloc(S->base,(S->stacksize+STACK)*sizeof(char));
if(!S->base) exit(OVERFLOW);
S->top=S->base+S->stacksize;
S->stacksize+=STACK;
}
*S->top++=*e;
return *e;
}
char Pop(Sqstack *S,char *e)
{
if(S->top==S->base) return ERROR;
*e=*--S->top;
return *e;
}
print(char e)
{
printf("%c",e);
return 1;
}
char Inorder(BiTree T,int(* Visit)(char e))
{
Sqstack S;BiTree p;
Initstack(&S); Push(&S,T);
while(!stackempty(&S))
{
while(GetTop(S,p)&&p) Push(&S,p->lchild);
Pop(&S,p);
if(!stackempty(&S))
{
Pop(&S,p);
if(!Visit(p->data)) return ERROR;
Push(&S,p->rchild);
}
}
}
stackempty(Sqstack *S)
{
if(S->top==S->base) return 1;
else
return 0;
}
char CreateBiTree(BiTree T)
{
char ch;
ch=getchar();
if(ch=='#') T=NULL;
else
{
if(!(T=(BiTNode *)malloc(sizeof(BiTNode)))) exit(OVERFLOW);
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
return OK;
}
main()
{
BiTree S1;
CreateBiTree(&S1);
Inorder(&S1,print);
}