以下二叉搜索树 (BST)的实现有什么问题?有人告诉我,最好在插入函数中使用指向struct
节点的指针作为参数。
struct node
{
int key_value;
struct node* left;
struct node* right;
};
insert(int key, struct node *leaf)
{
if( leaf == 0 )
{
leaf = (struct node*) malloc( sizeof( struct node ) );
leaf->key_value = key;
/* initialize the children to null */
leaf->left = 0;
leaf->right = 0;
}
else if(key < leaf->key_value)
{
insert( key, leaf->left );
}
else if(key > leaf->key_value)
{
insert( key, leaf->right );
}
}