Java返回语句逻辑与二叉搜索树

huangapple go评论67阅读模式
英文:

Java return statement logic with a Binary search tree

问题

以下是您要翻译的内容:

我正在尝试理解主要插入方法上的返回语句如何在第一个条件为真时将 null 值替换为已创建的节点。

public void insert(int input){
    insert(root, input);
}

public Node insert(Node node, int key) {
    if (node == null) {
        return new Node(key);
    } else if (node.key > key) {
        node.left = insert(node.left, key);
    } else if (node.key < key) {
        node.right = insert(node.right, key);
    } else {
        return node;
    }
}

我的理解是,返回语句的工作原理是停止函数并返回一个值。因此,在这种情况下,将创建一个具有 key 作为其数据的新节点,然后返回该节点。返回语句还有其他作用吗?
英文:

I'm trying to understand how the return statement on the main insert method substitutes the null value for the created node when the first condition is true.

 public void insert(int input){
        insert(root, input);
    }
}

public Node insert(Node node, int key) {
    if (node == null) {
        return new Node(key);
    } else if (node.key > key) {
        node.left = insert(node.left, key);
    } else if (node.key < key) {
        node.right = insert(node.right, key);
    } else {
        return node;
    }
}

My understanding of how the return statement works is that it stops a function and returns a value. Hence, in this situation, a new node is created with key as it's data, then that node is returned. Is there some other thing the return statement does?

答案1

得分: 0

以下是翻译好的部分:

  1. Your insert(input) method ignores the return value of its insert(node, key) call. This means that if root is null, nothing will be inserted into the tree.
  2. Your insert(node, key) method does not return values on all code paths. This means the code won’t even compile, and also won’t be correct.
    The fixed code might look as follows:
    root = insert(root, input); // 1
}

public Node insert(Node node, int key) {
    if (node == null) {
        return new Node(key);
    }

    if (node.key > key) {
        node.left = insert(node.left, key);
    } else if (node.key < key) {
        node.right = insert(node.right, key);
    }

    return node; // 2
}

This simply makes sure that the current node is always returned after the insertion took place (recursively), or if no insertion took place because the value already existed.
Now the return logic should make sense.

英文:

There are two problems in your code, and together these problems might explain why you’re having trouble understanding the role of return.

  1. Your insert(input) method ignores the return value of its insert(node, key) call. This means that if root is null, nothing will be inserted into the three
  2. Your insert(node, key) method does not return values on all code paths. This means the code won’t even compile, and also won’t be correct.

The fixed code might look as follows:

public void insert(int input) {
    root = insert(root, input); // 1
}

public Node insert(Node node, int key) {
    if (node == null) {
        return new Node(key);
    }

    if (node.key > key) {
        node.left = insert(node.left, key);
    } else if (node.key < key) {
        node.right = insert(node.right, key);
    }

    return node; // 2
}

This simply makes sure that the current node is always returned after the insertion took place (recursively), or if no insertion took place because the value already existed.

Now the return logic should make sense.

huangapple
  • 本文由 发表于 2020年8月6日 19:47:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63282931.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定