0

While writing program to delete a node from BST, I am not able to make a node null.

Don't know what wrong I am doing.

MainClass:

TreeNode rootNode = null;
rootNode = new TreeNode().insert(75);
rootNode.delete(rootNode, 75);

In TreeNode Class, I have the method delete as(just showing the relevant part of the method):

public void delete(TreeNode node, int valueToDelete) {
    if(node == null) {//node supplied is null
        return;
    }
    node = null;
}

In the method delete(), I am setting rootNode itself as null, still its not null, when I reach back to my MainClass. Before and after delete, the value 75 remains as it is in the tree whose root node is pointed by rootNode. I don't know why.

Jivi
  • 94
  • 1
  • 3
  • 8
  • You are still holding onto a reference of the rootNode... `rootNode.delete(rootNode` – OneCricketeer Feb 13 '16 at 16:25
  • 1
    You may want to consider returning a Node or null value from your delete method so you can do `rootNode = rootNode.delete(value)`. Returning null from delete will do what you want – OneCricketeer Feb 13 '16 at 16:28
  • I suggest you look at how the builtin libraries implement a Tree even if you can't use them yourself. – Peter Lawrey Feb 13 '16 at 17:58

0 Answers0