C二叉搜索树删除实现

时间:2015-09-19 02:34:26

标签: c algorithm binary-search-tree

我正在尝试在C中实现二进制搜索树的删除功能,但是我遇到了问题。

我有树和节点的以下结构

typedef struct {
    double value;

    struct Node *parent;
    struct Node *right_child;
    struct Node *left_child;
} Node;

typedef struct {
    struct Node *root;
} Tree;

我还有一个有序遍历功能

void inOrderTraversalNode(Node *n) {

    if (n != NULL) {

        inOrderTraversalNode(n->left_child);
        printf("%f\n", n->value);
        inOrderTraversalNode(n->right_child);
    }
}

子树最小函数

Node * minimum(Node *n) {

    while (n->left_child != NULL) {
        n = n->left_child;
    }

    return n;
}

和移植功能

void transplant(Tree *t, Node *u, Node *v) {

    Node *p = u->parent;

    //replace u's parent's child pointer to v
    if (p == NULL) {
        t->root = v;
    } else if (u == p->left_child) {
        p->left_child = v;
    } else {
        p->right_child = v;
    }

    //set v's parent pointer to u's parent
    if (v != NULL) {
        v->parent = p;
    }
}

最后我有删除功能

void delete(Tree *t, Node *z) {

    //if node z has no left subtree, replace z with right subtree and vice-versa.
    if (z->left_child == NULL) {
        transplant(t, z, z->right_child);

    } else if (z->right_child == NULL) {
        transplant(t, z, z->left_child);
    } else {
        Node *y = minimum(z->right_child);

        if (y->parent != z) {
            transplant(t, y, y->right_child);

            Node *y_right_child = y->right_child;

            y_right_child = z->right_child;
            y_right_child->parent = y;
        }
        transplant(t, z, y);

        Node *y_left_child = y->left_child;

        y_left_child = z->left_child;
        y_left_child->parent = y;
    }

}

但是当我在main

中运行以下代码时
int main(void) {

        Node n1;
        Node n2;
        Node n3;
        Node n4;
        Node n5;
        Node n6;
        Node n7;
        Node n8;

        n1.value = 4;
        n1.parent = NULL;
        n1.left_child = &n2;
        n1.right_child = &n5;

        n2.value = 2;
        n2.parent = &n1;
        n2.left_child = &n3;
        n2.right_child = &n4;

        n3.value = 1;
        n3.parent = &n2;
        n3.left_child = NULL;
        n3.right_child = NULL;

        n4.value = 3;
        n4.parent = &n2;
        n4.left_child = NULL;
        n4.right_child = NULL;

        n5.value = 6;
        n5.parent = &n1;
        n5.left_child = &n6;
        n5.right_child = &n7;

        n6.value = 5;
        n6.parent = &n5;
        n6.left_child = NULL;
        n6.right_child = NULL;

        n7.value = 7;
        n7.parent = &n5;
        n7.left_child = NULL;
        n7.right_child = NULL;


        Tree t;

        t.root = &n1;

        printf("In order traversal\n");
        inOrderTraversalNode(t.root);

        printf("Delete node\n");
        delete(&t,&n1);
        inOrderTraversalNode(t.root);


        return EXIT_SUCCESS;
    }

为第一次遍历返回1,2,3,4,5,6,7。但是,在删除1,2,3,4,7之后,它会返回n5以进行第二次遍历,因为它错过了包含n6的{​​{1}}节点。我不明白为什么会这样。我在delete函数中插入了一些print语句,而5节点正在添加n7节点作为其左子节点,但由于某种原因,它在遍历期间不会被打印。

2 个答案:

答案 0 :(得分:1)

此代码引起了我的注意:

        Node *y_right_child = y->right_child;

        y_right_child = z->right_child;
        y_right_child->parent = y;

您为变量y_right_child指定了值,然后立即将其替换为其他值。看来,您打算做的是将节点z的右子节点转移到节点y。就是这样:

        y->right_child = z->right_child;
        y->right_child->parent = y;

此时您无需记住或修改y原始正确的孩子,因为这已经通过函数transplant()处理。

其他子转移代码存在类似问题:

    Node *y_left_child = y->left_child;

    y_left_child = z->left_child;
    y_left_child->parent = y;

再次,您为变量赋值,然后立即替换它。你真正想要的是:

    y->left_child = z->left_child;
    y->left_child->parent = y;

请注意,因为节点y从其子树开始作为最小值,所以可以确定y->left_child最初为NULL。

答案 1 :(得分:1)

正如您的代码所代表的那样,struct Node类型未在任何地方声明或定义。代码顶部的typedef无法实现此目的;它定义了一个名为Node的不同类型。见typedef struct vs struct definitions。这应该会在您的编辑中引起大量警告。不要忽视它们!

它似乎也是真正的错误的间接原因。在delete中您有代码

        Node *y_right_child = y->right_child;

        y_right_child = z->right_child;
        y_right_child->parent = y;

这显然不是正确的事情。您想要更改y的正确子指针。但是当你写下你真正想要的东西时:

        y->right_child = z->right_child;
        y->right_child->parent = y;

您收到错误,因为y->right_child属于未定义类型struct Node *,因此无法取消引用。您没有正确地修复此问题,而是引入了局部变量y_right_child,至少使代码编译。但编译的代码有什么用,如果它不起作用的话?分配给y_right_child只会改变局部变量的值;它不会改变指针y->right_child本身的值。

将第一行更改为typedef struct Node {会导致struct Node被定义为您想要的(并且typedef Node被定义为相同的类型)。然后将y_right_child更改为y->right_child并完全删除变量y_right_child,并对y->left_child执行相同操作,删除按预期工作。