0

我正在用 C 编写一个二叉搜索树的实现,这是“插入”过程的一部分:

void insert(void *key, struct bst *tree) {
    void n = NULL;
    struct bst **y = &n;
    struct bst **x = &(tree->root);
    struct bst *node = init(key);
    int cmp;

    while (*x != NULL) {
            y = x;
            cmp = <compare  node and x's key>
            if (cmp > 0)
                    x = &((*x)->left);
            else
                    x = &((*x)->right);
    }

    if (y == NULL) /* Empty Tree */
            tree->root = node;
    cmp = <compare node and y's key>

    else if (cmp > 0)
            (*y)->left = node;
    else
            (*y)->right = node;

我正在使用双结构指针(x 和 y)来更改原始树。是否需要双指针,或者我可以使用指向树本身的简单指针来做到这一点?

编辑:在评论中得到答案,我不需要指针。

4

0 回答 0