Java binarysearch tree.root返回null

Java binarysearch tree.root返回null,java,binary-search-tree,Java,Binary Search Tree,我在网站上找到了二进制搜索树插入java代码 部分代码如下所示 if (root == null) { root = new Node(key); return root; } 我认为我们不需要任何返回语句,因为根本身是引用类型(节点),所以更新根就足够了 所以我像这样修改了代码 class BinarySearchTree { class Node { int key; Node left, right;

我在网站上找到了二进制搜索树插入java代码

部分代码如下所示

    if (root == null) { 
        root = new Node(key); 
        return root; 
    } 
我认为我们不需要任何返回语句,因为根本身是引用类型(节点),所以更新根就足够了

所以我像这样修改了代码

class BinarySearchTree {

class Node {
    int key;
    Node left, right;

    public Node(int item) {
        key = item;
        left = right = null;
    }
}

Node root;

BinarySearchTree() {
    root = null;
}

void insert(int key) {
    insertRec(root, key);
}

/* A recursive function to insert a new key in BST */
void insertRec(Node root, int key) {

    if (root == null) {
        root = new Node(key);
    }

    if (key < root.key)
        insertRec(root.left, key);
    else if (key > root.key)
        insertRec(root.right, key);
}

// Driver Program to test above functions 
public static void main(String[] args) {
    BinarySearchTree tree = new BinarySearchTree(); 

    tree.insert(50);
    tree.insert(20);

    System.out.println(tree.root);
    }
}
类二进制搜索树{
类节点{
int键;
左、右淋巴结;
公共节点(int项){
键=项目;
左=右=空;
}
}
节根;
二进制搜索树(){
root=null;
}
无效插入(整数键){
insertRec(根,键);
}
/*在BST中插入新键的递归函数*/
void insertRec(节点根,int键){
if(root==null){
根=新节点(键);
}
if(keyroot.key)
insertRec(root.right,key);
}
//用于测试上述功能的驱动程序
公共静态void main(字符串[]args){
BinarySearchTree=新的BinarySearchTree();
插入(50);
插入(20);
System.out.println(tree.root);
}
}
但是tree.root返回null

为什么会发生这种情况?

root=新节点(键)
更新局部变量,它不会更新树的根(
this.root
),也不应该更新。因此,此分配不会更新树

Node insertRec(Node root, int key) {

    if (root == null) {
        root = new Node(key);
        return root;
    }

    if (key < root.key)
        root.left = insertRec(root.left, key);
    else
       root.right  = insertRec(root.right, key);

   return root;
}
当您返回新创建的
节点
(与您更改的原始代码相同)时,可以将其指定为树的根(与原始代码使用
root=insertRec(root,key);
)或现有树节点的左或右子级(与原始代码使用
root.left=insertRec一样)(root.left,key);
root.right=insertRec(root.right,key);
)。这就是树的更新方式

编辑:Java是一种按值传递语言,而不是按引用传递。当您将变量传递给方法时,该方法无法更改传递变量的值。如果您将值为
null
的变量传递给方法,并且该方法为其赋值,则一旦该方法返回,该变量仍将包含
null

root=new Node(key);
更新局部变量,它不会更新树的根(
this.root
),也不应该更新。因此,此赋值不会更新树

Node insertRec(Node root, int key) {

    if (root == null) {
        root = new Node(key);
        return root;
    }

    if (key < root.key)
        root.left = insertRec(root.left, key);
    else
       root.right  = insertRec(root.right, key);

   return root;
}
当您返回新创建的
节点
(与您更改的原始代码相同)时,可以将其指定为树的根(与原始代码使用
root=insertRec(root,key);
)或现有树节点的左或右子级(与原始代码使用
root.left=insertRec一样)(root.left,key);
root.right=insertRec(root.right,key);
)。这就是树的更新方式


