有序二叉树的查找问题
#include "malloc.h"#include"stdio.h"
typedef struct node
{ int data;
struct node *left,*right;
}ErXTree;
void insert(ErXTree **t,ErXTree *s)
{ if(*t==NULL) *t=s;
else if(s->data==(*t)->data) return;
else if(s->data<(*t)->data) insert(&(*t)->left,s);
else if(s->data>(*t)->data) insert(&(*t)->right,s);
}
void creat(ErXTree **t1)
{ int x,k=0;
ErXTree *s,*t;
t=NULL;
scanf("%d",&x);
while(x!=0)
{ s=(ErXTree*)malloc(sizeof(ErXTree));
s->data=x;if(k==0)*t1=s;k++;
s->left=NULL;
s->right=NULL;
insert(&t,s);
scanf("%d",&x);
}
}
ErXTree *search(ErXTree *b,int x)
{ if(b=NULL) return NULL;
else
{ if(b->data==x) return b;
if(x<b->data) return (search(b->left,x));
else return (search(b->right,x));
}
}
main()
{ ErXTree *t;
int x,k;
creat(&t);
printf("输入要查找的关键字:");
scanf("%d",&x);
if(search(t,x)==x)
printf("找到了");
else printf("没有找到");
} //输入要查找的值后, 程序就结束了
[此贴子已经被作者于2005-4-6 11:17:58编辑过]