Question

This is for a red black tree.

For the pseudocode "p[z] <-- y", would the interpretation in java be:

z.getParent() = y;

or

z.setParent(y);

Thanks :)

Was it helpful?

Solution

The correct code would be z.setParent(y);

Be aware that z.getParent() = y; is invalid code. The assignment operator = stores the result of evaluating the expression to the right of the = into a variable, attribute or array position to the left of the =. In an expression like the first one in your question, you'd be trying to assign the value y into the result of calling z.getParent() - that is, trying to assign a value into another value, and that won't work.

The right way to change an attribute is by calling the corresponding setXXX() method, or by directly assigning to the attribute if it was declared non-private, like this: z.parent = y;

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top