Below is the code I wrote for the case when the deleteItem is at the leaf node. Even if I am equating the found leaf node to "null" then also when I print the inorder traversing order if the tree, that element is not deleted and comes on the screen. What am I missing?
public void deleteNode(T deleteItem)
{
TreeNode<T> node = root;
if(root == null)
{
System.out.print("Binary Tree does not exist");
System.exit(0);
}
while((node.data != deleteItem) && (node.leftNode != null || node.rightNode != null))
{
if(deleteItem.compareTo(node.data)<0)
node = node.leftNode;
else
node = node.rightNode;
}
//System.out.printf("deleting item is: %s\n", node.data);
if(node.data != deleteItem)
{
System.out.println("\ndeleteItem not found in the tree");
System.exit(0);
}
else
{
if(node.leftNode == null && node.rightNode == null)
{
node = null;
}
}
}