【发布时间】:2011-12-04 02:00:47
【问题描述】:
我当前的二叉树插入方法没有插入到作为其父级左子节点的任何节点的右侧。 当前代码:
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;
}
【问题讨论】:
标签: java binary-tree binary-search-tree