#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LIST_INIT_SIZE 10
#define LISTINCREMENT 100
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define TRUE 1
#define FALSE 0
#define true 1
#define false 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define OPSETSIZE 7
#define MAXQSIZE 100
typedef char TelemType;
typedef int Status;
typedef struct BiTNode
{
TelemType data;
struct BiTNode *lchild, *rchild;
} BiTNode, *BiTree;
typedef BiTree SElemType;
typedef struct
{
SElemType *base;
SElemType *top;
int stacksize;
} SqStack;
Status InitStack(SqStack *S);
Status Push(SqStack *S, SElemType e);
Status Pop(SqStack *S, SElemType *e);
Status StackEmpty(SqStack S);
Status CreateBiTree(BiTree *T);
Status PrintElement(TelemType e);
Status visit(TelemType e);
Status PreorderTraverse(BiTree T, Status (*visit)(TelemType e));
Status InorderTraverse(BiTree T, Status (*visit)(TelemType e));
Status PostorderTraverse(BiTree T, Status (*visit)(TelemType e));
int main()
{
BiTree T;
CreateBiTree(&T);
printf("PreorderTraverse:");
PreorderTraverse(T, visit);
printf("
InorderTraverse_1:");
InorderTraverse(T, visit);
printf("
InorderTraverse_2:");
InorderTraverse2(T, visit);
printf("
PostorderTraverse:");
PostorderTraverse(T, visit);
return 0;
}
Status InitStack(SqStack *S)
{
S->base = (SElemType *)malloc(STACK_INIT_SIZE*sizeof (SElemType));
if (!S->base) exit (OVERFLOW);
S->top = S->base;
S->stacksize = STACK_INIT_SIZE;
return OK;
}
Status Push(SqStack *S, SElemType e)
{
if (S->top - S->base >= S->stacksize) //栈满
{
S->base = (SElemType *)realloc
(S->base, (S->stacksize + STACKINCREMENT)
* sizeof(SElemType));
if (!S->base) exit (OVERFLOW);
S->top = S->base + S->stacksize;
S->stacksize += STACKINCREMENT;
} // if
*S->top++ = e;
return OK;
} //Push
Status Pop(SqStack *S, SElemType *e)
{
if(S->top == S->base)return ERROR;
*e = *--S->top;
return OK;
} //Pop
Status StackEmpty(SqStack S)
{
if (S.base == S.top)
return TRUE;
return FALSE;
}
Status 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;
}
Status PrintElement(TelemType e)
{
printf("%c ", e);
return OK;
}
Status visit(TelemType e)
{
printf("%c ", e);
return OK;
}
Status PreorderTraverse(BiTree T, Status (*visit)(TelemType e))
{
if (T)
{
if (visit(T->data))
if (PreorderTraverse(T->lchild, visit))
if (PreorderTraverse(T->rchild, visit)) return OK;
return ERROR;
}
return OK;
}
Status InorderTraverse(BiTree T, Status (*visit)(TelemType e) )
{
if (T)
{
if (InorderTraverse(T->lchild, visit))
if (visit(T->data))
if (InorderTraverse(T->rchild, visit)) return OK;
return ERROR;
}
return OK;
}
Status InorderTraverse2(BiTree T, Status (*visit)(TelemType e))
{
SqStack S;
InitStack(&S);
BiTree p = T;
while( p || !StackEmpty(S) )
{
while (p)
{
Push(&S, p);
p = p -> lchild;
}
if( !StackEmpty(S))
{
Pop(&S, &p);
if (!visit(p -> data))
return ERROR;
p = p -> rchild;
}
}
return OK;
}
Status PostorderTraverse(BiTree T, Status (*visit)(TelemType e))
{
if (T)
{
if (PostorderTraverse(T->lchild, visit))
if (PostorderTraverse(T->rchild, visit))
if (visit(T->data)) return OK;
return ERROR;
}
return OK;
}