Binary Search Tree can't delete the root

Posted by Ali Zahr on Stack Overflow See other posts from Stack Overflow or by Ali Zahr
Published on 2014-05-28T15:59:24Z Indexed on 2014/05/28 21:27 UTC
Read the original article Hit count: 199

Everything is working fine in this function, but the problem is that I can't delete the root, I couldn't figure out what's the bug here.I've traced the "else part" it works fine until the return, it returns the old value I don't know why. Plz Help!

node *removeNode(node *Root, int key) {

node *tmp = new node;

if(key > Root->value)
    Root->right = removeNode(Root->right,key);

else if(key < Root->value)
    Root->left = removeNode(Root->left, key);

else if(Root->left != NULL && Root->right != NULL) {
    node *minNode = findNode(Root->right);

    Root->value = minNode->value;

    Root->right = removeNode(Root->right,Root->value);
}

else {
    tmp = Root;

    if(Root->left == NULL)
        Root = Root->right;
    else if(Root->right == NULL)
        Root = Root->left;

    delete tmp;
}

return Root;

}

© Stack Overflow or respective owner

Related posts about c

    Related posts about data-structures