I am trying to implement a self-balancing binary search tree. When I try to get the height of the right branch, it works until I reach this point. Then I get a segmentation fault error and valgrind says there was an invalid read of size 8. The odd thing is when I look at the tree when this error occurs, the right branch isn't null. Is there some check that I am forgetting to do that is causing this error?
Lines where error is thrown:
int rightChildHeight = 0;
if (n->right != NULL)
rightChildHeight = n->right->getHeight();
getHeight():
int Node::getHeight()
{
// No kids => 2
if (!left && !right)
return 1;
// 1 kid => height of kid +1
else if (!left && right)
return right->getHeight() + 1;
else if (left && !right)
return left->getHeight() + 1;
// 2 kids => height of taller kid +1
else
return max(left->getHeight(), right->getHeight()) + 1;
}
Tree when the error occurs:
Aucun commentaire:
Enregistrer un commentaire