java - Delete a node from a binary search tree -
i new binary search trees , deleting node giving me problems. tried draw out problem see doing wrong , still cannot seem see problem , not want copy code website.
i understand how delete node 1 child or no children , think code correct methods. problem deleting node 2 children. cannot work properly. or advice appreciated.
public void deletenode(int number) { if (root == null) { joptionpane.showmessagedialog(null," tree empty, can not delete ", joptionpane.warning_message); return; } node child = root; node parent = root; while (number != child.data) { if (number < child.data) { parent = child; child = child.left; } else if (number > child.data) { parent = child; child = child.right; } if(child == null){ joptionpane.showmessagedialog(null," number not found",joptionpane.warning_message); return; } } if(child.right == null && child.left == null) { hasnochildren(child, parent); } else if(child.left != null && child.right != null) { hastwochildren(child, parent); } else if (child.right != null && child.left == null) { hasrightchild(child, parent); } else if (child.left != null && child.right == null) { hasleftchild(child, parent); } }
this method delete node 2 children
public void hastwochildren(node child, node parent) { node temp = null; if(child.data < parent.data){ node childorg = child; temp = child; child = child.left; while(child.right != null){ temp = child; child = child.right; } childorg.data = child.data; if (child.left != null && child.right == null) { hasleftchild(child, temp); }else{ temp.right = null; } } else { node childorg = child; temp = child; child = child.right; while(child.left != null){ temp = child; child = child.left; } childorg.data = child.data; if (child.left != null && child.right == null) { hasrightchild(child, temp); }else{ temp.left = null; } } }
these methods delete node no children or 1 child
public void hasnochildren(node child, node parent) { if(child.data == root.data) { root = null; } else if(child.data < parent.data){ parent.left = null; }else{ parent.right = null; } } public void hasleftchild(node child, node parent){ if(child.data < parent.data){ parent.left = child.left; }else{ parent.right = child.left; } } public void hasrightchild(node child, node parent){ if(child.data < parent.data){ parent.left = child.right; }else{ parent.right = child.right; } }
here complete binary tree deletion implementation possible cases :
public boolean delete(node nodetodelete) { boolean succes = false; node nodetoremove = findthenodetodelete(nodetodelete); if (nodetoremove == null) { return succes; } // case1; if node has no children if (nodetoremove.left == null && nodetoremove.right == null) { if (nodetoremove.parent.left.data == nodetodelete.data) { nodetoremove.parent.left = null; nodetoremove = null; succes = true; return succes; } else if (nodetoremove.parent.right.data == nodetodelete.data) { nodetoremove.parent.right = null; nodetoremove = null; succes = true; return succes; } // case 2: if has 1 children } else if ((nodetoremove.left == null && nodetoremove.right != null) || (nodetoremove.right == null && nodetoremove.left != null)) { if (nodetoremove.left != null) { nodetoremove.parent.left = nodetodelete.left; nodetodelete = null; succes = true; return succes; } else if (nodetoremove.parent.right != null) { nodetoremove.parent.right = null; nodetoremove = null; succes = true; return succes; } } // case 3 : if (nodetoremove.left != null && nodetoremove.right != null) { node minleftnode = findtheleftmostnodefromtherightsubtree(nodetoremove.right); system.out.println("----" + minleftnode.data); // if parent of node not null means // root // assign left mode min root , // min.right=notetoremove.right; // derefrence min node /remove tree minleftnode.parent.left = null; minleftnode.parent = null; node parentofthenodetodelete = nodetoremove.parent; minleftnode.parent = parentofthenodetodelete; node rightofnodetodelete = nodetoremove.right; node leftofnodetodelete = nodetoremove.left; if (parentofthenodetodelete == null) { root = minleftnode; } else { if (parentofthenodetodelete.right.data == nodetodelete.data) { parentofthenodetodelete.right = minleftnode; } else if (parentofthenodetodelete.left.data == nodetodelete.data) { parentofthenodetodelete.left = minleftnode; } } minleftnode.right = rightofnodetodelete; minleftnode.left = leftofnodetodelete; nodetoremove = null; } return succes; }
Comments
Post a Comment