實現二叉樹各種遍曆演算法

來源:互聯網
上載者:User

/*exp7-2.cpp*/

#include<stdio.h>
#include<malloc.h>
#define MaxSize 100
typedef char ElemType;
typedef struct node
{
 ElemType data;/*資料元素*/
 struct node *lchild;/*指向左孩子*/
 struct node *rchild;/*指向右孩子*/
}BTNode;
extern void CreateBTNode(BTNode *&b,char *str);/*在algo7-1.cpp檔案中*/
extern void DispBTNode(BTNode *b);
void PreOrder(BTNode *b)/*先序遍曆的遞迴演算法*/
{
 if(b!=NULL)
 {
  printf("%c",b->data);/*訪問根結點*/
  PreOrder(b->lchild);/*遞迴訪問左子樹*/
  PreOrder(b->rchild);/*遞迴訪問右子樹*/
 }
}

void PreOrder1(BTNode *b)/*先序遍曆的非遞迴演算法*/
{
 BTNode *p;
 struct
 {
  BTNode *pt;
  int tag;
 }St[MaxSize];
 int top=-1;
 top++;
 St[top].pt=b;St[top].tag=1;
 while(top>-1)/*棧不空時迴圈*/
 {
  if(St[top].tag==1)/*不能直接存取的情況*/
  {
   p=St[top].pt;
   top--;
   if(p!=NULL)
   {
    top++;/*右孩子進棧*/
    St[top].pt=p->rchild;
    St[top].tag=1;
    top++;/*左孩子進棧*/
    St[top].pt=p->lchild;
    St[top].tag=1;
    top++;/*根結點進棧*/
    St[top].pt=p;
    St[top].tag=0;
   }
  }
  if(St[top].tag==0)/*直接存取的情況*/
  {
   printf("%c",St[top].pt->data);
   top--;
  }

 }
}

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");

 }
}

void InOrder(BTNode *b)/*中序遍曆的遞迴演算法*/
{
 if(b!=NULL)
 {
  InOrder(b->lchild);/*遞迴訪問左子樹*/
  printf("%c",b->data);/*訪問根結點*/
  InOrder(b->rchild);/*遞迴訪問右子樹*/
 }
}

void InOrder1(BTNode *b)/*中序遍曆的非遞迴演算法一*/
{
 BTNode *p;
 struct
 {
  BTNode *pt;
  int tag;
 }St[MaxSize];
 int top=-1;
 top++;
 St[top].pt=b;St[top].tag=1;
 while(top>-1)/*棧不空時迴圈*/
 {
  if(St[top].tag==1)/*不能直接存取的情況*/
  {
   p=St[top].pt;
   top--;
   if(p!=NULL)
   {
    top++;/*右孩子進棧*/
    St[top].pt=p->rchild;
    St[top].tag=1;
    top++;/*根結點進棧*/
    St[top].pt=p;
    St[top].tag=0;
    top++;/*左孩子進棧*/
    St[top].pt=p->lchild;
    St[top].tag=1;
   }
   if(St[top].tag==0)/*直接存取的情況*/
   {
    printf("%c",St[top].pt->data);
    top--;
   }
  }
 }

}

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");
 }
}

void PostOrder(BTNode *b)/*後序遍曆的遞迴演算法*/
{
 if(b!=NULL)
 {
  PostOrder(b->lchild);/*遞迴訪問左子樹*/
  PostOrder(b->rchild);/*遞迴訪問右子樹*/
  printf("^%c",b->data);/*訪問根結點*/
 }
}

void PostOrder1(BTNode *b)/*後序遍曆的非遞迴演算法一*/
{
 BTNode *p;
 struct
 {
  BTNode *pt;
  int tag;
 }St[MaxSize];
 int top=-1;
 top++;
 St[top].pt=b;St[top].tag=1;
 while(top>-1)/*棧不空時迴圈*/
 {
  if(St[top].tag==1)/*不能直接存取的情況*/
  {
   p=St[top].pt;
   top--;
   if(p!=NULL)
   {
    top++;
    St[top].pt=p;/*根結點進棧*/
    St[top].tag=0;
    top++;/*右孩子進棧*/
    St[top].pt=p->rchild;
    St[top].tag=1;
    top++;/*左孩子進棧*/
    St[top].pt=p->lchild;
    St[top].tag=1;
   }
  }
  if(St[top].tag==0)/*直接存取的情況*/
  {
   printf("%c",St[top].pt->data);
   top--;
  }
 }
}

void PostOrder2(BTNode *b)/*後序遍曆的非遞迴演算法二*/
{
 BTNode *St[MaxSize];
 BTNode *p;
 int flag,top=-1;/*棧指標置初值*/
 if(b!=NULL)
 {
  do
  {
   while(b!=NULL)/*將t的所有左結點入棧*/
   {
    top++; 
    St[top]=b;
    b=b->lchild;
   }
   p=NULL;/*p指向當前結點的前一個已訪問的結點*/
   flag=1;/*設定b的訪問標記為已訪問過*/
   while(top!=-1 && flag)
   {
    b=St[top];/*取出當前的棧頂元素*/
    if(b->rchild==p)/*右子樹不存在或已被訪問,訪問之*/
    {
     printf("%c",b->data);/*訪問*b結點*/
     top--;
     p=b;
    }
    else
    {
     b=b->rchild;/*t指向右子樹*/
     flag=0;/*設定未被訪問的標記*/
    }
   }
  }while(top!=-1);
  printf("\n");
 }
}

void TravLevel(BTNode *b)/*層次遍曆*/
{
 BTNode *Qu[MaxSize];/*定義順序迴圈隊列*/
 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");
}

void main()
{
 BTNode *b;
 CreateBTNode(b,"A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))");
 printf("二叉樹b:");DispBTNode(b);printf("\n\n");
 printf("層次遍曆序列:");
 TravLevel(b);
 printf("\n");
 printf("先序遍曆序列:\n");
 printf("  遞迴演算法:");PreOrder(b);printf("\n");
 printf("非遞迴演算法1:");PreOrder1(b);printf("\n");
 printf("非遞迴演算法2:");PreOrder2(b);printf("\n");
 printf("中序遍曆序列:\n");
 printf("  遞迴演算法:");InOrder(b);printf("\n");
 printf("非遞迴演算法1:");InOrder1(b);printf("\n");
 printf("非遞迴演算法2: ");InOrder2(b);printf("\n");
 printf("後序遍曆序列:\n");
 printf("  遞迴演算法: ");PostOrder(b);printf("\n");
 printf("非遞迴演算法1:");PostOrder1(b);printf("\n");
 printf("非遞迴演算法2:");PostOrder2(b);printf("\n");

}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.