#include <stdio.h>
typedef char DataType;
typedef struct node /*二叉树的存储结构*/
{
DataType data;
struct node *Lchild;
struct node *Rchild;
}BiTNode,*BiTree;
int Count1=0;
int Count2=0;
int depth = 0;
BiTNode *Create_BiTree();
void PreOrder(BiTree root);
void InOrder(BiTree root);
void PostOrder(BiTree root);
void PreOrder_CalNode(BiTree root);
void InOrder_PrintLeaf(BiTree root);
void PostOrder_CalLeaf(BiTree root);
int CalLeaf(BiTree root);
void TreeDepth(BiTree root,int h);
int PostOrder_TreeDepth(BiTree root);
BiTree Parent(BiTree root,BiTree current);
void PrintTree(BiTree root,int h);
int main()
{
BiTree root;
printf("请输入先序二叉树序列(# = NULL):");
root = Create_BiTree();
printf("\n先序递归遍历如下:");
PreOrder(root);
printf("\n\n中序递归遍历如下:");
InOrder(root);
printf("\n\n后序递归遍历如下:");
PostOrder(root);
printf("\n");
PreOrder_CalNode(root);
printf("\n二叉树的结点数是:%d\n",Count1);
printf("\n二叉树的叶子结点是:");
InOrder_PrintLeaf(root);
printf("\n");
PostOrder_CalLeaf(root);
printf("\n二叉树的叶子结点数是:%d\n",Count2);
int h=1;
TreeDepth(root,h);
printf("\n二叉树的高度是:%d\n",depth);
h = 1;
printf("\n二叉树的树状打印如下:\n\n");
PrintTree(root,h);
return 0;
}
BiTNode *Create_BiTree()
{
DataType ch;
BiTree root;
scanf("%c",&ch);
if(ch=='#') root=NULL;
else{
root = (BiTree)malloc(sizeof(BiTNode));
root->data = ch;
root->Lchild = Create_BiTree();
root->Rchild = Create_BiTree();
}
return root;
}
void PreOrder(BiTree root)
{
if(root){
printf("%c",root->data);
PreOrder(root->Lchild);
PreOrder(root->Rchild);
}
}
void InOrder(BiTree root)
{
if(root){
InOrder(root->Lchild);
printf("%c",root->data);
InOrder(root->Rchild);
}
}
void PostOrder(BiTree root)
{
if(root){
PostOrder(root->Lchild);
PostOrder(root->Rchild);
printf("%c",root->data);
}
}
void PreOrder_CalNode(BiTree root)
{
if(root)
{
Count1++;
PreOrder_CalNode(root->Lchild);
PreOrder_CalNode(root->Rchild);
}
}
void InOrder_PrintLeaf(BiTree root)
{
if(root){
InOrder_PrintLeaf(root->Lchild);
if(root->Lchild == NULL && root->Rchild == NULL){
printf("%c ",root->data);
}
InOrder_PrintLeaf(root->Rchild);
}
}
void PostOrder_CalLeaf(BiTree root)
{
if(root){
PostOrder_CalLeaf(root->Lchild);
PostOrder_CalLeaf(root->Rchild);
if(root->Lchild == NULL && root->Rchild == NULL){
Count2++;
}
}
}
int CalLeaf(BiTree root)
{
int nl,nr;
if(root==NULL) return 0;
if(root->Lchild==NULL && root->Rchild==NULL) return 1;
nl = CalLeaf(root->Lchild);
nr = CalLeaf(root->Rchild);
}
void TreeDepth(BiTree root,int h)
{
if(root){
if(h>depth) depth = h;
TreeDepth(root->Lchild,h+1);
TreeDepth(root->Rchild,h+1);
}
}
int PostOrder_TreeDepth(BiTree root)
{
int hl,hr,h;
if(root==NULL) return 0;
else {
hl = PostOrder_TreeDepth(root->Lchild);
hr = PostOrder_TreeDepth(root->Rchild);
h = (hl > hr ? hl : hr) + 1;
return h;
}
}
BiTree Parent(BiTree root,BiTree current)
{
BiTree p;
if(root==NULL) return NULL;
if(root->Lchild==current || root->Rchild==current)
return root;
p=Parent(root->Lchild,current);
if(p!=NULL) return p;
else return(Parent(root->Rchild,current));
}
void PrintTree(BiTree root,int h)
{
if(root==NULL) return;
PrintTree(root->Rchild,h+1);
for(int i=0;i<h;i++) printf(" ");
printf("%c\n",root->data);
PrintTree(root->Lchild,h+1);
}