#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define max 1000
typedef
char
datatype;
typedef
struct
Node
{
datatype data;
struct
Node *lchild;
struct
Node *rchild;
}BiTNode, *BiTree;
typedef
struct
{
BiTree da[1000];
int
top;
}SeqStack;
typedef
struct
{
BiTree da[max];
int
front;
int
rear;
}SeqQueue;
SeqQueue *InitQueue()
{
SeqQueue *q;
q = (SeqQueue *)
malloc
(
sizeof
(SeqQueue));
q->front = q->rear = max-1;
return
q;
}
int
IsEmpty(SeqQueue *q)
{
if
(q->front == q->rear)
return
1;
else
return
0;
}
void
EnterQueue(SeqQueue *q,BiTree root)
{
BiTree p;
p = root;
if
((q->rear+1) % max == q->front) {
return
;
}
else
{
q -> rear = (q->rear+1) % max;
q->da[q->rear] = p;
}
}
void
out(SeqQueue *q, BiTree *root)
{
if
(q->front == q->rear)
return
;
else
{
q->front = (q->front+1)%max;
*root = q->da[q->front];
}
}
void
creat(BiTree *root)
{
char
ch;
ch =
getchar
();
if
(ch ==
'#'
) {
*root = NULL;
}
else
{
*root = (BiTree)
malloc
(
sizeof
(BiTNode));
(*root)->data = ch;
creat(&(*root)->lchild);
creat(&(*root)->rchild);
}
}
void
cengci(BiTree root)
{
SeqQueue *s;
BiTree p;
s=InitQueue();
EnterQueue(s,root);
while
(!IsEmpty(s)) {
out(s,&p);
printf
(
"%c"
,p->data);
if
(p->lchild != NULL)
EnterQueue(s,p->lchild);
if
(p->rchild != NULL)
EnterQueue(s,p->rchild);
}
printf
(
"\n"
);
}
void
getTop(SeqStack *s, BiTree *p)
{
if
(Empty(s))
return
;
else
*p = s->da[s->top];
}
void
push(SeqStack *s, BiTree root)
{
BiTree p;
p = root;
if
(s->top == 999)
return
;
else
{
s->top++;
s->da[s->top]=p;
}
}
SeqStack *InitStack()
{
SeqStack *s;
s=(SeqStack *)
malloc
(
sizeof
(SeqStack));
s->top = -1;
return
s;
}
int
Empty(SeqStack *s)
{
if
(s->top == -1)
return
1;
else
return
0;
}
void
pop(SeqStack *s, BiTree *root)
{
BiTree p;
p = *root;
if
(Empty(s))
return
;
else
{
*root = s-> da[s->top];
s->top--;
}
}
void
PostOrder(BiTree root)
{
SeqStack *s;
BiTree p,q;
s=InitStack();
p = root;
q = NULL;
while
(p != NULL || !Empty(s)) {
while
(p != NULL) {
push(s,p);
p = p -> lchild;
}
if
(!Empty(s)) {
getTop(s,&p);
if
(p->rchild==NULL || p->rchild == q) {
pop(s,&p);
printf
(
"%c"
,p->data);
q=p;
p=NULL;
}
else
p = p -> rchild;
}
}
}
int
main()
{
BiTree root;
creat(&root);
PostOrder(root);
return
0;
}