#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define M 100
//FCA##DB###EH##GM###
typedef struct BiTNode{
char data;
struct BiTNode *lchild,*rchild;
int tag;
}BiTNode,*BiTree;
typedef int Status;
typedef char SElemType,LinkList;
typedef struct StackNode//定义
{
BiTree data;
struct StackNode *next;
}StackNode,*LinkStack;
Status InitStack(LinkStack &S) //链栈的初始化
{
S=(StackNode*)malloc(sizeof(StackNode));
S=NULL;
return OK;
}
Status Push(LinkStack &S,BiTree T) //入栈
{
StackNode* p;
p=new StackNode;
p->data = T;
p->next = S;
S=p;
return OK;
}
Status Pop(LinkStack &S,BiTree &T) //出栈
{
if(S==NULL)
{
return ERROR;
}
else
{
StackNode* p;
T=S->data;
p=S;
S=S->next;
free(p);
return OK;
}
}
BiTree CreatBiTree()//创建二叉树
{
BiTree T;
char c;
scanf("%c",&c);
if(c=='#')
{
T=NULL;
}
else
{
T=(BiTree)malloc(sizeof(BiTNode));
T->data=c;
T->lchild=CreatBiTree();
T->rchild=CreatBiTree();
}
return T;
}
visit(char data){
printf("%c ",data);
}
PreOrderTraverse(BiTree T)//先序递归实现二叉树
{
if(T){
visit(T->data);
PreOrderTraverse(T->lchild);
PreOrderTraverse(T->rchild);
}
}
InOrderTraverse(BiTree T)//中序递归实现二叉树
{
if(T)
{
InOrderTraverse(T->lchild);
visit(T->data);
InOrderTraverse(T->rchild);
}
}
PostOrderTraverse(BiTree T)//后序递归实现二叉树
{
if(T)
{
PostOrderTraverse(T->lchild);
PostOrderTraverse(T->rchild);
visit(T->data);
}
}
void PreOrderTraverse2(BiTree T)//非递归先序
{
LinkStack S;
InitStack(S);
BiTree p=T;
while(p||S!=NULL)
{
if(p!=NULL)
{
printf("%c ",p->data);
Push(S,p);
p=p->lchild;
}
else
{
Pop(S,p);
p=p->rchild;
}
}
}
void InOrderTraverse2(BiTree T)
{//中序遍历二叉树T的非递归算法
LinkStack S;
InitStack(S);
BiTree p=T;
BiTree q=new BiTNode;
while(p||S!=NULL)
{
if(p)
{
Push(S,p);
p=p->lchild;
}
else
{
Pop(S,q);
printf("%c",q->data);
p=q->rchild;
}
}
}
void PostOrderTraverse2(BiTree T)//后序非递归实现二叉树
{
LinkStack S;
InitStack(S);
BiTree p=T;
LinkStack q;
while(p||S!=NULL)
{
if(p)
{
Push(S,p);
p->tag=0;
p=p->lchild;
}
else
{
Pop(S,p);
if(p->tag==0)
{
p->tag=1;
Push(S,p);
p=p->rchild;
}
else{
printf("%c",p->data);
p=NULL;
}
}
}
}
int Depth (BiTree T)//判断二叉树的深度
{
if(T==NULL)
return 0;
else
{
int m=Depth(T->lchild);
int n=Depth(T->rchild);
if(m>n)
return(m+1);
else
return(n+1);
}
}
void Change(BiTree T){
if(T){
BiTree temp=T->lchild;
T->lchild=T->rchild;
T->rchild=temp;
Change(T->lchild);
Change(T->rchild);}
} //左右子树交换
void FloorPrint(BiTree T) //层序遍历
{
BiTree temp[100]; //创建BiTree指针类型的指针数组
int in = 0;
int out = 0;
temp[in++] = T; //先保存二叉树根节点
while (in > out)
{
if (temp[out])
{
printf("%c ",temp[out]->data);
temp[in++] = temp[out]->lchild;
temp[in++] = temp[out]->rchild;
}
out++;
}
}//层次遍历
int main()
{
BiTree T;
T=CreatBiTree();
int c;
c=Depth(T);
printf("深度为:%d",c);
printf("\n");
printf("交换左右孩子树\n");
Change(T);
printf("\n");
printf("层序遍历\n");
FloorPrint(T);
printf("\n");
printf("先序遍历二叉树\n");
PreOrderTraverse(T);
printf("\n");
printf("中序遍历二叉树\n");
InOrderTraverse(T);
printf("\n");
printf("后序遍历二叉树\n");
PostOrderTraverse(T);
printf("\n");
printf("中序遍历二叉树非递归\n");
InOrderTraverse2(T);
printf("\n");
printf("先序遍历二叉树非递归\n");
PreOrderTraverse2(T);
printf("\n");
printf("后序遍历二叉树非递归\n");
PostOrderTraverse2(T);
printf("\n");
return 0;
}