#include <stdio.h>
#include <malloc.h>
#define NULL 0
typedef struct MyTree
{
char data;
struct MyTree *lChild;
struct MyTree *rChild;
}*BeeTree;
void CreateTree(BeeTree *p)
{
char ch;
ch=getchar();
scanf("%c",&ch);
if(ch=='.')
*p=NULL;
p=(BeeTree *)malloc(sizeof(BeeTree));
(*p)->data=ch;
CreateTree(&((*p)->lChild));
CreateTree(&((*p)->rChild));
}
int Allsumleaf(BeeTree T)
{
int Allsum=0;
if(T!=NULL)
{
Allsumleaf(T->lChild);
Allsumleaf(T->lChild);
if(T->lChild==NULL&&T->rChild==NULL)
Allsum++;
}
return Allsum;
}
void middle(BeeTree T)
{
if(T!=NULL)
{
middle(T->lChild);
printf("%c",T->data);
middle(T->rChild );
}
}
void PreOrder(BeeTree T)
{
if(T!=NULL)
{
printf("%c",T->data);
PreOrder(T->lChild);
PreOrder(T->rChild );
}
}
void tail(BeeTree T)
{
if(T!=NULL)
{
tail(T->lChild);
tail(T->rChild );
printf("%c",T->data );
}
}
int deepthth(BeeTree T)
{
int deepth=0,deepth1,deepthr;
if(T=NULL) deepth=0;
else{
deepth1=deepthth(T->lChild);
deepthr=deepthth(T->rChild );
deepth=1+(deepth1>deepthr?deepth1:deepthr);
}
return deepth;
}
int main(int argc, char* argv[])
{ printf("\n 请输入该树:\n");
BeeTree Tree;
CreateTree(&Tree);
printf("\n该树的前续遍历:\n");
PreOrder(Tree);
printf("\n该树的中续遍历:\n");
middle(Tree);
printf("\n该树的后续遍历:\n");
tail(Tree);
printf("\n该树的子叶数为:%d\n",Allsumleaf(Tree));
printf("\n该树的深度为:%d\n",deepthth(Tree));
return 0;
}
浙公网安备 33010602011771号