#include "stdio.h"
#include "malloc.h"
#include "stdlib.h"
#define MAXSIZE 10
#define MAXSIZE_IN_CREMENT 2
typedef struct BiTnode{
char data;
struct BiTnode *lchild,*rchild;
}BiTree;
typedef struct {
BiTree *base;
BiTree *top;
int stacksize;
}SqStack;
void InitStack(SqStack *s){
s->base=(BiTree *)malloc(MAXSIZE*sizeof(BiTree));
if(!s->base)exit(0);
s->top=s->base;
s->stacksize=MAXSIZE;
}
struct BiTnode *CreateBiTree(){
BiTree *T;
char c;
printf("Please enter the element of the BiTree:");
scanf("%c",&c);getchar();
if(c==' ')
T=NULL;
else
{
T=(BiTree*)malloc(sizeof(BiTree));
T->data=c;
T->lchild=CreateBiTree();
T->rchild=CreateBiTree();
}
return T;
}
void push(SqStack *s,BiTree *p){
if(s->top-s->base>=s->stacksize){
s->base=(BiTree *)realloc(s->base,(MAXSIZE+MAXSIZE_IN_CREMENT)*sizeof(BiTree));
if(!s->base)exit(0);
s->top=s->base+s->stacksize;
s->stacksize+=MAXSIZE_IN_CREMENT;}
s->top=p;
s->top++;
}
void pop(SqStack *s){
BiTree *p;
s->top--;
p=s->top;
printf("%c",p->data);
}
void Traverse(BiTree *T,BiTree *p){
SqStack Snode;
InitStack(&Snode);
p=T;
while(1){
if(p!=NULL){push(&Snode,p);p=p->lchild;}
else {
pop(&Snode);
//printf("%c",p->data);
p=p->rchild;
}
}
}
void main()
{
BiTree T,p;
T=*CreateBiTree();
Traverse(&T,&p);
printf("/n");
}