【问题标题】:Binary tree not inserting to right of any node that is the left child of another node二叉树不插入到作为另一个节点的左子节点的任何节点的右侧
【发布时间】: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


    【解决方案1】:

    你的问题在这里:

    if (word.compareToIgnoreCase(current.left.value) < 0) {// check next node for lesser than,
        current.left = (insert(current.left, word));
    }
    

    您希望这能做什么?你已经知道你应该插入到当前节点的左边,但是你为什么要在这里重新检查下一个节点呢?

    【讨论】:

    • 感谢您指出这一点,我不知道为什么我有那条线。我已将其删除,正在等待测试用例完成运行。
    【解决方案2】:

    你应该递归而不是向下比较到左边:

    private static BinaryTreeNode insert(BinaryTreeNode current, String word) {
        if (current == null) {
            current = new BinaryTreeNode(word);
        } else {
            int test = word.compareToIgnoreCase(current.value);
            if (test < 0) {
                current.left = insert(current.left, word);
            } else if (test > 0) {
                current.right = insert(current.right, word);
            }
            // else word already at this node!
        }
        return current;
    }
    

    注意函数应该是静态的,因为它不依赖于this

    【讨论】:

    • (要挑剔:OP 应该“使用递归”,insert 方法应该“recur”。我猜 OP 已经对这个项目做了足够多的诅咒,他没有不需要“递归”。)
    【解决方案3】:

    我认为有一些错误...我会这样做:

    private void insert(BinaryTreeNode current, String word) {
        if (current == null) {
            current = new BinaryTreeNode(word);
        } else {
            if (word.compareToIgnoreCase(current.value) < 0) {
    
                if (current.left != null) {
                    insert(current.left, word);
                } else {
                    current.left = new BinaryTreeNode(word);
                    System.out.println(word + "left");
                }
    
            } else {
    
                if (current.right != null) {
                    insert(current.right, word);
                } else {
                    current.right = new BinaryTreeNode(word); 
                    System.out.println(word + "right");
                }
    
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      相关资源
      最近更新 更多