#include <iostream>
using namespace std;
struct BSTNode
{
int data;
BSTNode *lchild;
BSTNode *rchild;
};
void insertBST(BSTNode *&T,int e)
{
if(T)
{
if(e < T->data)
insertBST(T->lchild,e);
if(e > T->data)
insertBST(T->rchild,e);
}
else
{
T=new BSTNode;
T->data=e;
T->lchild=T->rchild=NULL;
}
}
void createBST(BSTNode *&T)
{
T=NULL;
int e;
while(cin>>e && e)
insertBST(T,e);
}
BSTNode *searchBST(BSTNode *T,int key)
{
if(T==NULL)
return NULL;
if(key==T->data)
return T;
if(key < T->data)
return searchBST(T->lchild,key);
if(key > T->data)
return searchBST(T->rchild,key);
}
void inorderBST(BSTNode *T)
{
if(T)
{
inorderBST(T->lchild);
cout<<T->data<<” “;
inorderBST(T->rchild);
}
}
int main()
{
BSTNode *T;
cout<<“请输入一棵树(以0结束):”<<endl;
createBST(T);
inorderBST(T);
int e;
cout<<endl<<endl<<“请输入要插入的数值:”<<endl<<endl;
cin>>e;
insertBST(T,e);
inorderBST(T);
cout<<endl<<endl<<“请输入要查找的值:”<<endl<<endl;
cin>>e;
BSTNode *t=searchBST(T,e);
if(t)
cout<<“ok”<<endl;
else
cout<<“no”<<endl;
return 0;
}