I'm writing an iterative delete method for my BST class. I'm a little confused on what to do when I've found the node and I want to delete it. I've developed this small code and I've removed the other sections of code so I can show the problem I'm having.
The question I have is... should I delete curr as I'm doing? Or should I simply set it to NULL? I'm pretty sure I have to delete the current node, but if I do, the results aren't correct. If I just make current NULL, it's fine. I'm pretty sure my other functions are correct. Any help is appreciated.
Code:
// code for 2 children and zero children here (which I haven't written)
...
...
// code for the case when there is only one child
// curr points to the node being deleted
else {
if (curr == root) {
// update root to be the child
root = (curr->left != NULL) ? curr->left : curr->right;
delete curr;
curr = NULL;
}
}