编辑:Java是一种按值传递语言,而不是按引用传递。当您将变量传递给方法时,该方法无法更改传递变量的值。如果您将值为
null
的变量传递给方法,并且该方法为其赋值,则一旦该方法返回,该变量仍将包含
null

在构造函数中,您不初始化根节点。您的密钥也不会在节点中转换,您也不会将插入的节点设置为左或右节点。此外,您也不会对与您所遍历的当前节点相等的节点执行任何操作。

在构造函数中,您不会初始化根节点。您的密钥也不会在节点中转换并且您不会将插入的节点设置为左或右节点。您也不会对与您所遍历的当前节点相等的节点执行任何操作。

就像其他人已经指出的那样,每次插入元素时都需要更新
根节点

只需
返回
根的值并更新树的全局

Node insertRec(Node root, int key) {

    if (root == null) {
        root = new Node(key);
        return root;
    }

    if (key < root.key)
        root.left = insertRec(root.left, key);
    else
       root.right  = insertRec(root.right, key);

   return root;
}
完整代码:

class BinarySearchTree {

class Node {
    int key;
    Node left, right;

    public Node(int item) {
        key = item;
        left = right = null;
    }
}

Node root;

BinarySearchTree() {
    root = null;
}

 Node insert(int key) {
   return insertRec(root, key);
}

/* A recursive function to insert a new key in BST */
Node insertRec(Node root, int key) {

    if (root == null) {
        root = new Node(key);
        return root;
    }

    if (key < root.key)
        root.left = insertRec(root.left, key);
    else
       root.right  = insertRec(root.right, key);

   return root;
}

// Driver Program to test above functions 
public static void main(String[] args) {
    BinarySearchTree tree = new BinarySearchTree(); 

    tree.root = tree.insert(50);
    tree.root = tree.insert(20);

    System.out.println(tree.root.key);
    }
}
类二进制搜索树{
类节点{
int键;
左、右淋巴结;
公共节点(int项){
键=项目;
左=右=空;
}
}
节根;
二进制搜索树(){
root=null;
}
节点插入(int键){
返回insertRec(根,键);
}
/*在BST中插入新键的递归函数*/
节点insertRec(节点根,int键){
if(root==null){
根=新节点(键);
返回根;
}
if(key
就像其他人已经指出的那样,每次插入元素时都需要更新
根目录

只需
返回
根的值并更新树的全局

Node insertRec(Node root, int key) {

    if (root == null) {
        root = new Node(key);
        return root;
    }

    if (key < root.key)
        root.left = insertRec(root.left, key);
    else
       root.right  = insertRec(root.right, key);

   return root;
}
完整代码:

class BinarySearchTree {

class Node {
    int key;
    Node left, right;

    public Node(int item) {
        key = item;
        left = right = null;
    }
}

Node root;

BinarySearchTree() {
    root = null;
}

 Node insert(int key) {
   return insertRec(root, key);
}

/* A recursive function to insert a new key in BST */
Node insertRec(Node root, int key) {

    if (root == null) {
        root = new Node(key);
        return root;
    }

    if (key < root.key)
        root.left = insertRec(root.left, key);
    else
       root.right  = insertRec(root.right, key);

   return root;
}

// Driver Program to test above functions 
public static void main(String[] args) {
    BinarySearchTree tree = new BinarySearchTree(); 

    tree.root = tree.insert(50);
    tree.root = tree.insert(20);

    System.out.println(tree.root.key);
    }
}
类二进制搜索树{
类节点{
int键;
左、右淋巴结;
公共节点(int项){
键=项目;
左=右=空;
}
}
节根;
二进制搜索树(){
root=null;
}
节点插入(int键){
返回insertRec(根,键);
}
/*在BST中插入新键的递归函数*/
节点insertRec(节点根,int键){
if(root==null){
根=新节点(键);
返回根;
}
if(key
它被称为“阴影效果”

这意味着父类中的根注释被方法中的根节点隐藏