delete a node in binary

hi everybody. Iam trying to delete a node(has no children). for some reason, it keeps giving me an error. any help is appreciated
thanks
void AVLtree<T>::remove(const T& item)
{
node* curr = root;
node* prev = nullptr;
node* next = nullptr;
if(curr == nullptr)
{
return;
}
while(curr != nullptr)
{
if(curr->data == item)
{
if(curr->right == nullptr && curr->left == nullptr)
{
delete curr;
//..//curr = NULL;
}
if(curr->right == nullptr && curr->left != nullptr)
{
if(height() == 2)
{
prev = curr;
prev->left = curr->left;
delete curr;
root = prev->left;
}
if(height() > 2)
{
next = curr;
next = curr->left;
prev->left = next;
delete curr;
}
}
}
else if(curr->data > item)
{
//prev = curr;
curr = curr->left;
}
else
{
//prev = curr;
curr = curr->right;
}
}
}

the error is: Access violation reading location 0xfeeefeee.
Last edited on
Since that code isn't indented (because you didn't use code tags, which you can get from the <> button to the right of the editing box), it's really tedious to read.

But, somewhere, you're attempting to read memory you don't have access to.

These lines in particular seem suspicious:
1
2
3
4
prev = curr;
prev->left = curr->left;
delete curr;
root = prev->left;


Prev is set to point to the same memory as curr. After you delete the memory pointed to by curr, prev also points to free memory, yet you attempt to dereference it. Did you mean to make a copy of the object pointed to by curr when you did that assignment operation?

-Albatross

Last edited on
thanks
Topic archived. No new replies allowed.