Reputation: 3737
So I'm implementing a BST right now and I'm trying to write a successor function. This is what I have right now:
int BinarySearchTree::TREE_SUCCESSOR(node* x)
{
node* y = NULL;
if (x->right != NULL)
{
return FIND_MIN(x->right);
}
else
{
y = x->parent;
while (y != NULL && x = y->right)
{
x = y;
y = y->parent;
}
return y->key;
}
}
however on this line:
while (y != NULL && x = y->right)
I get this error on y
:
Expression must be a modifiable lvalue
Why am I getting this error?
Any help would be greatly appreciated, thanks!!
Upvotes: 1
Views: 704
Reputation: 311018
It seems that you mean the following
while (y != NULL && x == y->right)
^^^
As for the meaning of the error message then when you use the assignment operator then its priority is less than the priorities of equality and logical operators and you have in fact
while ( ( y != NULL && x ) = y->right)
Take into account that it is much better to place the declaration of y
in the code block of the else statement because it is used only in this scope
//...
else
{
node* y = x->parent;
Upvotes: 6