0

我正在尝试从我的 BST 中删除根节点,然后按顺序打印树根删除似乎是一个问题,所有其他节点都已成功删除。

根是 20。

inOrderPrint 5 6 7 8 9 10 17 18 20 23 24 25 29 55 56 57 58 59

提供要删除的节点

20

删除后

5 6 7 8 9 10 17 18 20 5 6 7 8 9 10 17 18 23 24 25 29 55 56 57 58 59

正如您在删除后看到的那样,bintree 并不像预期的那样。粗体键是不需要的。下面是我的代码

void treeDeleteNode (binTreeT **tree, binTreeT *node)
{
    binTreeT *succs;
    binTreeT *parent;
    binTreeT *root = *tree;

    if (node->rchild == NULL) {
        transplantTree (&root, node, node->lchild);
    }
    else if (node->lchild == NULL) {
        transplantTree (&root, node, node->rchild);
    }
    else {
        succs = treeMin (node->rchild);
        parent = getParentNode (root, succs);
        if (parent != node) {
            transplantTree (&root, succs, succs->rchild);
            succs->rchild = node->rchild;
        }
        transplantTree (&root, node, succs);
        succs->lchild = node->lchild;
    }
}


void transplantTree (binTreeT **root, binTreeT *old, binTreeT *new)
{
    binTreeT *rootRef = *root;
    binTreeT *parent;

    parent = getParentNode(rootRef, old);
    if (NULL == parent) {
        *root = new;
    }
    else {
        if (parent->lchild == old) {
            parent->lchild = new;
        }
        else {
            parent->rchild = new;
        }
    }
}


binTreeT* treeMin (binTreeT *tree)
{
    while (tree->lchild != NULL) {
        tree = tree->lchild;
    }
    return tree;
}


binTreeT* getParentNode(binTreeT *root, binTreeT* node)
{
    binTreeT *parent = NULL;

    while (root->data != node->data) {
        parent = root;
        if (node->data < root->data) {
            root = root->lchild;
        }
        else if(node->data > root->data) {
            root = root->rchild;
        }
    }
    return parent;
}


void inOrderPrint (binTreeT *tree)
{
    if (NULL != tree) {
        inOrderPrint (tree->lchild);
        printf("%d \t", tree->data);
        inOrderPrint (tree->rchild);
    }
}

.....任何帮助表示赞赏......

4

3 回答 3

0

在函数treeDeleteNode中,每次调用transplantTree函数时都应将其tree作为第一个参数而不是&root. 这是因为根节点可以在此函数中进行修改,这些更改应针对tree变量本身进行。

于 2013-07-06T09:11:30.267 回答
0

改变

else {
    succs = treeMin (node->rchild);
    parent = getParentNode (root, succs);
    if (parent != node) {
        transplantTree (&root, succs, succs->rchild);
        succs->rchild = node->rchild;
    }
    transplantTree (&root, node, succs);
    succs->lchild = node->lchild;
}

else {
    succs = treeMin (node->rchild);
    parent = getParentNode (root, succs);
    if (parent != node) {
        //TODO:copy succs'content to node instead
        transplantTree (&root, succs, succs->rchild);
        free(succs);
    }
    else {
        transplantTree (&root, node, succs);
        succs->lchild = node->lchild;
        free(node);
    }
}
于 2013-07-06T09:18:45.237 回答
0
void treeDeleteNode (binTreeT **tree, binTreeT *node)
{
    binTreeT *sub;

       //* find the place where node should be */
    while (*tree) {
        if (*tree == node) break;
        if ( node->data <= (*tree)->data) tree = &(*tree)->lchild;
        else tree = &(*tree)->rchild;
        }
        /* not found: nothing to do (except freeing node) */
    if ( !*tree) { free(node); return; }

        /* When we get here, *tree points to the pointer that points to node. 
        ** If any of the {l,r}pointers is NULL, the other
        ** will become the new root of the subtree (replacing node)
        */
    if ( !node->lchild) { *tree = node->rchild; free(node); return; }
    if ( !node->rchild) { *tree = node->lchild; free(node); return; }

        /* cut off left subchain of tree, save it, and set it to NULL */
    sub = node->lchild;
    node->lchild = NULL;


        /* find leftmost subtree of right subtree of 'node' */
    for (*tree = node->rchild; *tree; tree =  &(*tree)->lchild) {;}
        /* and put the remainder there */
    *tree = sub;

    free(node);
}
于 2013-07-06T12:38:25.547 回答