algorithm - Java : setting object to null within a method has no effect (Reusing code) -


i trying write method delete node binary search tree. here method delete node.

public void delete(int deletionnodevalue) {     node<integer> nodetobedeleted = getnode(deletionnodevalue);     if(nodetobedeleted == null) return; // no node such value exists throw error     if(isleafnode(nodetobedeleted)) {         nodetobedeleted = null;     } else if (nodetobedeleted.getnumchildren() == 1) {         bypassnode(nodetobedeleted);     }else {         replace(nodetobedeleted, getsuccessor(nodetobedeleted.getvalue()));     } } 

i checked method on leaf node, though after debugging discovered execution of nodetobeselected=null takes place, node isn't deleted. can still search deleted value , program still manages fetch it.

tree.add(5); tree.delete(5); system.out.println(tree.getnode(5).getvalue()); // output : 5, should've been deleted 

here 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 given value found     return null; } 

is getnode() method returning found node value? how can make return reference , directly manipulate found node?

you have delete node tree , not locally in program.

node<integer> nodetobedeleted = getnode(deletionnodevalue); 

gives copy of node in tree.

nodetobedeleted = null; 

sets copy null. connection tree not deleted because part of node object. delete connection have write method delete node , should contain like

parent.leftnode = null; // if nodetobedeleted == leftnode parent.rightnode = null; // if nodetobedeleted == rightnode 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -