英文:
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
以下是翻译好的部分:
- Your
insert(input)
method ignores the return value of itsinsert(node, key)
call. This means that ifroot
isnull
, nothing will be inserted into the tree. - 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
.
- Your
insert(input)
method ignores the return value of itsinsert(node, key)
call. This means that ifroot
isnull
, nothing will be inserted into the three - 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论