I find that confusion in this matter generally boils down to not understanding how objects are passed in Java.
With updateNodeData as demonstrated in the code below, you'll be passing a copy of the reference by value as the first argument. That node's data would then be modified (assuming data is public).
Similarly, nodeToBeChanged's leftNode would also now refer to the same node as that passed in newLeftNode. A new Node with copied values isn't cloned/created.
public void updateNodeData(Node node, int newValue){
node.data = newValue;
}
public void setLeftNode(Node nodeToBeChanged, Node newLeftNode)
{
nodeToBeChanged.leftNode = newLeftNode;
}
However, what is often confusing is that because it's a copy of the reference that is passed by value, you cannot do a swap of two Nodes as demonstrated below. You'll be swapping the copies and not the actual references.
public void swap(Node node1, Node node2)
{
Node tmpNode = node1;
arg1 = arg2;
arg2 = tmpNode;
}
Now to come back to the original question. If a Node tree were to be implemented with a Node such as this one:
public class Node {
private int data;
private Node left;
private Node right;
...
}
There WILL NOT be different clones of the same Node all over the place wasting memory. It will just be a long chain of nodes and references.