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 :)

有帮助吗?

解决方案

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;

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top