如果找到,返回比较的次数,否则返回-1
#include<iostream>
using namespace std;
#include<cstdio>
#include<cstdlib>
typedef struct node
{
int key;
int data;
struct node *lc,*rc;
}BT;
//二叉排序树的插入
int insert(BT *&p,int k)
{
if(p==NULL)
{
p=(BT *)malloc(sizeof(BT));
p->key=k;
p->lc=p->rc=NULL;
return 1;
}
else if(k==p->key)
return 0;
else if(k<p->key)
return insert(p->lc,k);
else
return insert(p->rc,k);
}
//二叉排序树的生成
BT *create(int a[],int n)
{
BT *t=NULL;
int i=0;
while(i<n)
{
insert(t,a[i]);
i++;
}
return t;
}
//二叉排序树的查找
int h=0;
BT *x;
void search(BT *t,int k)
{
h++;
if(t==NULL||t->key==k)
{
x=t;
return ;
}
if(k<t->key)
search(t->lc,k);
else
se