Okay so I’m reading some code about RedBlackTrees. And I noticed this line “v1 = v2 = v3 = v4;” and I understand something like “v1 += v2” (add v2 to the current value of v1) and “v1 = v2” (create a reference from v2 to v1) etc.
public void insert( AnyType item )
{
current = parent = grand = header;
But I’m curious to what is happening in the memory/references with current = parent = grand = header;
http://faculty.washington.edu/moishe/javademos/REDBlack/RedBTree.java
Edit : 10:46 PM
I still have to wait 10 minutes to approve questions, sorry for the wait ladies and gents.
What’s happening is that the value of
headeris assigned to the value ofgrandwhich is assigned to the value ofparentwhich in turns gets assigned tocurrent. In the end, the 4 variables hold the same value; this idiom is used to quickly initialize several variables to the same value and it’s equivalent to this:The assignment occurs from right-to-left, in fact the expression in the question is evaluated like this:
This works because the result of the assignment operator actually evaluates to the assigned value, for example this works:
In the last snippet,
42gets assigned toxand then the value ofxgets returned.