亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我無法弄清楚二叉樹插入方法中的邏輯問題

我無法弄清楚二叉樹插入方法中的邏輯問題

小唯快跑啊 2023-04-26 14:33:46
我嘗試在 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 mainINFORMATION: Rot:----------------------------Juli 27, 2019 6:13:31 NACHM. Main mainINFORMATION: tree.root.value1.0Juli 27, 2019 6:13:31 NACHM. Main mainINFORMATION: Linker Teilbaum:--------------------------Juli 27, 2019 6:13:31 NACHM. Main mainINFORMATION: tree.root.left.value)-7.0Juli 27, 2019 6:13:31 NACHM. Main mainINFORMATION: tree.root.left.left.value)-8.0Juli 27, 2019 6:13:31 NACHM. Main mainINFORMATION: root.left.right.value-7.0這是應該創建的樹     1  /     \-7       2/ \ / \ -8 -7 1 3 \ \ -4 10
查看完整描述

1 回答

?
拉風的咖菲貓

TA貢獻1995條經驗 獲得超2個贊

你有幾個問題。


在下面的代碼中查看我的評論。


            if (root.value < node.value) {

                if (node.right == null) {

                    root.right = new Node(node.value);

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

                }

            } else { //<--- delete the right(}) curly brace.

                     // because your else clause is in the wrong place

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

                insert(root.right);

            }

        }

正如我在評論中所說,您需要停止使用root進行比較。不會更改root以反映遞歸調用中的節點更改。所以你一遍又一遍地替換相同的值。

更新答案從這里開始。

這是我在保持代碼不變方面所能做的最好的事情。主要問題是您一遍又一遍地使用相同的 root 值。所以我添加了一個插入方法,它同時獲取了根和節點。我不會這樣做的。這就是我會做的。

  1. 首先通過values,而不是nodes插入方法。

  2. 制作一個second insert method需要 anode和的 a value。

  3. 如果 theroot為 null,則創建 anode with the value并分配它。

  4. insert(node, value)否則,根據需要遞歸調用using left 或 right。如果null,則創建一個具有值的新節點并分配它。

這是你的修改版本。我還添加了一個靜態打印例程。

public class TreeDemo {


   public static void main(String[] args) {

      Tree t = new Tree();

      t.insert(new Node(-1));

      t.insert(new Node(5));

      t.insert(new Node(8));

      t.insert(new Node(-10));

      t.insert(new Node(4));

      t.insert(new Node(-3));

      t.insert(new Node(9));

      t.insert(new Node(12));


      print(t.root);


   }


   public static void print(Node n) {

      if (n.left != null) {

         print(n.left);

      }

      // print inorder

      System.out.println(n.value);

      if (n.right != null) {

         print(n.right);

      }

   }

}


class Node {


   Node  left  = null;

   Node  right = null;

   float value = 0;


   public Node(float value) {


      this.value = value;

      left = null;

      right = null;

   }

}


class Tree {


   Node root;


   public Tree() {

      root = null;

   }

   public void insert(Node node) {

      if (root == null) {

         root = node;

         return;

      }

      insert(root, node);

   }

   // added this method

   private void insert(Node troot, Node node) {


      if (troot.value > node.value) {

         if (troot.left == null) {

            troot.left = node;

         }

         else {

            insert(troot.left, node);

         }

      }

      else {

         if (troot.value <= node.value) {

            if (troot.right == null) {

               troot.right = node;

            }

            else {

               insert(troot.right, node);

            }

         }

      }

   }

}


查看完整回答
反對 回復 2023-04-26
  • 1 回答
  • 0 關注
  • 205 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號