在Java中搜索树中的节点

发布于 2024-10-01 08:17:38 字数 922 浏览 4 评论 0原文

我有一个使用以下构造函数制作的二叉树:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疧_╮線 2024-10-08 08:17:38

由于您在每次递归调用中递减 maxGeneration,因此您根本不需要 深度 变量:当 maxGeneration == 0 时,您根本不需要不再搜索并返回null

至于您的其他问题,不要直接返回 child1.depthFirstSearch(...) 的值,而是将该值存储在临时变量中。如果不为null,则说明已找到该节点,因此立即返回该节点,否则继续在child2下搜索。


更新:

应该是if (maxGeneration >= 1) ...(大于或等于),否则最后一次调用maxGeneration == 1 将始终返回 null。或者,您可以只检查 0 并返回 null:

if (maxGeneration == 0)
  return null;

// rest of your code

此外,您仍然没有使用返回值来检查该节点是否确实在左子树中找到。现在,即使您找到 child1 下的节点,您仍然会在 child2 下查找,最终将返回 null,这是错误的。仅当左侧搜索返回 null 时,才需要在 child2 下搜索:

Person temp;
if (child1 != null) {
  temp = child1.depthFirstSearch(name, maxGeneration-1);
  if (temp != null)
    return temp; // found the node, just return
}
// otherwise the following code will execute
if (child2 != null) {
  temp = child2.depthFirstSearch(name, maxGeneration-1);
  if (temp != null)
    return temp; // found the node, just return
}
// didn't find node under either child
return null;

Since you decrement maxGeneration in each recursive call, you don't need the depth variable at all: when maxGeneration == 0 you simply don't search any more and return null.

As for your other problem, instead of directly returning the value of child1.depthFirstSearch(...), store the value in a temporary variable. If it is not null, you have found the node, so return it immediately, otherwise continue searching under child2.


Update:

It should be if (maxGeneration >= 1) ... (greater than or equal to), otherwise the last call with maxGeneration == 1 will always return null. Alternatively, you can just check for 0 and return null:

if (maxGeneration == 0)
  return null;

// rest of your code

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 under child2 and you will end up returning null, which is wrong. You need to search under child2 only if the left search returned null:

Person temp;
if (child1 != null) {
  temp = child1.depthFirstSearch(name, maxGeneration-1);
  if (temp != null)
    return temp; // found the node, just return
}
// otherwise the following code will execute
if (child2 != null) {
  temp = child2.depthFirstSearch(name, maxGeneration-1);
  if (temp != null)
    return temp; // found the node, just return
}
// didn't find node under either child
return null;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文