【问题标题】:Couldn't insert values in BinaryTree [duplicate]无法在 BinaryTree 中插入值 [重复]
【发布时间】:2020-09-25 07:26:01
【问题描述】:
class bTree {
    public class Node {
        Node left;
        Node right;
        int val;

        Node () {}
        Node (int val){
            this.val=val;
        }
    }

    Node root;

    public void insert(int val){
        if (root == null){
            root = new Node(val);
        } else {
            Node current = root;
            // If val less than parent node's val go left
            if (val <= root.val){
                if (root.left == null){
                    root.left = new Node(val);
                } else {
                    insert(val);
                }
            }
            // If val greater than parent node's val go right
            else {
                if (root.right == null){
                    root.right = new Node(val);
                } else {
                    insert(val);
                }
            }              // inner else ends
          }           // outer else ends
    }     // insert() ends

    public void displayTree(Node root){
   
        if (root.left != null){
            displayTree(root.left);
        }
        System.out.print(root.val + " - "); 
        if (root.right != null){
            displayTree(root.right);
        }

    }

    public static void main(String[] args) {
        bTree bt = new bTree();
        bt.insert(10);
        bt.insert(30);
        bt.insert(4);
        bt.insert(5);
        bt.displayTree(bt.root);
    }
}

我在尝试实现二叉搜索树时遇到了在其中插入值的问题。我在创建 Node 主类之前已经实现了它,但现在在主类(如 LinkedList)中嵌套一个 Node 类会使它复杂化。

    public void insert(int val){
        if (root == null){
            root = new Node(val);
        } else {
            Node current = root;

在此位 current 始终获取 root 的值,这导致插入的项目不超过 3 个。我知道这个问题,但无法解决。对代码的任何重新设计将不胜感激。

【问题讨论】:

  • 您可以将子树的根作为参数传递给您的 insert 函数。
  • @Janez Kuhar @Abra 将root 的引用传递给insert() 确实有效。谢谢。

标签: java data-structures binary-tree


【解决方案1】:

在您的代码中,您没有在insert() 方法中传递Node 的引用来追踪您在当前树中的哪个节点位置。

目前您只能插入 3 个项目,因为对于 3 个项目,没有使用 insert(val) 的递归,但是在 3 个项目之后,您正在使用 insert 调用的递归,因为您没有传递当前节点位置这个问题来了。

这是在二叉树中插入的工作示例:

class bTree {
    Node root;
    public class Node {
        Node left;
        Node right;
        int val;

        Node () {}
        Node (int val){
            this.val=val;
        }
    }

    public void insert(Node currnode, int val){
        if(currnode == null) {
            root = new Node(val);
            return;
        } 
        if(val <= currnode.val) {
            if(currnode.left == null) {
                currnode.left = new Node(val);
            } else {
                insert(currnode.left, val);
            }
            
        } else {
            if(currnode.right == null) {
                currnode.right = new Node(val);
            } else {
                insert(currnode.right, val);
            }
        }
    }

    public void displayTree(Node root){
   
        if (root.left != null){
            displayTree(root.left);
        }
        System.out.print(root.val + " - "); 
        if (root.right != null){
            displayTree(root.right);
        }
    }

    public static void main(String[] args) {
        bTree bt = new bTree();
        bt.insert(bt.root,10);
        bt.insert(bt.root,30);
        bt.insert(bt.root,4);
        bt.insert(bt.root,5);
        bt.displayTree(bt.root);
    }
}

【讨论】:

  • root 传递给insert() 成功了!这也有助于我的其他与 OOP 相关的查询。非常感谢。
  • 很高兴知道它对你有帮助?
猜你喜欢
  • 1970-01-01
  • 2015-10-18
  • 2018-11-06
  • 2017-07-03
  • 2016-03-02
  • 2012-11-07
  • 2019-11-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多