1

Very quick question. I am working on the deletion method of a trinary tree. My approach is simple, I delete and join by replacing the node object with the node.left object.

 public void delete(int key, Node node) {
    if (node == null) {
      throw new RuntimeException();
    } else if (key < node.key) {
       delete(key, node.left) ;
    } else if (key > node.key) {
       delete(key, node.right);
    } else { 
      //found the node to delete, delete from here.
      if (node.mid != null) {
          node = node.mid;
      }
      else if (node.left == null) {
          node = node.right;
      }    
      else if (node.right == null) {
          node = node.left;
        //node.key=node.left.key;
      }
      else {
        node= findMin(node.right);
      } 
    }

  }  

This is part of the code. However, it doesn't work, that is, its key node.key is still the same.

else if (node.right == null){
          node = node.left; 
      }

But if I write node.key explicitly, then it works.

 else if (node.right == null){
          node.key=node.left.key;
      }

Why was that? I can never understand the object and reference thing, geez... Thanks guys.

venergiac
  • 7,469
  • 2
  • 48
  • 70
Zvx
  • 19
  • 2
  • What is `node` an instance variable or a local variable in the method doing the deletion? I assume it is a local variable in the method. If that is the case, then you are only modifying the reference, not the object. For example if you do `Node someNode = new Node();`, you create a new new Node object and the someNode reference points to that new object. If you pass someNode to a method, that method will receive a reference to the same Node object. But changing the reference in that method will not affect the original object, nor the other reference (someNode). – Alderath Jan 02 '14 at 08:24
  • entire code please... Just this piece of code is not sufficient... We will only be able to guess, not answer.. – TheLostMind Jan 02 '14 at 08:29

2 Answers2

0

That is because in first case you are not changing the key at all which is supposed to keep track of the next node to be addressed

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
  • Sorry I don't really understand, could you explain bit more? I changed the whole node object with node.left object, why isn't the variable changed? – Zvx Jan 02 '14 at 08:25
0

The question is not completely clear, but I think you have to see java specifications: java passes value by copy. So, if the code specified is into a method and node is a parameter of the function, the passed object will be NOT changed, referenced object (in your case "key") yes.

See Is Java "pass-by-reference" or "pass-by-value"?

I hope it helps.

Community
  • 1
  • 1
venergiac
  • 7,469
  • 2
  • 48
  • 70
  • I think you are close to the answer. But as I know, what is copied is the "reference of the object", not the whole object, the object should be only one and be changed. What do I missed? – Zvx Jan 02 '14 at 08:46
  • Now I see the function: change "void delete" to "Node delete" and return the node. Yes java passes the reference then if you change the refrence it will work, but you are changing the local member. Let me an example: a -> ObjectNode [location memory ab12345], delete receive the reference b -> ObjectNode [location memory ab12345]; b is changed with the new node b -> NewObjectNode [location memory affff14]; a remainede the same. – venergiac Jan 02 '14 at 08:52
  • Change void? What type should I change it to? – Zvx Jan 02 '14 at 08:55
  • Finally consider also that java formally passes parameter by value not by reference. See http://www.javaworld.com/article/2077424/learn-java/does-java-pass-by-reference-or-pass-by-value.html – venergiac Jan 02 '14 at 09:04
  • I got it, finally. The local "node" is a new different node. So the original "node" outside the function isn't changed. Is it like that? Thanks you. – Zvx Jan 02 '14 at 11:24
  • yes.. the original variable is referenced to original node when you change the local (function) variable you change only the local variable: more formally, java passes variable by value copy. – venergiac Jan 02 '14 at 11:46