二叉树遍历非递归遍历不出来?求解决
#include<stdio.h>#include<malloc.h>
#include<stdlib.h>
#define ERROR 0
#define OK 1
#define int_size 100
#define incre_size 10
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef char TElemType;
typedef struct BiTNode
{
TElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
typedef BiTree SElemType;
typedef struct
{
SElemType *top;
SElemType *base;
int stacksize;
}SqStack;
Status IntiStack(SqStack *S)
{
S->base=(SElemType *)malloc(int_size*sizeof(SElemType));
if(!S->base)
exit(ERROR);
S->top=S->base;
S->stacksize=int_size;
return OK;
}
Status Push(SqStack *S,SElemType e)
{
if(S->top-S->base>=int_size)
{
S->base=(SElemType *)realloc(S->base,(S->stacksize+incre_size)*sizeof(SElemType));
if(!S->base)
exit(ERROR);
S->top=S->base+S->stacksize;
S->stacksize+=incre_size;
}
*(S->top)++=e;
return OK;
}
Status Pop(SqStack *S,SElemType &e)
{
if(S->top==S->base)
return ERROR;
e=*--S->top;
return OK;
}
void ClearStack(SqStack *S)
{
S->top=S->base;
}
void DestroyStack(SqStack *S)
{
free(S->base);
S->top=S->base=NULL;
S->stacksize=0;
}
Status StackEmpty(SqStack *S)
{
if(S->top==S->base)
return TRUE;
else
return FALSE;
}
Status GetTop(SqStack *S,SElemType &e)
{
if(S->top>S->base)
{
e=*--S->top;
return OK;
}
else
return ERROR;
}
//以上是栈的基本操作
BiTree CreateTree()
{
char ch;BiTree T;
ch=getchar();
if(ch=='#')
{
return NULL;
}
else
{
T=(BiTree)malloc(sizeof(BiTNode));
T->data=ch;
T->lchild=CreateTree();
T->rchild=CreateTree();
}
return T;
}
void CreateTree1(BiTree *T)
{
TElemType ch;
ch=getchar();
if(ch=='#')
{
(*T)=NULL;
}
else
{
(*T)=(BiTree)malloc(sizeof(BiTNode));
(*T)->data=ch;
CreateTree1(&((*T)->lchild));
CreateTree1(&((*T)->rchild));
}
}
void InOrder(BiTree T)
{
if(T!=NULL)
{
InOrder(T->lchild);
printf("%c ",T->data);
InOrder(T->rchild);
}
}
void visit(TElemType e)
{
printf("%c ",e);
}
//非递归的中序遍历结果
void InOrderTravese(BiTree T,void(*Visit)(TElemType))
{
SqStack S;BiTree p;
IntiStack(&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);
Visit(p->data);
Push(&S,p->rchild);
}
}
printf("\n");
DestroyStack(&S);
}
int main()
{
BiTree T;
printf("请输入二叉树的节点信息:\n");
T=CreateTree();
//CreateTree1(&T);
printf("中序遍历的结果为:\n");
InOrder(T);
printf("\n");
printf("中序非递归为:\n");
InOrderTravese(T,visit);
return 0;
}
中序递归遍历有结果,非递归没有结果?