Java: setting an object to null inside a method has no effect (code reuse)

I am trying to write a method to remove Node from a binary search tree. Here is my method for removing node.

public void delete(int deletionNodeValue) { Node<Integer> nodeToBeDeleted = getNode(deletionNodeValue); if(nodeToBeDeleted == null) return; // No node with such value exists throw an error if(isLeafNode(nodeToBeDeleted)) { nodeToBeDeleted = null; } else if (nodeToBeDeleted.getNumChildren() == 1) { bypassNode(nodeToBeDeleted); }else { replace(nodeToBeDeleted, getSuccessor(nodeToBeDeleted.getValue())); } } 

I tested this method on the node sheet, although after debugging I found that the execution of nodeToBeSelected=null is running, Node is not actually deleted. Since I can still find the remote value, and the program still manages to retrieve it.

 tree.add(5); tree.delete(5); System.out.println(tree.getNode(5).getValue()); // Output : 5, should've been deleted 

Here is my getNode () method

 public Node<Integer> getNode(int searchValue) { Node<Integer> currentNode = root; while(currentNode != null) { int currentNodeValue = currentNode.getValue(); if(searchValue == currentNodeValue) return currentNode; else if(searchValue < currentNodeValue) currentNode = currentNode.getLeftChild(); else currentNode = currentNode.getRightChild(); } // if no node with given value is found return null; } 

The getNode () method returns the found Node by value? How can I make it return a link and directly manipulate the found node?

+6
source share
3 answers

You need to remove the node from the tree, not locally in your program.

 Node<Integer> nodeToBeDeleted = getNode(deletionNodeValue); 

gives a copy of the node in the tree.

 nodeToBeDeleted = null; 

sets this copy to null. The connection to the tree is not deleted because it is part of the node object. To remove a connection, you need to write another method to remove the node, and this should contain something like

 parent.leftNode = null; // if nodeToBeDeleted == leftNode parent.rightNode = null; // if nodeToBeDeleted == rightNode 
+5
source

When you say nodeToBeDeleted = null; inside the delete method, you really don't call the Node returned by the getNode method to start pointing to null .

Java is always pass-by-value . This means that you cannot link to the method in a new memory location inside the method. Similarly, you cannot make the link returned by the method call point to a new memory location inside another method. (Even if this place, well .. zero).

According to the explanation above, it's almost impossible to use the getNode method to get a Node that you don't want to remove, and then make this node a null reference in another way. A quick fix would be to duplicate the code in the getNode method inside the delete method. You must add the setLeftChild and setRightChild to Node (as opposed to leaving leftChild and rightChild public, as suggested by others). Then you can set it to zero:

nodeToBeDeleted.setLeftChild(null)

+4
source

When you set nodeToBeDeleted to null , you only set the value of a local variable that contains a reference to the actual object. The actual object is not deleted in any way.

With the code shown here, to remove a node, you must find its parent and set the link to this node (leftChild or rightChild) to null. This ensures that the object will not be referenced by its parent, it may no longer be displayed using any link, and therefore will have the right to garbage collection.

+4
source

Source: https://habr.com/ru/post/988263/


All Articles