在Java中搜索树中的节点
我有一个使用以下构造函数制作的二叉树:
public Person(String name, int age, char gender, Person c1, Person c2)
其中 c1 是左子树,c2 是右子树。
我想编写一个在最大代内搜索特定名称的方法。就像 a.depthFirstSearch(Eva, 1);
一样,其中 Eva 是要搜索的名称,1 是我可以查看的最大代数(或级别)。
这是我所拥有的: 编辑:
public Person depthFirstSearch(String name, int maxGeneration)
{
{
Person temp;
if (maxGeneration>1){
if (this.name.equals(name)){
temp=this;
return temp;
}
else{
if (child1!=null)
temp=child1.depthFirstSearch(name, maxGeneration-1);
if (child2!=null)
temp=child1.depthFirstSearch(name, maxGeneration-1);
}
}
return null;
}
}
这里有两个问题。我认为每次函数调用自身时深度都会重置为 0,所以我知道我可以在其他地方跟踪深度或找到替代方案。我认为另一个问题是 child2 从未真正到达,因为我在 child1 处返回。我不太确定这是如何工作的,所以如果有人能解释一下,那就太好了。对一些修复有什么建议吗?
另外,我被告知我必须首先搜索深度,这意味着首先研究更深的几代。我不太确定这意味着什么以及它与我在实现中使用的逻辑有何不同。
I have a binary tree made with the following constructor:
public Person(String name, int age, char gender, Person c1, Person c2)
where c1 is the left child and c2 is the right child.
I want to write a method that searches for a particular name within a maximum generation. So like a.depthFirstSearch(Eva, 1);
where Eva is the name to search for and 1 is the maximum number of generations (or levels) I can look into.
Here's what I have:
EDIT:
public Person depthFirstSearch(String name, int maxGeneration)
{
{
Person temp;
if (maxGeneration>1){
if (this.name.equals(name)){
temp=this;
return temp;
}
else{
if (child1!=null)
temp=child1.depthFirstSearch(name, maxGeneration-1);
if (child2!=null)
temp=child1.depthFirstSearch(name, maxGeneration-1);
}
}
return null;
}
}
There's two problems here. I think depth gets reset to 0 every time the function calls itself, so I know I can either keep track of depth somewhere else or find an alternative. The other problem, I think, is that child2 is never really reached, since I return at child1. I'm not really sure how this works so if someone could explain that, that'd be great. Any suggestions for some fixes?
Also, I'm told that I have to search depth first, meaning looking into the deeper generations first. I'm not really sure what that means and how different it is from the logic I'm using in my implementation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您在每次递归调用中递减
maxGeneration
,因此您根本不需要深度
变量:当maxGeneration == 0
时,您根本不需要不再搜索并返回null
。至于您的其他问题,不要直接返回
child1.depthFirstSearch(...)
的值,而是将该值存储在临时变量中。如果不为null
,则说明已找到该节点,因此立即返回该节点,否则继续在child2
下搜索。更新:
应该是
if (maxGeneration >= 1) ...
(大于或等于),否则最后一次调用maxGeneration == 1
将始终返回 null。或者,您可以只检查 0 并返回 null:此外,您仍然没有使用返回值来检查该节点是否确实在左子树中找到。现在,即使您找到
child1
下的节点,您仍然会在child2
下查找,最终将返回 null,这是错误的。仅当左侧搜索返回 null 时,才需要在child2
下搜索:Since you decrement
maxGeneration
in each recursive call, you don't need thedepth
variable at all: whenmaxGeneration == 0
you simply don't search any more and returnnull
.As for your other problem, instead of directly returning the value of
child1.depthFirstSearch(...)
, store the value in a temporary variable. If it is notnull
, you have found the node, so return it immediately, otherwise continue searching underchild2
.Update:
It should be
if (maxGeneration >= 1) ...
(greater than or equal to), otherwise the last call withmaxGeneration == 1
will always return null. Alternatively, you can just check for 0 and return null:Also, you still aren't using the return value to check if the node was actually found in the left subtree or not. Right now, even if you find the node under
child1
, you still look underchild2
and you will end up returning null, which is wrong. You need to search underchild2
only if the left search returned null: