#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DataType char
#define MAXSIZE 1000
int count = 0;
typedef struct Node
{
DataType data;
struct Node * Lchild;
struct Node * Rchild;
} BiTNode,* BiTree;
void CreateBiTree(BiTree * root)
{
char ch;
ch = getchar();
if(ch == ' ')
* root = NULL;
else
{
*root = (BiTree)malloc(sizeof(BiTNode));
(*root)->data = ch;
CreateBiTree(&((*root)->Lchild));
CreateBiTree(&((*root)->Rchild));
}
}
void PreOrder(BiTree root)
{
if(root)
{
putchar(root->data);
PreOrder(root->Lchild);
PreOrder(root->Rchild);
}
}
void InOrder(BiTree root)
{
if(root)
{
InOrder(root->Lchild);
putchar(root->data);
InOrder(root->Rchild);
}
}
void PostOrder(BiTree root)
{
if(root)
{
PostOrder(root->Lchild);
PostOrder(root->Rchild);
putchar(root->data);
count++;
}
}
int leaf(BiTree root)
{
if(root == NULL) return 0;
if(!root->Lchild && !root->Rchild)
{
putchar(root->data);
return 1;
}
return leaf(root->Lchild) + leaf(root->Rchild);
}
int TreeDepth(BiTree root)
{
int h,lh,rh;
if(root == NULL) return 0;
lh = TreeDepth(root->Rchild);
rh = TreeDepth(root->Lchild);
h = (lh>rh ? lh : rh) + 1;
return h;
}
void PrintfTree(BiTree root, int h)
{
if(root == NULL) return;
PrintfTree(root->Rchild,h+4);
for (int i = 0; i < h; i++)
printf(" ");
printf("%c\n",root->data);
PrintfTree(root->Lchild,h+4);
}
int main(int argc, char **argv) {
int LeafNum,Depth;
BiTree Tree;
printf("按先序输入建立树:");
CreateBiTree(&Tree);
printf("先序:");
PreOrder(Tree);
printf("\n中序:");
InOrder(Tree);
printf("\n后序:");
PostOrder(Tree);
printf("\n叶子节点:");
LeafNum = leaf(Tree);
printf("\n节点数:%d",count);
printf(" 叶子节点数为%d",LeafNum);
Depth = TreeDepth(Tree);
printf("\n树高度为:%d\n",Depth);
PrintfTree(Tree,1);
return 0;
}