1
#ifndef _BST_H_
/* Returns negative (left<right), zero (left==right), or positive (left>right). */
typedef int comparator(void* left, void* right);
struct bst_node {
void* data;
struct bst_node* left;
struct bst_node* right;
};
struct bst_node* new_node(void* data);
void free_node(struct bst_node* node);
struct bst_node** search(struct bst_node** root, comparator compare, void* data);
void insert(struct bst_node** root, comparator compare, void* data);
void delete(struct bst_node** node);
#endif
这是一个头文件。 我不明白search
函数,为什么返回类型是node**
?为什么这个搜索函数返回一个指向指针的指针?
编辑: 添加的搜索FUNC这里:
struct bst_node** search(struct bst_node** root, comparator compare, void* data) {
struct bst_node** node = root;
while (*node != NULL) {
int compare_result = compare(data, (*node)->data);
if (compare_result < 0)
node = &(*node)->left;
else if (compare_result > 0)
node = &(*node)->right;
else
break;
}
return node;
}