通过树递归计算性别

发布于 2024-09-30 14:52:32 字数 749 浏览 0 评论 0原文

我有一个具有以下构造函数的 Person 类:

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

其中 c1 是左孩子,c2 是右孩子。我想编写一个方法来计算与给定性别(M 或 F)匹配的人数:

public int countGender(char gen)
{
    int count=0;
    if (this.gender==gen){
        count++;
    }
    if (child1!=null){
        if (child1.gender==gen){
            count+=1+child1.countGender(gen);
        }
        else count+=child1.countGender(gen);
    }
    if (child2!=null){
        if (child2.gender==gen){
            count+=1+child2.countGender(gen);
        }
        else count+=child2.countGender(gen);
    }
    return count;
}

我已经尝试了几乎所有方法。我很难想象每次函数调用自身时会发生什么。会重置吗?或者因为我使用的是+=,它会在重置之前保存自己吗?我的方法还有什么问题吗?请帮助我理解。

I have a Person class 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 counts the number of Persons that match the given gender, either M or F:

public int countGender(char gen)
{
    int count=0;
    if (this.gender==gen){
        count++;
    }
    if (child1!=null){
        if (child1.gender==gen){
            count+=1+child1.countGender(gen);
        }
        else count+=child1.countGender(gen);
    }
    if (child2!=null){
        if (child2.gender==gen){
            count+=1+child2.countGender(gen);
        }
        else count+=child2.countGender(gen);
    }
    return count;
}

I've tried just about everything. I'm having a hard time imagining what happens to count every time the function calls itself. Does it reset? Or since I'm using +=, does it save itself kind of before it resets? What else is wrong with my method? Please help me understand.

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

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

发布评论

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

评论(2

陪你搞怪i 2024-10-07 14:52:33

每次调用 countGender 都有自己的 count 变量,初始化为零。

您不需要检查一个人的两个孩子的性别,因为对每个孩子的递归调用都可以做到这一点。例如:

public int countGender(char gen) {

    int count = 0;

    if (this.gender == gen) {
        count++;
    }

    if (child1 != null) {
        count += child1.countGender(gen);
    }

    if (child2 != null) {
        count += child2.countGender(gen);
    }

    return count;
}

您可以将 p.countGender(...) 视为在其 p 所在的树中为您提供指定性别的人数。根。

这是以下各项之和:

  • 1,如果 p 性别正确,则
  • p 子树中性别正确的人数(如果有的话)
  • p子树中性别正确的人数(如果有的话)。

上面给出的代码对人物树执行深度优先遍历。它实际上是一个前序遍历(处理根节点,然后处理它的左子树和右子树)。也可以进行中序遍历(左子树,然后节点本身,然后右子树),或后序遍历(左子树,然后右子树,然后节点本身)。

对于计算节点,正如您在此处所做的那样,顺序实际上并不重要:结果将是相同的。但对于某些操作,例如显示树中的所有人员,它确实很重要。

Each invocation of countGender has its own count variable, initialised to zero.

You need don't need to examine the gender of a Person's two children, as the recursive calls for each of those children can do that. For example:

public int countGender(char gen) {

    int count = 0;

    if (this.gender == gen) {
        count++;
    }

    if (child1 != null) {
        count += child1.countGender(gen);
    }

    if (child2 != null) {
        count += child2.countGender(gen);
    }

    return count;
}

You can think of p.countGender(...) as giving you the number of people, with the specified gender, in the tree that has p at its root.

This is the sum of:

  • 1, if p has the correct gender
  • the number of people with the correct gender in the left subtree of p (if there is one)
  • the number of people with the correct gender in the right subtree of p (if there is one).

The code given above performs a depth-first traversal of the tree of people. It's actually a preorder traversal (deal with the root node, followed by its left and right subtrees). It's also possible to do an inorder traversal (left subtree, then the node itself, then the right subtree), or a postorder traversal (left subtree, then right subtree, then the node itself).

For counting nodes, as you do here, the order doesn't actually matter: the result will be the same. For certain operations, though, such as displaying all the people in the tree, it does matter.

本宫微胖 2024-10-07 14:52:33

我认为你加 1 的次数太多了。每次子进程运行 countGender 时,如果性别正确(第 5 行),计数就会加 1。但是,您在第 9 行和第 15 行再次检查孩子的性别,尽管每个孩子都检查自己的性别。因此,你对所有事情都进行了重复计算。

至于递归,countGender 的每次调用都是不同的,并且与任何其他调用分开。每个实例的count都是内存中的一个单独的变量,并且每次都初始化为0。

I think you're adding 1 too many times. Every time an child runs countGender, it adds 1 to the count if it is the correct gender (line 5). But, you're checking the child's gender again at lines 9 and 15, even though each child checks its own gender. Thus, you're double-counting everything.

As for recursion, every call of countGender is distinct and separate from any other calls. Each instance's count is a separate variable in memory, and it's initialized to 0 every time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文