无法在 BinaryTree 中插入值

Couldn't insert values in BinaryTree

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 main class 之前已经实现了它,但现在在 main class(如 LinkedList)中嵌套 Node class 使它变得复杂。

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

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

在您的代码中,您没有在 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);
    }
}