我无法弄清楚二叉树插入方法中的逻辑问题

我尝试在 Java 中为二叉树开发一个简单的 insertOperation 我没有成功。


我的包由三个类组成(Tree、Node、Main) 怎么会有逻辑思维错误?我不需要记录不同的代码。在互联网上,有运行的例子。我认为可能正在运行,但事实并非如此。


public class Node {


    Node left = null;

    Node right = null;

    float value = 0;


    public Node(float value) {


        this.value = value;

        left = null;

        right = null;

    }

}



import java.util.logging.Logger;


public class Tree {


    Logger log = Logger.getLogger(Tree.class.getName());


    Node root;


    public Tree() {

        root = null;

    }



    public void insert(Node node) {

    if (node == null) {

        throw new NullPointerException("Einzufügendes Objekt ist Null");

    }


    if (root == null) {

        root = node;


        log.info("root.value:" + root.value);


    } else if (root.value > node.value) {


        if (node.left == null) {


            root.left = node;

            log.info("node.left.value: " + root.left.value);

        }


        else {


            log.info("insert(root.left): " + root.left.value);

            insert(root.left);

        }


    } else {


        if (node.right == null) {


            root.right = node;

            log.info("node.right.value: " + root.right.value);

        }


        else {


            log.info("insert(node.right): " + root.right.value);


            insert(root.right);

        }

    }

}

}

预期结果是,当我执行此操作时,使用来自互联网的其他运行方法执行了我的 insertOperation:


Juli 27, 2019 6:13:31 NACHM. Main main

INFORMATION: Rot:----------------------------

Juli 27, 2019 6:13:31 NACHM. Main main

INFORMATION: tree.root.value1.0

Juli 27, 2019 6:13:31 NACHM. Main main

INFORMATION: Linker Teilbaum:--------------------------

Juli 27, 2019 6:13:31 NACHM. Main main

INFORMATION: tree.root.left.value)-7.0

Juli 27, 2019 6:13:31 NACHM. Main main

INFORMATION: tree.root.left.left.value)-8.0

Juli 27, 2019 6:13:31 NACHM. Main main

INFORMATION: root.left.right.value-7.0


这是应该创建的树


     1

  /     \

-7       2

/ \ / \ -8 -7 1 3 \ \ -4 10


小唯快跑啊
浏览 175回答 1
1回答

拉风的咖菲猫

你有几个问题。在下面的代码中查看我的评论。&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (root.value < node.value) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (node.right == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; root.right = new Node(node.value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.info("node.right.value:" + root.right.value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else { //<--- delete the right(}) curly brace.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// because your else clause is in the wrong place&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.info("insert(node.right):" + root.right.value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; insert(root.right);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }正如我在评论中所说,您需要停止使用root进行比较。不会更改root以反映递归调用中的节点更改。所以你一遍又一遍地替换相同的值。更新答案从这里开始。这是我在保持代码不变方面所能做的最好的事情。主要问题是您一遍又一遍地使用相同的 root 值。所以我添加了一个插入方法,它同时获取了根和节点。我不会这样做的。这就是我会做的。首先通过values,而不是nodes插入方法。制作一个second insert method需要 anode和的 a&nbsp;value。如果 theroot为 null,则创建 anode with the value并分配它。insert(node, value)否则,根据需要递归调用using left 或 right。如果null,则创建一个具有值的新节点并分配它。这是你的修改版本。我还添加了一个静态打印例程。public class TreeDemo {&nbsp; &nbsp;public static void main(String[] args) {&nbsp; &nbsp; &nbsp; Tree t = new Tree();&nbsp; &nbsp; &nbsp; t.insert(new Node(-1));&nbsp; &nbsp; &nbsp; t.insert(new Node(5));&nbsp; &nbsp; &nbsp; t.insert(new Node(8));&nbsp; &nbsp; &nbsp; t.insert(new Node(-10));&nbsp; &nbsp; &nbsp; t.insert(new Node(4));&nbsp; &nbsp; &nbsp; t.insert(new Node(-3));&nbsp; &nbsp; &nbsp; t.insert(new Node(9));&nbsp; &nbsp; &nbsp; t.insert(new Node(12));&nbsp; &nbsp; &nbsp; print(t.root);&nbsp; &nbsp;}&nbsp; &nbsp;public static void print(Node n) {&nbsp; &nbsp; &nbsp; if (n.left != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print(n.left);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; // print inorder&nbsp; &nbsp; &nbsp; System.out.println(n.value);&nbsp; &nbsp; &nbsp; if (n.right != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print(n.right);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;}}class Node {&nbsp; &nbsp;Node&nbsp; left&nbsp; = null;&nbsp; &nbsp;Node&nbsp; right = null;&nbsp; &nbsp;float value = 0;&nbsp; &nbsp;public Node(float value) {&nbsp; &nbsp; &nbsp; this.value = value;&nbsp; &nbsp; &nbsp; left = null;&nbsp; &nbsp; &nbsp; right = null;&nbsp; &nbsp;}}class Tree {&nbsp; &nbsp;Node root;&nbsp; &nbsp;public Tree() {&nbsp; &nbsp; &nbsp; root = null;&nbsp; &nbsp;}&nbsp; &nbsp;public void insert(Node node) {&nbsp; &nbsp; &nbsp; if (root == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;root = node;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; insert(root, node);&nbsp; &nbsp;}&nbsp; &nbsp;// added this method&nbsp; &nbsp;private void insert(Node troot, Node node) {&nbsp; &nbsp; &nbsp; if (troot.value > node.value) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (troot.left == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; troot.left = node;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; insert(troot.left, node);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (troot.value <= node.value) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (troot.right == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;troot.right = node;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;insert(troot.right, node);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java