binary search tree delete problem
Apr 29, 2012 at 4:30pm UTC
I copied this code out of the book for deleting a node and it doesn't seem to work. The only part that works is when the node to be deleted has 2 children. Does anyone know what the problem is?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
void bSearchTreeType<elemType>::deleteFromTree
(nodeType<elemType>* &p)
{
nodeType<elemType> *current; //pointer to traverse the tree
nodeType<elemType> *trailCurrent; //pointer behind current
nodeType<elemType> *temp; //pointer to delete the node
if (p == NULL)
cout << "Error: The node to be deleted is NULL."
<< endl;
else if (p->lLink == NULL && p->rLink == NULL)
{
temp = p;
p = NULL;
delete temp;
}
else if (p->lLink == NULL)
{
temp = p;
p = temp->rLink;
delete temp;
}
else if (p->rLink == NULL)
{
temp = p;
p = temp->lLink;
delete temp;
}
if p is the right node to q, then does setting p = NULL work the same as setting q->right = NULL?
I feel like that is where the problem is.
Last edited on Apr 29, 2012 at 4:38pm UTC
Apr 29, 2012 at 6:16pm UTC
If you think you know where the problem is, then try to fix it that way! Otherwise say: I have no idea how to solve this!
Topic archived. No new replies allowed.