definition.h
#define INIT_SIZE 100
#define INCREMENT 10
typedef char TElemType;
typedef struct{
TElemType data;
struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;
typedef struct{
BiTree *top, *base;
unsigned stasksize;
}SqStask;
BiTree CreateBiTree(void);
void PreOrderTraverse(BiTree);
int InOrderTraver(BiTree);
int InOrderTraverse2(BiTree);
int Initstack(SqStack *);
int Push{SqStack *, BiTree); */
main.c
#include (stdio.h)
#include "definition.h"
int main()
{ BiTree bt;
print("shururoot; ");
bt = CreateBiTree();
PreOrderTraver(bt);
print("\n");
if( InOrderTraverse(bt) )
return 1;
print("\n");
if( InOrderTravwese2(bt) )
return 1;
print("\n");
return 0;
}
function.c
#include (stdio.h)
#include (malloc.h)
#include "definition.h"
BiTree CreateBiTree(void)
{ TElemType e;
BiTree tem = NULL;
if( (e=getchar())!='#'){
getchar();
tem = (BiTree)malloc(sizeof(BiTNode));
if(!tmp)
return NULL;
tmp->data=e;
printf("shuruleftchild; ");
tmp->lchild=CreateBiTree();
printf("shururightchild; ");
tmp->rchild=CreateBiTree();
}
else
getchar();
return tmp;
}
void PreOrderTraverse(BiTree bt)
{ if(bt){
printf("%c", bt->data);
PreOrderTraverse(bt->lchild);
PreOrderTraverse(bt->rchild);
}
}
int InOrderTraverse(BiTree bt)
{ SqStack S;
BiTree tmp;
if( InitStack(&s) )
return 1;
if( Push(&S, bt) )
return 1;
while(S.base!=S.top){
while( tmp=*(S.top-1) ){
if(Push(&S, tmp->lchild) )
return 1;
}
--S.top;
if(S.base!=S.top){
tmp = *(--S.top);
printf("%c", tmp->data);
if( Push(&S, tmp->rchild) )
return 1;
}
}
free(S.base);
return 0;
}
int InOrderTraverse2(BiTree bt)
{ SqStack S;
BiTree tmp = bt;
if( InitStack(&s) )
return 1;
while( tmp(S.base!=S.top) ){
if((tmp){
if( Push(&S, tmp) )
return 1;
tmp = tmp->lchild;
}
else{
tmp = *(--S.top);
printf("%c", tmp->data);
tmp = tmp->rchild;
}
}
free(S.base);
return 0;
}
int InitStack(SqStack *S)
{ S->base=(BiTree *)malloc( INIT_SIZE * sizeof(BiTree) );
if(!S->base)
return 1;
S->top=S->base;
S->stacksize=INIT_SIZE;
return 0;
}
int Push(SqStack *S,BiTree T)
{ if( (unsigned)(S->top - S->base) >=S->stacksize){
S->base=(BiTree *)realloc(S->base,(S->stacksize+INCREMENT)*sizeof(BiTree) );
if(!S->base)
return 1;
S->top = S->base + S->stacksize;
S->stacksize += INCREMENT;
}
*S->top++ = T;
return 0;
}