typedef struct Tree
{
int value;
struct Tree *left,*right;
}Node,*ptrNode;
void CreatBinTree(ptrNode &root,int key)
{
if(root==NULL)
{
ptrNode tmp=new Node;
tmp->value=key;
tmp->left=tmp->right=NULL;
root=tmp;
}
else if(root->value>key)
{
CreatBinTree(root->left,key);
}
else if(root->value<key)
{
CreatBinTree(root->right,key);
}
else if(root->value==key)
{
printf("There is a same value\n");
return;
}
}
void main()
{
int a[10]={2,3,5,6,7,1,10,12,8,16};
ptrNode head=NULL;
for(int i=0;i<10;i++)
{
CreatBinTree(head,a[i]);
}
}
递归建立二叉搜索树
最新推荐文章于 2020-11-09 23:10:52 发布