这东西很多的这里给你一个#include
#include
typedef struct np{
int dat;
struct np *left,*right;
} node;
node *create(void)
{
return (malloc(sizeof(node)));
}
node *t(node *a,int d)
{
if (a==NULL) {
a=create();
a->left =a->right =NULL;
a->dat=d;
}
else if (d>=a->dat) {
a->right =t(a->right,d);
}
else if (d
a->left =t(a->left ,d);
}
return a;
}
void inorder(node *r)
{
if (r!=NULL) {
inorder(r->left );
printf("%d ",r->dat );
inorder(r->right );
}
}
int ser(node *so,int a)
{
if (so==NULL)
return 0;
else if (so->dat==a)
return 1;
else if (a>so->dat)
return ser(so->right,a);
else if (a
return ser(so->left ,a);
}
int main(int argc, char* argv[])
{
node *bst=NULL;
FILE *fp;
int i;
fp=fopen("c:\\dat.txt","r"); /*假设数据文件是c:\dat.txt*/
while (!feof(fp)){
fscanf(fp,"%d",&i);
bst=t(bst,i); /*生成二叉排序树*/
}
fclose(fp);
inorder(bst); /*输出二叉排序树*/
putchar('\n');
scanf("%d",&i); /*输入需要查找的数字*/
if (ser(bst,i)) printf("YES"); /*如果找到,则输出yes,否则输出no*/
else printf("NO");
return 0;
}
//-