我的二叉樹的當前插入方法未插入到其父節點的左側子節點的右側。 當前代碼:二元樹沒有插入到任何節點的右側,是另一個節點的左側子節點
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;
}
感謝您指出了這一點,我不知道,爲什麼我有這樣的線在那裏。我已經刪除它,現在正在等待測試用例完成運行。 –