#include <stdio.h>
#include <malloc.h>
typedef struct BiTNode
{
int data;
struct BiTNode *lchild,*rchild;
} BiTNode;
int sum=0;
void LeafCount(BiTNode *T)
{
if(T)
{
sum++;
LeafCount(T->lchild);
LeafCount(T->rchild);
}
}
void DispBST(BiTNode *bt)
{
if (bt!=NULL)
{
printf("%d",bt->data);
if (bt->lchild!=NULL||bt->rchild!=NULL)
{
printf("(");
DispBST(bt->lchild);
if (bt->rchild!=NULL) printf(",");
DispBST(bt->rchild);
printf(")");
}
}
}
int InsertBST(BiTNode *&p,int k)
{
if (p==NULL)
{
p=(BiTNode *)malloc(sizeof(BiTNode));
p->data=k;
p->lchild=p->rchild=NULL;
return 1;
}
else if (k==p->data)
return 0;
else if (k<p->data)
return InsertBST(p->lchild,k);
else
return InsertBST(p->rchild,k);
}
BiTNode *CreateBST(int A[],int n)
{
BiTNode *bt=NULL;
int i=0;
while (i<n)
{
InsertBST(bt,A[i]);
i++;
}
return bt;
}
int main()
{
BiTNode *bt,*p;
int n=12,x=46;
int a[]= {25,18,46,2,53,39,32,4,74,67,60,11};
bt=CreateBST(a,n);
DispBST(bt);
LeafCount(bt);
printf("\n叶节点个数为:%d\n",sum);
return 0;
}