帮助树递归
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果子级之一为
null
,您的代码将返回0
。这是不正确的,因为您没有考虑另一个孩子或这个
。计数应始终为>= 1
,因为树中始终至少有一个节点。此外,如果您看到一个子项为
null
,您也无法立即返回。您还需要计算另一个孩子(如果存在)。以下是我的实现方式:
您需要考虑两个子项,即使其中一个为
null
。Your code returns
0
if one of the children arenull
. This is incorrect because you don't account for the other child, orthis
. 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:
You need to account for both children, even if one of them is
null
.结果是错误的,因为当子节点为 null 时,您返回 0,忘记计算节点本身或其他子节点。如果它是叶节点 (
child1 == null && child2 == null
)你无论如何应该返回1。类似的东西:
按照你的原始代码,它会是这样的:
但在这种情况下,我会说坚持使用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:
following your original code it would be something like:
but in that case I would say to stick with jjnguy answer which calculates the result partially..