简体   繁体   中英

Removing a node with two children in a Binary Tree

So this is the code that I have so far. I think I've got the logic down for the most part but I'm stuck on the part with two children.. I've figured out what sort of algorithm I have to use to make it work (I've annotated with comments at the bottom of the function), I'm just not sure how to start... Can anyone point me in the right direction??

Heres what I have so far:

void BinaryTree::remove(int data){
// Is the data valid?
if(data < 0){
    cerr << data << " is not valid.  Must be a positive integer value."  << endl;
}
else{
    // Find the node
    BinNode* loc = root;
    BinNode* parent = nullptr;
    bool found = false;
    while(!found && loc != nullptr){
        if(data > loc->data){
            parent = loc;
            loc = loc->right;
        }
        else if(data < loc->data){
            parent = loc;
            loc = loc->left;
        }
        else found = true;
    }

    // If there is a parent, take care of the pointer
    if(parent != nullptr){
        if(loc->data < parent->data)
            parent->left = nullptr;
        else if(loc->data > parent->data)
            parent->right = nullptr;
    }
    // If there are children, save pointers to them
    BinNode* leftChild = nullptr;
    BinNode* rightChild = nullptr;  
    if(loc->left != nullptr)
        leftChild = loc->left;
    if(loc->right != nullptr)
        rightChild = loc->right;

    // So now pointers to the children have been saved (if they exist) and
    // parent pointers have been taken care of (if they exist) the node can be deleted
    // If no children exist simply just delete the node and return
    delete loc;
    // If one child exists
    if(leftChild != nullptr || rightChild != nullptr){  
        if(leftChild != nullptr){
            if(leftChild->data < parent->data)
                parent->left = leftChild;
            else if(leftChild->data > parent->data)
                parent->right = leftChild;
        }
        else if(rightChild != nullptr){
            if(rightChild->data < parent->data)
                parent->left = rightChild;
            else if(rightChild->data > parent->data)
                parent->right = rightChild;
        }       
    }

    // Both children exist...this sucks.
    else if(leftChild != nullptr && rightChild != nullptr){
        // Find a minimum in the right subtree
        BinNode * min = root;
        BinNode * minParent = nullptr;
        while(min->left != nullptr){
            minParent = min;
            min = min->left;
        }
        // Replace value of the node to be removed with the found minimum
        loc = new BinNode(min->data);

        // Delete the remaining duplicate node
        if(minParent != nullptr)
            minParent->left = nullptr;
        delete min;
    }
}
}

Here we are. This seems to work. If anyone has any suggestions as to how I can optimize this or a problem that I need to fix please speak up! Thanks!

void BinaryTree::remove(int data){
// Is the data valid?
if(data < 0){
    cerr << data << " is not valid.  Must be a positive integer value."  << endl;
}
else{
    // Find the node
    BinNode* loc = root;
    BinNode* parent = nullptr;
    bool found = false;
    while(!found && loc != nullptr){
        if(data > loc->data){
            parent = loc;
            loc = loc->right;
        }
        else if(data < loc->data){
            parent = loc;
            loc = loc->left;
        }
        else found = true;

    // If there are children, save pointers to them
    BinNode* leftChild = nullptr;
    BinNode* rightChild = nullptr;  
    if(loc->left != nullptr)
        leftChild = loc->left;
    if(loc->right != nullptr)
        rightChild = loc->right;

    // So now pointers to the children have been saved (if they exist) and
    // parent pointers have been taken care of (if they exist) the node can be deleted
    // If no children exist simply just delete the node and return

    // Check if two children exist
    if(leftChild != nullptr && rightChild != nullptr){

        // Find a minimum in the right subtree
        BinNode * min = loc->right;
        BinNode * minParent = loc;
        while(min->left != nullptr){
            minParent = min;
            min = min->left;
        }
        // Replace value of the node to be removed with the found minimum
        loc->data = min->data;

        // Delete the duplicate
        if(minParent != loc)
            minParent->left = nullptr;
        else minParent->right = nullptr;
        delete min;
    }

    // If one child exists
    // Need to handle if it is the root here
    // change root pointer to remaining child
    else if(leftChild != nullptr || rightChild != nullptr){ 
        // If there is a parent, take care of the pointer
        if(parent != nullptr){
            if(loc->data < parent->data)
                parent->left = nullptr;
            else if(loc->data > parent->data)
                parent->right = nullptr;

        // Now loc can be deleted
        delete loc;
        if(leftChild != nullptr){
            if(parent != nullptr){
                if(leftChild->data < parent->data)
                    parent->left = leftChild;
                else if(leftChild->data > parent->data)
                    parent->right = leftChild;
            }
            // If it is the root
            else{
                root = leftChild;
            }
        }
        else if(rightChild != nullptr){
            if(parent != nullptr){
                if(rightChild->data < parent->data)
                    parent->left = rightChild;
                else if(rightChild->data > parent->data)
                parent->right = rightChild;
            }
            // If it is the root
            else{
                root = rightChild;
            }
        }       
    }

    // If there are no children
    else if (leftChild == nullptr && rightChild == nullptr){
        // If there is a parent, take care of the pointer
        if(parent != nullptr){
            if(loc->data < parent->data)
                parent->left = nullptr;
            else if(loc->data > parent->data)
                parent->right = nullptr;
        }
        // If it is the root
        else 
            root = nullptr;
        delete loc;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM