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