#include "stdio.h"
typedef struct _tree_test
{
int data;
struct _tree_test *lchild;
struct _tree_test *rchild;
}tree_test;
tree_test *root;
int sum = 0;int m = sizeof(tree_test);
void insert_data(int x)
{
tree_test *p,*q,*s;
s = (tree_test *)malloc(sizeof(tree_test));
s->data = x;
s->lchild = NULL;
s->rchild = NULL;
if(!root)
{
root = s;
return;
}
p = root;
while(p)
{
q = p;
if(x == p->data)
{
printf("data already exist!/n");
return;
}
else if(x < p->data)
p = p->lchild;
else
p = p->rchild;
}
if(x < q->data)
q->lchild = s;
else
q->rchild = s;
}
void LTR(tree_test *root)
{
if(root)
{
if((NULL == root->lchild) &&(NULL == root->rchild))
{
sum++;
printf("%d/n",root->data);
}
LTR(root->lchild);
LTR(root->rchild);
}
}
void main()
{
int i,x;
i = 1;
root = NULL;
do
{
printf("Please input data%d:",i);
i++;
scanf("%d",&x);
if(-9999 == x)
{
LTR(root);
printf("leaf number:%d/n",sum);
return;
}
else
insert_data(x);
}while(-9999 != x);
return;
}