二叉树2:层次遍历方式及先序、中序、后序(递归与非递归)遍历方式

#include<stdio.h>
#include<malloc.h>
#include<math.h>
#define MaxSize 100
typedef char ElemType;

typedef struct node
{
 ElemType data;
 struct node *lchild;
 struct node *rchild;
}BTNode;

// Create BinaryTree by Binary-linked-list 
BTNode *CreatBTNode(char *str)
{
 BTNode *St[MaxSize],*p=NULL,*b;
 int top=-1,k,j=0;
 char ch;
 b=NULL;
 ch=str[j];
 while (ch!='\0')
 {
  switch(ch)
  {
  case '(':
   top++;
   St[top]=p;
   k=1;
   break;
  case ')':
   top--;
   break;
  case',':
   k=2;
   break;
  default:
   p=(BTNode*)malloc(sizeof(BTNode));
   p->data=ch;
   p->lchild=p->rchild=NULL;
   if(b==NULL)
    b=p;
   else
   {
    switch(k)
    {
    case 1:
     St[top]->lchild=p;
     break;
    case 2:
     St[top]->rchild=p;
     break;
    }
   }
  }
  j++;
  ch=str[j];
 }
 return b;
}

// output Binary Tree by Tree model
void DispBTNode(BTNode *b)
{
 BTNode *St[MaxSize],*p;
 int K[MaxSize];
 int i,c,top=-1;
 int a=-1;
 int n=3;
 p=b;
 do 
 {
  while(p!=NULL)
  {
   St[++top]=p;
   if(p!=b)
    ++n;
   K[++a]=n;
   p=p->rchild;
  }
  p=St[top--];
  c=K[a--];
  n=c;
  for(i=0;i<=c;i++)
   printf("      ");
  printf("----%c\n",p->data);
  p=p->lchild;
 } while (top>-1 || p!=NULL);
}

// traversal based on level
void TravLevel(BTNode *b)
{
 BTNode *Qu[MaxSize]; // Circular-queue
 int front,rear;
 front=rear=0;
 if(b!=NULL)
  printf("%c",b->data);
 rear++;
 Qu[rear]=b;
 while(rear!=front)
 {
  front=(front+1)%MaxSize;
  b=Qu[front];
  if(b->lchild!=NULL)
  {
   printf("%c",b->lchild->data);
   rear=(rear+1)%MaxSize;
   Qu[rear]=b->lchild;
  }
  if(b->rchild!=NULL)
  {
   printf("%c",b->rchild->data);
   rear=(rear+1)%MaxSize;
   Qu[rear]=b->rchild;
  }
 }
 printf("\n");
}
 // traversal based on PreOrder 
 //   :recursive 
void PreOrder(BTNode *b)
{
 if(b!=NULL)
 {
  printf("%c",b->data);
  PreOrder(b->lchild);
  PreOrder(b->rchild);
 }
}
 
 // not recursive
 void PreOrder2(BTNode *b)
 {
  BTNode *St[MaxSize],*p;
  int top=-1;
  if(b!=NULL)
  {
   top++;
   St[top]=b;
   while(top>-1)
   {
    p=St[top];
    top--;
    printf("%c",p->data);
    if(p->rchild!=NULL)
    {
     ++top;
     St[top]=p->rchild;
    }
     if(p->lchild!=NULL)
     {
      ++top;
      St[top]=p->lchild;
     }
   }
  }
  printf("\n");
 }
 
 // In-Order traversal
 //  :recursive
 void InOrder(BTNode *b)
 {
  if(b!=NULL)
  {
   InOrder(b->rchild);
   printf("%c",b->data);
   InOrder(b->lchild);
  }
 }

 // not recursive 
 void InOrder2(BTNode *b)
 {
  BTNode *St[MaxSize],*p;
  int top=-1;
  if(b!=NULL)
  {
   p=b;
   while(top>-1||p!=NULL)
   {
    while(p!=NULL)
    {
     top++;
     St[top]=p;
     p=p->lchild;
    }
    if(top>-1)
    {
     p=St[top];
     top--;
     printf("%c",p->data);
     p=p->rchild;
    }
   }
  }
  printf("\n");
 }

 // traversal based on PostOrder
 // :recursive
 void PostOrder(BTNode *b)
 {
  if(b!=NULL)
  {
   PostOrder(b->lchild);
   PostOrder(b->rchild);
   printf("%c",b->data);
  }
 }
 // not recursive :used tags
 void PostOrder1(BTNode *b)
 {
  BTNode *St[MaxSize];
  BTNode *p;
  int flag,top=-1;
  if(b!=NULL)
  {
   do 
   {
    while(b!=NULL)
    {
     top++;
     St[top]=b;
     b=b->lchild;
    }
    p=NULL;
    flag=1;
    while(top!=-1 && flag)
    {
     b=St[top];
     if(b->rchild==p)
     {
      printf("%c",b->data);
      top--;
      p=b;
     }
     else // return above to traversal left subtree of b
     {
      b=b->rchild;
      flag=0;
     }
    }
   }while(top!=-1);
   printf("\n");
  }
 }

 // using the struct-stack to accomplish Post-Traversal
 void PostOrder2(BTNode *b)
 {
  BTNode *p;
  struct 
  {
   BTNode *q;
   int tag;
  }St[MaxSize];
  int top=0;
  p=b;
  do 
  {
   while(p!=NULL)
   {
    top++;
    St[top].q=p;
    St[top].tag=0;
    p=p->lchild;
   }
   if(top>0)
   {
    p=St[top].q;
    if(St[top].tag==1)
    {
     printf("%c",p->data);
     top--;
     p=NULL;
    }
    else
    {
     St[top].tag=1;
     p=p->rchild;
    }
   }
  } while (p!=NULL||top!=0);
 }
 void main()
 {
  BTNode *b;
  b=CreatBTNode("A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))");
  printf("Binary Tree:b");
  DispBTNode(b);
  printf("\n\n");
  printf("traversal based on level:");
  TravLevel(b);
  printf("\n");
  printf("traversal based on PreOrder:\n");
  printf("recursive algorithm:");
  PreOrder(b);
  printf("\n");
  printf("not recursive algorithm:");
  PreOrder2(b);
  printf("\n");
  printf("traversal based on InOrder:\n");
  printf("recursive algorithm:");
  InOrder(b);
  printf("\n");
  printf("not recursive:");
  InOrder2(b);
  printf("\n");
  printf("traversal based on PostOrder:");
  printf("recursive algorithm:");
  PostOrder(b);
  printf("\n");
  printf("not recursive algorithm 1:");
  PostOrder1(b);
  printf("not recursive algorithm 2:");
  PostOrder2(b);
  printf("\n");
 
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值