遍历二叉树————非递归后续遍历

#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" );
}
 
//BIAOJI
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);
  //   cengci(root);
     PostOrder(root);
     return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值