My current insert method for my binary tree is not inserting to the right of any node that is the left child of its parent.
current code:
private BinaryTreeNode insert(BinaryTreeNode current, String word) {
if (current == null) {
current = new BinaryTreeNode(word);
} else {
if (word.compareToIgnoreCase(current.value) < 0) { // if smaller than current node
if (current.left != null) {
if (word.compareToIgnoreCase(current.left.value) < 0) {// check next node for lesser than,
current.left = (insert(current.left, word));
}
} else {
current.left = new BinaryTreeNode(word);// iff current node is end of tree
System.out.println(word + "left");
}
} else {
if (current.right != null) { // if larger than current node
current.right = (insert(current.right, word));
} else {
current.right = new BinaryTreeNode(word); // if current node is end of tree
System.out.println(word + "right");
}
}
}
return current;
}
Your problem lies here:
What are you expecting this to do? You already know you should be inserting to the left of the current node, but why are you re-checking the next node down here?