Manipulating object references

121
April 12, 2018, at 09:43 AM

Recently, I have been getting confused with how an object is referenced. The Node class below is presented to give some insight on how the get and set methods are implemented.

public class Node{
   private int data;
   private Node left;
   private Node right;
   public TreeNode(int data){
      this.data=data; 
      left=right=null; 
   }
   public Node getLeft(){
      return left;
   }
   public Node getRight(){
      return right;
   }
   public void setLeft(Node left){
      this.left = left;
   }
   public void setRight(Node right){
      this.right = right;
   }
}

private void changeNode(Node node){
    if(node.getLeft() == null & node.getRight() == null) {
        node = null;
    }
    if(node.getLeft() == null || node.getRight() == null) {
        if(node.getLeft() == null) {
            node = node.getRight();
        }
        else {
            node = node.getLeft();
        }
    }
    else {
        Node tempNode = node.getRight();
        Node pNode = node;
        while(tempNode.getLeft() != null) {
            pNode = tempNode;
            tempNode = tempNode.getLeft();
        }
        node.setData(tempNode.getData());       
        pNode.setLeft(tempNode.getRight());     '
        //****************************
        //pNode = pNode.getLeft();
        //pNode = tempNode.getRight();
        //****************************
    }
}   

Originally, I had

pNode = pNode.getLeft();
pNode = tempNode.getRight(); (shown in comments)

To me that seems like setting pNode's reference to pNode.left's reference. Then setting pNode.left's reference to tempNode.right's reference.

Another way of saying is I'm trying to set pNode's left child to tempNode's right child. However, it's clear this method doesn't work.

Instead, I should've used

pNode.setLeft(tempNode.getRight()); (shown uncommented)

I know the first implementation looks sloppier than the second but again it makes logical sense to me. In my perspective, I'm just swapping references around. Could someone please explain the flaws in my logic? Thanks!

READ ALSO
How to generate emoji unicode like “\uD83D\uDE02” when i got is “U+1F602”? [on hold]

How to generate emoji unicode like “\uD83D\uDE02” when i got is “U+1F602”? [on hold]

I'm currently create something like emoji-keyboard in android that will show list of emoticon, I dont want to use image, so i need unicode character for emoji in java source code to show emoticon in String output

152
Android JNI native library not packaged to apk

Android JNI native library not packaged to apk

While transforming my project from eclipse to ASI got a fatal error which caused by "the lib not found " problem

142
IDE to create spark project in Python [on hold]

IDE to create spark project in Python [on hold]

IntelliJ / Jupyter/ Pycharm ? What should be the best IDE to develop Apache spark project? I have created standalone programs using pyspark but when it comes to creating a full fledged projectwhat should be the best IDE?

141
What does {}. before a CSS Selector mean?

What does {}. before a CSS Selector mean?

I was working on some CSS files and I found this:

96