#include<stdio.h>
#include <malloc.h>
typedef struct Node
{
int
data;
struct Node *LChild;
struct Node *RChild;
}BitNode,*BitTree;
/*先序创建二叉树*/
void CreatBiTree(BitTree *bt)
{
char
ch;
ch=getchar();
if(ch == '.') *bt = NULL;
else
{
*bt
= (BitTree)malloc(sizeof(BitNode));
(*bt)->data = ch;
CreatBiTree(&((*bt)->LChild));
CreatBiTree(&((*bt)->RChild));
}
}
void Visit(char ch)
{
printf("%c ",ch);
}
/*先序遍历二叉树*/
void
PreOrder(BitTree root)
{
if
(root != NULL)
{
Visit(root->data);
PreOrder(root->LChild);
PreOrder(root->RChild);
}
}
/*中序遍历二叉树*/
void
InOrder(BitTree root)
{
if
(root != NULL)
{
InOrder(root->LChild);
Visit(root->data);
InOrder(root->RChild);
}
}
/* 后序遍历二叉树*/
void
PostOrder(BitTree root)
{
if(root
!= NULL)
{
PostOrder(root->LChild);
PostOrder(root->RChild);
Visit(root->data);
}
}
//后序遍历求二叉树的高度递归算法//
int PostTreeDepth(BitTree bt)
{
int
hl,hr,max;
if(bt
!= NULL)
{
hl
= PostTreeDepth(bt->LChild); //求左子树的深度
hr
= PostTreeDepth(bt->RChild); //求右子树的深度
max
= hl>hr?hl:hr; //得到左、右子树深度较大者
return(max+1); //返回树的深度
}
else
return(0); //如果是空树,则返回0
}
void main()
{
BitTree
T;
int h;
int layer;
int treeleaf;
layer=0;
printf("请输入二叉树中的元素(以扩展先序遍历序列输入,其中.代表空子树):/n");
CreatBiTree(&T);
printf("先序遍历序列为:");
PreOrder(T);
printf("/n中序遍历序列为:");
InOrder(T);
printf("/n后序遍历序列为:");
}