#include<stdio.h >
#include<stdlib.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
int flag;
int t,sum;
typedef struct BiTNode
{
char data;
struct BiTNode *lchild;
struct BiTNode *rchild;
}*BTree;
#include<stdlib.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
int flag;
int t,sum;
typedef struct BiTNode
{
char data;
struct BiTNode *lchild;
struct BiTNode *rchild;
}*BTree;
struct QNote
{
BTree date;
};
{
BTree date;
};
typedef struct
{
BTree *base;
BTree *top;
int stacksize;
}SqStack;
int InitStack(SqStack &S);
int pop(SqStack &S,BTree &e);
int GetTop(SqStack S,BTree &e);
int push(SqStack &S,BTree n);
int StackEmpty(SqStack S);
int DestroyStack(SqStack &S);
int CreateBTree(BTree &T);
int PreTraverseBTree(BTree T);
int InTraverseBTree(BTree T);
int PostTraverseBTree(BTree T);
int FindTraverseBTree(BTree T);
int LevelTraverseBTree(BTree T);
int InTraverseBTree2(BTree T);
void show();
int main()
{ BTree T;
int n;
show();
do
{
scanf("%d",&n);
switch(n)
{
case 1:{
printf("以先序序列输入创建二叉树,每个空链接都要设置成空格或'$'符号\n");//注意二叉树的结束是两个孩子都为空,输出时候要注意
CreateBTree(T);
break;
}
case 2:{
if(flag)
{
printf("前序遍历结果是:\n");
PreTraverseBTree(T);
printf("\n");
}
else
printf("请先创建二叉树!\n");
break;
}
case 3:{
if(flag)
{
printf("中序递归遍历结果是:\n");
InTraverseBTree(T);
printf("\n");
}
else
printf("请先创建二叉树!\n");
break;
}
case 4:{
if(flag)
{printf("中序非递归遍历结果是:\n");
InTraverseBTree2(T);
printf("\n");
}
else
printf("请先创建二叉树!\n");
break;
}
case 5:{
if(flag)
{printf("后序遍历结果是:\n");
PostTraverseBTree(T);
printf("\n");
}
else
printf("请先创建二叉树!\n");
break;
}
case 6:{
if(flag)
{printf("层次遍历结果是:\n");
LevelTraverseBTree(T);
printf("\n");
}
else
printf("请先创建二叉树!\n");
break;
}
case 7:{
if(flag)
{
t=0;
sum=0;
FindTraverseBTree(T);
printf("深度是:%d\n",t);
}
else
printf("请先创建二叉树!\n");
break;
}
case 8:{
exit(0);
printf("退出成功!");
break;
}
default :{
printf("操作不合法!\n");
break;
}
}
printf("请继续:\n");
}while(1);
return 0;
}
void show()
{
printf("可执行操作有:\n");
printf("***********************************************************************\n");
printf("********* 1.创建二叉树 *********\n");
printf("********* 2.先序遍历二叉树 *********\n");
printf("********* 3.中序遍历二叉树1 *********\n");
printf("********* 4.中序遍历二叉树2 *********\n");
printf("********* 5.后续遍历二叉树 *********\n");
printf("********* 6.层序遍历二叉树 *********\n");
printf("********* 7.求二叉树的深度 *********\n");
printf("********* 8.退出 *********\n");
printf("***********************************************************************\n");
}
int InitStack(SqStack &S)
{
S.base=(BTree*)malloc(LIST_INIT_SIZE*sizeof(BTree));
if(!S.base)
{
printf("错误!\n");
return ERROR;
}
S.top=S.base;
S.stacksize=LIST_INIT_SIZE;
return OK;
}
int pop(SqStack &S,BTree &e)
{
if(S.top==S.base)
{
return ERROR;
}
e=*--S.top;
return OK;
}
int GetTop(SqStack S,BTree &e)
{
if(S.top==S.base)
{
return ERROR;
}
e=*(--S.top);
return OK;
}
int push(SqStack &S,BTree n)
{
if(S.top-S.base>=S.stacksize)
{
S.base=(BTree*)realloc(S.base,(S.stacksize+LISTINCREMENT)*sizeof(BTree));
if(S.base==NULL)
{
printf("错误!\n");
return ERROR;
}
S.top=S.base+S.stacksize;
S.stacksize=S.stacksize+LISTINCREMENT;
}
*(S.top)=n;
S.top++;
return OK;
}
int StackEmpty(SqStack S)
{
if(S.base==S.top)
{
return ERROR;
}
return OK;
}
int DestroyStack(SqStack &S)
{
free(S.base);
S.base=NULL;
S.stacksize=0;
return OK;
}
int pop(SqStack &S,BTree &e);
int GetTop(SqStack S,BTree &e);
int push(SqStack &S,BTree n);
int StackEmpty(SqStack S);
int DestroyStack(SqStack &S);
int CreateBTree(BTree &T);
int PreTraverseBTree(BTree T);
int InTraverseBTree(BTree T);
int PostTraverseBTree(BTree T);
int FindTraverseBTree(BTree T);
int LevelTraverseBTree(BTree T);
int InTraverseBTree2(BTree T);
void show();
int main()
{ BTree T;
int n;
show();
do
{
scanf("%d",&n);
switch(n)
{
case 1:{
printf("以先序序列输入创建二叉树,每个空链接都要设置成空格或'$'符号\n");//注意二叉树的结束是两个孩子都为空,输出时候要注意
CreateBTree(T);
break;
}
case 2:{
if(flag)
{
printf("前序遍历结果是:\n");
PreTraverseBTree(T);
printf("\n");
}
else
printf("请先创建二叉树!\n");
break;
}
case 3:{
if(flag)
{
printf("中序递归遍历结果是:\n");
InTraverseBTree(T);
printf("\n");
}
else
printf("请先创建二叉树!\n");
break;
}
case 4:{
if(flag)
{printf("中序非递归遍历结果是:\n");
InTraverseBTree2(T);
printf("\n");
}
else
printf("请先创建二叉树!\n");
break;
}
case 5:{
if(flag)
{printf("后序遍历结果是:\n");
PostTraverseBTree(T);
printf("\n");
}
else
printf("请先创建二叉树!\n");
break;
}
case 6:{
if(flag)
{printf("层次遍历结果是:\n");
LevelTraverseBTree(T);
printf("\n");
}
else
printf("请先创建二叉树!\n");
break;
}
case 7:{
if(flag)
{
t=0;
sum=0;
FindTraverseBTree(T);
printf("深度是:%d\n",t);
}
else
printf("请先创建二叉树!\n");
break;
}
case 8:{
exit(0);
printf("退出成功!");
break;
}
default :{
printf("操作不合法!\n");
break;
}
}
printf("请继续:\n");
}while(1);
return 0;
}
void show()
{
printf("可执行操作有:\n");
printf("***********************************************************************\n");
printf("********* 1.创建二叉树 *********\n");
printf("********* 2.先序遍历二叉树 *********\n");
printf("********* 3.中序遍历二叉树1 *********\n");
printf("********* 4.中序遍历二叉树2 *********\n");
printf("********* 5.后续遍历二叉树 *********\n");
printf("********* 6.层序遍历二叉树 *********\n");
printf("********* 7.求二叉树的深度 *********\n");
printf("********* 8.退出 *********\n");
printf("***********************************************************************\n");
}
int InitStack(SqStack &S)
{
S.base=(BTree*)malloc(LIST_INIT_SIZE*sizeof(BTree));
if(!S.base)
{
printf("错误!\n");
return ERROR;
}
S.top=S.base;
S.stacksize=LIST_INIT_SIZE;
return OK;
}
int pop(SqStack &S,BTree &e)
{
if(S.top==S.base)
{
return ERROR;
}
e=*--S.top;
return OK;
}
int GetTop(SqStack S,BTree &e)
{
if(S.top==S.base)
{
return ERROR;
}
e=*(--S.top);
return OK;
}
int push(SqStack &S,BTree n)
{
if(S.top-S.base>=S.stacksize)
{
S.base=(BTree*)realloc(S.base,(S.stacksize+LISTINCREMENT)*sizeof(BTree));
if(S.base==NULL)
{
printf("错误!\n");
return ERROR;
}
S.top=S.base+S.stacksize;
S.stacksize=S.stacksize+LISTINCREMENT;
}
*(S.top)=n;
S.top++;
return OK;
}
int StackEmpty(SqStack S)
{
if(S.base==S.top)
{
return ERROR;
}
return OK;
}
int DestroyStack(SqStack &S)
{
free(S.base);
S.base=NULL;
S.stacksize=0;
return OK;
}
int visit(char c)
{
printf("%c\t",c);
return 0;
}
int CreateBTree(BTree &T)
{
char c;
c=getchar();
scanf("%c",&c);
if(c=='$')
{
T=NULL;
flag=1;
}
else
{
T=(BTree)malloc(sizeof(BiTNode));
if(!T)
{
printf("错误!\n");
return ERROR;
}
T->data=c;
CreateBTree(T->lchild);
CreateBTree(T->rchild);
}
return OK;
}
int PreTraverseBTree(BTree T)
{
if(T)
{
visit(T->data);
PreTraverseBTree(T->lchild);
PreTraverseBTree(T->rchild);
return OK;
}
else{
return ERROR;
}
}
int InTraverseBTree(BTree T)
{
if(T)
{
InTraverseBTree(T->lchild);
visit(T->data);
InTraverseBTree(T->rchild);
return OK;
}
else{
return ERROR;
}
}
int InTraverseBTree2(BTree T)
{
BTree p;
SqStack S;
InitStack(S);
push(S,T);
while(StackEmpty(S))
{
while(GetTop(S,p)&&p)
{
push(S,p->lchild);
}
pop(S,p);
if(StackEmpty(S))
{
pop(S,p);
visit(p->data);
push(S,p->rchild);
}
}
DestroyStack(S);
return OK;
}
int PostTraverseBTree(BTree T)
{
if(T)
{
PostTraverseBTree(T->lchild);
PostTraverseBTree(T->rchild);
visit(T->data);
return OK;
}
else
return ERROR;
}
int FindTraverseBTree(BTree T)
{
if(!T)
{
if(t<sum)
t=sum;
return OK;
}
else
{
sum++;
FindTraverseBTree(T->lchild);
FindTraverseBTree(T->rchild);
sum--;
return OK;
}
}
int LevelTraverseBTree(BTree T)
{
struct QNote que[100];
int head=0,rear=0;
if(!T)
{
printf("二叉树为空!\n");
return ERROR;
}
que[head].date=T;
rear++;
while(head<rear)
{
visit(que[head].date->data);
if(que[head].date->lchild)
{
que[rear].date=que[head].date->lchild;
rear++;
}
if(que[head].date->rchild)
{
que[rear].date=que[head].date->rchild;
rear++;
}
head++;
}
return OK;
}
{
printf("%c\t",c);
return 0;
}
int CreateBTree(BTree &T)
{
char c;
c=getchar();
scanf("%c",&c);
if(c=='$')
{
T=NULL;
flag=1;
}
else
{
T=(BTree)malloc(sizeof(BiTNode));
if(!T)
{
printf("错误!\n");
return ERROR;
}
T->data=c;
CreateBTree(T->lchild);
CreateBTree(T->rchild);
}
return OK;
}
int PreTraverseBTree(BTree T)
{
if(T)
{
visit(T->data);
PreTraverseBTree(T->lchild);
PreTraverseBTree(T->rchild);
return OK;
}
else{
return ERROR;
}
}
int InTraverseBTree(BTree T)
{
if(T)
{
InTraverseBTree(T->lchild);
visit(T->data);
InTraverseBTree(T->rchild);
return OK;
}
else{
return ERROR;
}
}
int InTraverseBTree2(BTree T)
{
BTree p;
SqStack S;
InitStack(S);
push(S,T);
while(StackEmpty(S))
{
while(GetTop(S,p)&&p)
{
push(S,p->lchild);
}
pop(S,p);
if(StackEmpty(S))
{
pop(S,p);
visit(p->data);
push(S,p->rchild);
}
}
DestroyStack(S);
return OK;
}
int PostTraverseBTree(BTree T)
{
if(T)
{
PostTraverseBTree(T->lchild);
PostTraverseBTree(T->rchild);
visit(T->data);
return OK;
}
else
return ERROR;
}
int FindTraverseBTree(BTree T)
{
if(!T)
{
if(t<sum)
t=sum;
return OK;
}
else
{
sum++;
FindTraverseBTree(T->lchild);
FindTraverseBTree(T->rchild);
sum--;
return OK;
}
}
int LevelTraverseBTree(BTree T)
{
struct QNote que[100];
int head=0,rear=0;
if(!T)
{
printf("二叉树为空!\n");
return ERROR;
}
que[head].date=T;
rear++;
while(head<rear)
{
visit(que[head].date->data);
if(que[head].date->lchild)
{
que[rear].date=que[head].date->lchild;
rear++;
}
if(que[head].date->rchild)
{
que[rear].date=que[head].date->rchild;
rear++;
}
head++;
}
return OK;
}