帮助树递归

发布于 2024-10-01 11:22:57 字数 885 浏览 0 评论 0原文

我有一个 Person 类,我想创建一棵树。这是 Person 类的构造函数。

public Person(String name, int age, char gender, Person c1, Person c2)

c1 是左边的孩子,c2 是右边的孩子。假设我像这样创建了三个人:

Person c = new Person("Carl", 50, 'M', null, f);

Person b = new Person("Barbara", 52, 'F', d, e);

Person a = new Person("Adam", 75, 'M', b, c);

所以在这里你说 Adam 是根节点,Adam 的左孩子是 b,即 Barbara,他的右孩子是 Carl,等等。

所以我想做的是编写一个计数方法,计算包括 this 在内的子级的数量。所以 a.count() 将返回 6(如果 Person f 没有任何孩子)。

这是我的代码:

public int count() // total person count including this object
{
    if(child1==null)
        return 0; //I tried return 1 for this too didnt work
    if (child2==null)
        return 0; //also tried 1 for this
    return 1+this.child1.count() +1+this.child2.count();
}

我在纸上运行了几次,它应该得出正确的结果,但当我实际运行它时,由于某种原因,它有一些偏差。

I have a Person class, and I want to create a tree. Here is the contsructor for the Person class.

public Person(String name, int age, char gender, Person c1, Person c2)

c1 is the child on the left, and c2 is the child on the right. And so say I create three Persons like so:

Person c = new Person("Carl", 50, 'M', null, f);

Person b = new Person("Barbara", 52, 'F', d, e);

Person a = new Person("Adam", 75, 'M', b, c);

So here you say Adam is the root node, and Adam's left child is b, which is Barbara and his right c which is Carl, and so on.

So what I want to do is write a count method, that counts the number of children including this. So a.count() would return 6 (if Person f doesnt have any children).

And so here's the code I have:

public int count() // total person count including this object
{
    if(child1==null)
        return 0; //I tried return 1 for this too didnt work
    if (child2==null)
        return 0; //also tried 1 for this
    return 1+this.child1.count() +1+this.child2.count();
}

I ran this on paper several times, and it should come up with the correct result, but it's off by a few for some reason when I actually run it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

淡水深流 2024-10-08 11:22:57

如果子级之一为 null,您的代码将返回 0。这是不正确的,因为您没有考虑另一个孩子或这个。计数应始终为 >= 1,因为树中始终至少有一个节点。

此外,如果您看到一个子项为 null,您也无法立即返回。您还需要计算另一个孩子(如果存在)。

以下是我的实现方式:

public int count() // total person count including this object
{
    int count = 1; // we count this node as 1 
    if (child1 != null) // if we have a left child, count its size
        count += child1.count();
    if (child2 != null) // if we have a right child, count its size
        count += child2.count()
    return count;
}

您需要考虑两个子项,即使其中一个为 null

Your code returns 0 if one of the children are null. This is incorrect because you don't account for the other child, or this. The count should always be >= 1 because you always have at least one node in the tree.

Also, you can't return right away if you see that one child is null. You need to count the other child too (if it exists).

Here is how I would implement it:

public int count() // total person count including this object
{
    int count = 1; // we count this node as 1 
    if (child1 != null) // if we have a left child, count its size
        count += child1.count();
    if (child2 != null) // if we have a right child, count its size
        count += child2.count()
    return count;
}

You need to account for both children, even if one of them is null.

oО清风挽发oО 2024-10-08 11:22:57

结果是错误的,因为当子节点为 null 时,您返回 0,忘记计算节点本身或其他子节点。如果它是叶节点 (child1 == null && child2 == null )你无论如何应该返回1。

类似的东西:

return 1 + (child1 == null ? 0 : child1.count()) + (child2 == null ? 0 : child2.count())

按照你的原始代码,它会是这样的:

if (child1 == null && child2 == null)
  return 1;
else if (child1 == null)
  return 1 + child2.count();
else if (child2 == null)
  return 1 + child1.count();
else
  return 1 + child1.count() + child2.count();

但在这种情况下,我会说坚持使用jjnguy答案,它部分计算结果..

The result is wrong because you return 0 when a child is null forgetting to count the node itself or the other child.. if it's a leaf node (child1 == null && child2 == null) you should anyway return 1.

Something like:

return 1 + (child1 == null ? 0 : child1.count()) + (child2 == null ? 0 : child2.count())

following your original code it would be something like:

if (child1 == null && child2 == null)
  return 1;
else if (child1 == null)
  return 1 + child2.count();
else if (child2 == null)
  return 1 + child1.count();
else
  return 1 + child1.count() + child2.count();

but in that case I would say to stick with jjnguy answer which calculates the result partially..

扮仙女 2024-10-08 11:22:57
private int count() {
    return 1 + ((this.child1 != null) ? (this.child1.count()) : (0)) + ((this.child2 != null) ? (this.child2.count()) : (0));
}
private int count() {
    return 1 + ((this.child1 != null) ? (this.child1.count()) : (0)) + ((this.child2 != null) ? (this.child2.count()) : (0));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文