急,帮忙看看哪里错了?重分悬赏...
#define STACK_INIT_SIZE 100#define STACKINCREMENT 10
#include <stdio.h>
#include <stdlib.h>
typedef char TElemType;
typedef struct BiTNode
{
TElemType data;
struct BiTNode *lchild,*rchild;//
}BiTNode,*BiTree;
typedef struct
{
BiTree *base;
BiTree *top;
int stacksize;
}SqStack;
void initialStack(SqStack *s)
{
s->base=(BiTree *)malloc(STACK_INIT_SIZE*sizeof(BiTree));
if(!s->base) exit(0);
s->top=s->base;
s->stacksize=STACK_INIT_SIZE;
}
void Push(SqStack *s,BiTNode *e)
{
if(s->top-s->base>=s->stacksize)
{
s->base=(BiTree *)realloc(s->base,(STACK_INIT_SIZE+STACKINCREMENT)*sizeof(BiTree));
if(!s->base) exit(0);
s->top=s->base+s->stacksize;
s->stacksize+=STACKINCREMENT;
}
*(s->top)++=e;
}
void Pop(SqStack *s,BiTNode *e)
{
if(s->top==s->base) exit(0);
e=*--(s->top);
}
int GetTop(SqStack *s,BiTNode *e)
{
if(s->top==s->base) return(0);
e=*(s->top-1);
return(1);
}
int StackEmpty(SqStack *s)
{
if(s->base==s->top)
return(1);
else
return(0);
}
BiTree CreatBiTree()//先序建立二叉链表
{
TElemType ch;
BiTree T;
scanf("%c",&ch);
if(ch==' ')
T=NULL;
else
{
T=(BiTree)malloc(sizeof(BiTNode));
if(!T)
exit(0);
T->data=ch;
T->lchild=CreatBiTree();
T->rchild=CreatBiTree();
}
return T;
}
void Visit(BiTree p)//对数据元素进行操作的Visit函数
{
printf("%c",p->data);
}
void InOrderTraverse(BiTree T)//非递归算法中序遍历二叉树链表
{
BiTree p;
SqStack S;
initialStack(&S);
p=T;
Push(&S,p);//将根结点指针压入栈中
while(!StackEmpty(&S))
{
while(GetTop(&S,p)&&(p!=NULL))
Push(&S,p->lchild);//从左走到尽
Pop(&S,p);//空指针退栈
if(!StackEmpty(&S))//访问结点,向右一步
{
Pop(&S,p);
Visit(p);//访问该结点
Push(&S,p->rchild);//将刚才访问的结点的右儿子结点的地址压入栈中
}
}
}
void main()
{
BiTree t;
printf("请按先序遍历序列输入二叉树中各个结点的值(字符),若为空树时输入空格键:\n");
t=CreatBiTree();
printf("[中序遍历]该二叉树的各个结点:\n");
InOrderTraverse(t);
}
//测试值abc@@de@g@@f@@@输入时用空格键替换@)
//递归算法先序建立二叉树二叉链表,再用非递归算法的中序遍历算法访问二叉树的各个结点