Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将节点插入BST_Java_Algorithm_Tree_Binary Search Tree - Fatal编程技术网

Java 将节点插入BST

Java 将节点插入BST,java,algorithm,tree,binary-search-tree,Java,Algorithm,Tree,Binary Search Tree,我用于将节点插入BST的代码不起作用。当我试图显示时,它只显示树的最后两个节点,即它以某种方式覆盖树中的节点。我做错了什么?提前谢谢 public Node Insert(int value) { Node newNode = new Node(); newNode.data = value; newNode.left = null; newNode.right = null; if(root == nul

我用于将节点插入BST的代码不起作用。当我试图显示时,它只显示树的最后两个节点,即它以某种方式覆盖树中的节点。我做错了什么?提前谢谢

public Node Insert(int value)
    {
        Node newNode = new Node();
        newNode.data = value;
        newNode.left = null;
        newNode.right = null;

        if(root == null){
            root = newNode;
            return root;
        }
        else{
            while(root != null){
                if(root.data < value){
                    if(root.right != null){
                        root = root.right;
                    }
                    else{
                        root.right = newNode;
                        break;
                    }
                }
                else{
                    if(root.left != null){
                        root = root.left;
                    }
                    else{
                        root.left = newNode;
                        break;
                    }

                }
            }
            return root;
        }

    }
    public void inOrder(){
        inOrder(this.root);
    }
    private void inOrder(Node root){
        if(root != null){
            inOrder(root.left);
            System.out.println(root.data);
            inOrder(root.right);
        }
    }
公共节点插入(int值)
{
Node newNode=新节点();
newNode.data=值;
newNode.left=null;
newNode.right=null;
if(root==null){
根=新节点;
返回根;
}
否则{
while(root!=null){
if(root.data<值){
if(root.right!=null){
root=root.right;
}
否则{
root.right=newNode;
打破
}
}
否则{
if(root.left!=null){
root=root.left;
}
否则{
root.left=newNode;
打破
}
}
}
返回根;
}
}
公共无效序(){
顺序(this.root);
}
私有void索引(节点根){
if(root!=null){
顺序(根。左);
System.out.println(root.data);
顺序(root.right);
}
}

根指向树的顶部节点。在中,当您更改其值时。必须使用另一个变量(如“n”)

“n”的初始值为“根”。 而在内部,您只使用“n”

if(root == null){
        root = newNode;
        return root;
    }
    else{
        Node n = root;
        while(n != null){
        ...
        }
        return root;
        ...

您必须返回“root”,而不是“n”,因为root不会更改。

请参阅代码中的注释。希望能有帮助

public Node Insert(int value)
{
    Node newNode = new Node();
    newNode.data = value;
    newNode.left = null;
    newNode.right = null;

    if(root == null){
        root = newNode;
        return root;
    }
    else{
        while(root != null){
            if(root.data < value){
                if(root.right != null){
                    root = root.right;
                    // root should point to the head of the tree.
                    /* Here you are changing the variable root to point to some other memory location and the reference to the head of the tree is lost. You should store the reference to the head of the tree in a temporary variable to return later  */
                }
                else{
                    root.right = newNode;
                    break;
                }
            }
            else{
                if(root.left != null){
                    root = root.left;
                   //root should point to the head of the tree.
                    /* Here you are changing the variable root to point to some other memory location and the reference to the head of the tree is lost. You should store the reference to the head of the tree in a temporary variable to return later  */
                }
                else{
                    root.left = newNode;
                    break;
                }

            }
        }
        // return temporary variable which points to the head of the tree here
        return root;
    }

}
public void inOrder(){
    inOrder(this.root);
}
private void inOrder(Node root){
    if(root != null){
        inOrder(root.left);
        System.out.println(root.data);
        inOrder(root.right);
    }
}
公共节点插入(int值)
{
Node newNode=新节点();
newNode.data=值;
newNode.left=null;
newNode.right=null;
if(root==null){
根=新节点;
返回根;
}
否则{
while(root!=null){
if(root.data<值){
if(root.right!=null){
root=root.right;
//根应该指向树的头部。
/*在这里,您将变量root更改为指向其他内存位置,对树头的引用将丢失。您应该将对树头的引用存储在临时变量中,以便稍后返回*/
}
否则{
root.right=newNode;
打破
}
}
否则{
if(root.left!=null){
root=root.left;
//根应该指向树的头部。
/*在这里,您将变量root更改为指向其他内存位置,对树头的引用将丢失。您应该将对树头的引用存储在临时变量中,以便稍后返回*/
}
否则{
root.left=newNode;
打破
}
}
}
//返回指向树头的临时变量
返回根;
}
}
公共无效序(){
顺序(this.root);
}
私有void索引(节点根){
if(root!=null){
顺序(根。左);
System.out.println(root.data);
顺序(root.right);
}
}

在调试器下逐步运行代码。