#include<stdio.h>
#include<string.h>
#include<windows.h>
#include<stdlib.h>
#include<stack>
#include<iostream>
#define max 500
using namespace std;
typedef struct node
{
char data;
struct node *left ,*right;
}binode,*bitree;
typedef struct
{
bitree num[max];
int top;
}sqstack;
typedef struct kk
{
bitree t;
struct kk *next;
}sqqueue;
typedef struct
{
sqqueue *front,*rear;
}queue;
typedef struct post_y
{
binode *prt;
int flag;
}post_type;
typedef struct LP
{
post_type num[max];
int top1;
}Sqstack;
int initqueue(queue *Q)
{
sqqueue *p;
p=(sqqueue*)malloc(sizeof(sqqueue));
Q->front=p;
Q->rear=p;
(Q->front)->next=NULL;
return 0;
}
int queueempty(queue *Q)
{
if(Q->rear==Q->front)
{
return 1;
}
else
return 0;
}
int queuepush(queue *Q,bitree T)
{
sqqueue *p;
p=(sqqueue*)malloc(sizeof(sqqueue));
p->t=T;
p->next=NULL;
(Q->rear)->next=p;
Q->rear=p;
return 0;
}
bitree GET_top(queue *Q)
{
sqqueue *p;
p=(sqqueue*)malloc(sizeof(sqqueue));
bitree q;
q=(bitree)malloc(sizeof(bitree));
q->data='1';
q->left=NULL;
q->right=NULL;
if(queueempty(Q))
{
return q;
}
else
{
p=(Q->front)->next;
return p->t;
}
}
int queuepop(queue *Q)
{
if(queueempty(Q))
{
return -1;
}
else
{
sqqueue *p;
p=(Q->front)->next;
(Q->front)->next=p->next;
if(p->next==NULL)
Q->rear=Q->front;
free(p);
return 0;
}
}
void initstack(sqstack *T)
{
T->top=-1;
}
int empty(sqstack T)
{
if(T.top==-1)
{
return -1;
}
return 0;
}
bitree get_top(sqstack S)
{
return S.num[S.top];
}
int full(sqstack S)
{
if(S.top==max-1)
{
return 1;
}
return 0;
}
int push(sqstack S,bitree e)
{
if(full(S))
{
return 0;
}
S.top++;
S.num[S.top]=e;
return 0;
}
int pop(sqstack S)
{
if(!empty(S))
{
return -1;
}
S.top--;
return 0;
}
void creattree(bitree &T)
{
char s;
scanf("%c",&s);
if(s=='#')
{
T=NULL;
}
else
{
T=(bitree)malloc(sizeof(binode));
T->data=s;
creattree(T->left);
creattree(T->right);
}
}
void visit(bitree T)
{
if(T!=NULL)
{
printf("%c ",T->data);
}
}
void firstvisit(bitree T)//递归实现先序遍历
{
if(NULL!=T)
{
visit(T);
firstvisit(T->left);
firstvisit(T->right);
}
}
void midvisit(bitree T)//递归实现中序遍历
{
if(T!=NULL)
{
midvisit(T->left);
visit(T);
midvisit(T->right);
}
}
void lastvisit(bitree T)//递归实现后序遍历
{
if(T!=NULL)
{
lastvisit(T->left);
lastvisit(T->right);
visit(T);
}
}
void post_visit(bitree T)
{
Sqstack S;
bitree p=NULL;
post_type elem;
S.top1=-1;
p=T;
while(NULL!=p||S.top1>-1)
{
while(NULL!=p)
{
elem.prt=p;
elem.flag=1;
S.num[++S.top1]=elem;
p=p->left;
}
elem=S.num[S.top1--];
if(1==elem.flag)
{
elem.flag=2;
S.num[++S.top1]=elem;
p=elem.prt->right;
}
else if(elem.flag==2)
{
p=elem.prt;
printf("%c ",p->data);
p=NULL;
}
}
}
void previsit(bitree T)
{
binode *p;
p=NULL;
sqstack S;
S.top=-1;
if(NULL!=T)
{
S.num[++S.top]=T;
while(S.top>-1)
{
p=S.num[S.top--];
printf("%c ",p->data);
if(NULL!=p->right)
{
S.num[++S.top]=p->right;
}
if(NULL!=p->left)
{
S.num[++S.top]=p->left;
}
}
}
}
void inorder(bitree T)
{
sqstack S;
S.top=-1;
binode *p=T;
while(p||S.top>-1)
{
while(NULL!=p)
{
S.num[++S.top]=p;
p=p->left;
}
p=S.num[S.top--];
printf("%c ",p->data);
p=p->right;
}
}
void levelvisit(bitree T)
{
queue *Q;
bitree p=T;
initqueue(Q);
queuepush(Q,T);
while(!queueempty(Q))
{
p=GET_top(Q);
queuepop(Q);
printf("%c ",p->data);
if(p->left!=NULL) queuepush(Q,p->left);
if(p->right!=NULL) queuepush(Q,p->right);
}
}
void menu()
{
printf(" 选择遍历方式
");
printf(" 1:先序遍历
");
printf(" 2:中序遍历
");
printf(" 3:后序遍历
");
printf(" 4:层次遍历
");
}
int main()
{
int n;
printf(" 手动输入测试数据
");
printf("输入节点,其中#表示空子树
");
bitree T;
creattree(T);
menu();
while(scanf("%d",&n)!=EOF)
{
switch(n)
{
case 1:
system("cls");
printf("非递归先序遍历得到的序列
");
previsit(T);
printf("
");
printf("递归先序遍历得到的序列
");
firstvisit(T) ;
printf("
");
Sleep(10000);
system("cls");
menu();
break;
case 2:
system("cls");
printf("非递归中序遍历得到的结果
");
inorder(T);
printf("
");
printf("递归中序遍历得到的序列
");
midvisit(T);
printf("
");
Sleep(100000);
system("cls");
menu();
break;
case 3:
system("cls");
printf("递归后序遍历得到的序列
");
lastvisit(T);
printf("
");
printf("非递归后序遍历的到的序列
");
post_visit(T);
printf("
");
Sleep(10000);
system("cls");
menu();
break;
case 4:
system("cls");
printf("层次遍历得到的序列
");
levelvisit(T);
printf("
");
Sleep(10000);
system("cls");
menu();
break;
}
}
return 0;
}
本次测试使用的数据
ABC##DE#G##F###
树的形态