Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.0 kB
0
Indexable
Never
void BST::remove(int a){
    node* del = search(a);
    if(del->left != NULL && del->right != NULL){
        node* delReplacement = search(successor(a));
        int replacement = delReplacement->item;
        delReplacement->item = del->item;
        del->item = replacement;
        remove(replacement);
    }else{
        if(del->right != NULL){
            node* parent = del->parent;
            if(parent->left = del){
                parent->left = del->right;
                del->right->parent = parent;
            }else if(parent -> right = del){
                parent->right = del->right;
                del->right->parent = parent;
            }
        }else if(del->left != NULL){
            node* parent = del->parent;
            if(parent->left = del){
                parent->left = del->left;
                del->right->parent = parent;
            }else if(parent -> right = del){
                parent->right = del->left;
                del->right->parent = parent;
            }
        }
        delete del;
    }
}