二叉树的非递归遍历的算法


#include<iostream.h>
#include "type.h"


void  main()

   BiTree  T;
 
   CreateBiTree(T);//创建二叉树
   cout<<"中序遍历为:"<<endl;
   InorderTraverse(T);//中序遍历
   cout<<"后序遍历为:"<<endl;
   InorderBack(T);//后序遍历
   cout<<"层次遍历二叉树"<<endl;
   Order(T);
   cout<<endl;

#include "type.h"


void Initstack(SqStack &s)
{s.base=(SElemType *)malloc(MAXSIZE*sizeof(SElemType));
 if(!s.base)
  return;
 s.top=s.base;
 s.stacksize=MAXSIZE;
}
void Push(SqStack &s,SElemType e)
{if(s.top-s.base>=s.stacksize) {
 s.base=(SElemType *)realloc(s.base,(s.stacksize+INCRESIZE)*sizeof(SElemType));
 if(!s.base)  return;
 s.top=s.base+s.stacksize;
 s.stacksize+=INCRESIZE;
}
*s.top++=e;
}
void Pop(SqStack &s,SElemType &e)
{ if(s.top==s.base)
     return;
  e=*--s.top;
}

SElemType GetTop(SqStack s)
{SElemType e;
 
 e=*(s.top-1);
 return e;
}

int i=0;
void    CreateBiTree(BiTree &T)
{
   
 
 char  ch[]="-+a  *b  -c  d  /e  f  ";
 
 if(ch[i]==' ')   {T=NULL;i++;}
  else
  {
           if(!(T=(BiTNode *)malloc(sizeof(BiTNode))))
        exit (OVERFLOW);
        T->data=ch[i++];
           CreateBiTree(T->lchild); 
        CreateBiTree(T->rchild);
  }
    
}


void  InorderTraverse(BiTree T)
{  
 SqStack  s;
    SElemType e; 
    e.ptr=T;
 Initstack(s);
 while(e.ptr||!StackEmpty(s))
 
 {
  if(e.ptr) 
  {
  
   Push(s,e);
      e.ptr=e.ptr->lchild;

  }
  else {
   Pop(s,e);
   cout<<e.ptr->data;
   e.ptr=e.ptr->rchild;
 
  }
 
 }
 cout<<endl;
}

 

Status  StackEmpty(SqStack s)
{ if(s.base==s.top)
  return 1;
  else return 0;
}


void  InorderBack(BiTree T)
{   SElemType  e;
    int  m=1,n=1;
 SqStack  sb;
    Initstack(sb);
        e.ptr=T->lchild;
       
  while(m&&(e.ptr||!StackEmpty(sb)))
 
  {
      if(e.ptr)
   {Push(sb,e);   e.ptr=e.ptr->lchild;}
       else
    { Pop(sb,e);
      cout<<e.ptr->data;
         e=GetTop(sb);
         e.ptr=e.ptr->rchild;
   if(e.ptr->lchild==NULL)
   {m=0;  cout<<e.ptr->data;}
          }
 
    }
        while(!StackEmpty(sb))
  {Pop(sb,e);
   cout<<e.ptr->data;
  }
       
      
  
  
  e.ptr=T->rchild;

  while(n&&(e.ptr||!StackEmpty(sb)))
 
  {
      if(e.ptr)
   {Push(sb,e);   e.ptr=e.ptr->lchild;}
       else
    { Pop(sb,e);
      cout<<e.ptr->data;
         e=GetTop(sb);
         e.ptr=e.ptr->rchild;
   if(e.ptr->lchild==NULL)
   {n=0;  cout<<e.ptr->data;}
          }
 
    }
        while(!StackEmpty(sb))
  {Pop(sb,e);
   cout<<e.ptr->data;
  }
       cout<<T->data;
    cout<<endl;
}

 

void InitQueue(LinkQueue &Q)
{ Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
if(!Q.front)  exit(OVERFLOW);
Q.front->next=NULL;


}

void EnQueue(LinkQueue  &Q,QElemType e)
{  QueuePtr p;
   p=(QueuePtr)malloc(sizeof(QNode));
   if(!p)  exit(OVERFLOW);
   p->data=e;
   p->next=NULL;
   Q.rear->next=p;
   Q.rear=p;
  


void GetHead(LinkQueue Q,QElemType &e)

{   if(Q.front==Q.rear)   return ;
    e=Q.rear->data;
 
}


void  DeQueue(LinkQueue &Q,QElemType &e)
{    QueuePtr  p;
 if(Q.front==Q.rear)   return;
    p=Q.front->next;
 e=p->data;
 Q.front->next=p->next;
 if(Q.rear==p)  Q.rear=Q.front;
 free(p);


}


Status  Empity(LinkQueue Q)
{if  (Q.rear==Q.front)
     return 1;
    else return 0;


}

 

void  Order(BiTree T)
{  QElemType  e,exp;
   LinkQueue  Q;
 
   InitQueue(Q);
   e.p=T;
   EnQueue(Q,e);
   while(!Empity(Q))
   { 
     DeQueue(Q,e);
     cout<<e.p->data;
     exp.p=e.p;
  if(e.p->lchild)
  {
    e.p=e.p->lchild;
    EnQueue(Q,e);
     
  }
     if(exp.p->rchild)
  {
    exp.p=exp.p->rchild;
    EnQueue(Q,exp);
      
  } 
  
  
   }
    

}

 

#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

#define FALSE 0
#define TRUE 1
#define ERROR 0
#define OK 1

#define MAXSIZE 20
#define INCRESIZE 10

typedef   int Status;  

 

 

 

typedef  struct BiTNode{
   char  data;
   struct BiTNode  *lchild,*rchild;
}BiTNode,*BiTree;


typedef  struct {
   BiTree  ptr;
   int task;

}SElemType;

typedef struct {
 SElemType *base;
 SElemType *top;
 int stacksize;
}SqStack;

typedef struct{
  BiTree p;
  int task;

}QElemType;

typedef struct QNode
{  QElemType  data;
   struct QNode *next;
}QNode, *QueuePtr;
  typedef struct
  {  QueuePtr  front;
     QueuePtr  rear;
  }LinkQueue;

 


void Initstack(SqStack &);
void Push(SqStack &,SElemType);
void Pop(SqStack &,SElemType &);
SElemType GetTop(SqStack);
void   CreateBiTree(BiTree &T);
void  InorderTraverse(BiTree T);
Status  StackEmpty(SqStack s);
void  InorderBack(BiTree T);

 

void  InitQueue(LinkQueue &Q);
void EnQueue(LinkQueue  &Q,QElemType e);
void GetHead(LinkQueue Q,QElemType &e);
void  DeQueue(LinkQueue &Q,QElemType &e);
Status  Empity(LinkQueue Q);
void  Order(BiTree T);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值