#include <stdio.h>
#include <malloc.h>
typedef struct BiTNode
{
int data;
struct BiTNode *lchild,*rchild;
} BiTNode;
int BTreeCount(BiTNode *T)
{
if(T==NULL)
{
return 0;
}else{
return BTreeCount(T->lchild)+BTreeCount(T->rchild)+1;
}
}
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};
int bTreeCount;
bt=CreateBST(a,n);
DispBST(bt);
printf("\n");
bTreeCount=BTreeCount(bt);
printf("结点总个数:%d\n",bTreeCount);
return 0;
}