I have been working on this method for six days without any luck. I should have talked to you earlier. This method is from the book, but it only is deleting California. I cannot figure out why. I tried a couple different methods(recursive and others) and none of them worked. It is moving through a tree of state objects which are String objects. current.myState is the String of the State object. leftChild and rightChild are also State objects.
public boolean delete(String key) // delete node with given key
{ // (assumes non-empty list)
State current = root;
State parent = root;
boolean isLeftChild = true;
while(!current.myState.equals(key))
{
parent = current;
if(!key.equals(current.myState))
{
isLeftChild = true;
current = current.leftChild;
}
else
{
isLeftChild = false;
current = current.rightChild;
}
if(current == null)
return false;
} // end while
// found node to delete
// if no children, simply delete it
if(current.leftChild==null && current.rightChild==null)
{
if(current == root)
root = null;
else if(isLeftChild)
parent.leftChild = null;
else
parent.rightChild = null;
}
// if no right child, replace with left subtree
else if( key.compareTo( current.myState ) < 0 )
if(current == root)
root = current.leftChild;
else if(isLeftChild)
parent.leftChild = current.leftChild;
else
parent.rightChild = current.leftChild;
// if no left child, replace with right subtree
else if( key.compareTo( current.myState ) > 0 )
if(current == root)
root = current.rightChild;
else if(isLeftChild)
parent.leftChild = current.rightChild;
else
parent.rightChild = current.rightChild;
else // two children, so replace with inorder successor
{
// get successor of node to delete (current)
State successor = getSuccessor(current);
// connect parent of current to successor instead
if(current == root)
root = successor;
else if(isLeftChild)
parent.leftChild = successor;
else
parent.rightChild = successor;
// connect successor to current's left child
successor.leftChild = current.leftChild;
} // end else two children
return true;
} // end delete()
//------------------------------------------------------------------------------
private State getSuccessor(State delNode)
{
State successorParent = delNode;
State successor = delNode;
State current = delNode.rightChild;
while(current != null)
{
successorParent = successor;
successor = current;
current = current.leftChild;
}
if(successor != delNode.rightChild)
{
successorParent.leftChild = successor.rightChild;
successor.rightChild = delNode.rightChild;
}
return successor;
}//end getSuccessor
For my Deletes I have. These are also supposed to start out with a new tree each time. I thought about deleting and inserting back, but that doesn't seem very efficient to me.
public void deleteStates()
{
System.out.println("\n\nAfter deleting nodes with zero children:");
delete("Maryland");
inOrder();
System.out.println("\n\nAfter deleting nodes with one child:");
delete("California");
delete("Massachusetts");
inOrder();
System.out.println("\n\nAfter deleting nodes with two children:\n");
delete("Vermont");
delete("New_York");
inOrder();
}//end deleteStates()
My output
After deleting nodes with zero children:
California
Colorado
Connecticut
Delaware
Maine
Maryland <--should be deleted
Massachusetts
New_Hampshire
New_Jersey
New_York
Pennsylvania
Rhode_Island
Vermont
Virginia
West_Virginia
After deleting nodes with one child:
Colorado
Connecticut
Delaware
Maine
Maryland
Massachusetts <--Should be deleted, but it deleted California
New_Hampshire
New_Jersey
New_York
Pennsylvania
Rhode_Island
Vermont
Virginia
West_Virginia
After deleting nodes with two children:
Colorado
Connecticut
Delaware
Maine
Maryland
Massachusetts
New_Hampshire
New_Jersey
New_York <--Should be Deleted
Pennsylvania
Rhode_Island
Vermont <-- Should be Deleted
Virginia
West_Virginia