【问题标题】:Tree structure category sum that cascades to root node级联到根节点的树结构类别和
【发布时间】:2009-12-04 07:39:45
【问题描述】:

我要站出来说我不是世界上最伟大的数学家 :D 所以这个问题对你们大多数人来说可能很简单。不幸的是,这让我感到困惑,并且已经尝试了几个可行的解决方案。

与任何一棵树一样,一棵树可以有许多分支,许多分支可以有更多分支,依此类推,直到它们以叶节点结束。我有每片叶子的信息表明它的价值。

我需要明确说明如何解决将每个叶节点值汇总为其分支(父节点)的总和并对其余部分执行相同操作但不要忘记分支是否由其他分支共享的问题就是每个与自身直接相关的下层分支和叶子的汇总。

为了更好地解释:

Root
|----Branch
|         |-Leaf 10
|----Branch
|         |----Branch
|         |-Leaf 20 |-Leaf 30
|----Branch         |-Leaf 40
|         |----Branch
|                   |----Branch
|                             |----Leaf 50
|-Leaf 60

目标:

Root 210
|----Branch 10
|         |-Leaf 10
|----Branch 90
|         |----Branch 70
|         |-Leaf 20 |-Leaf 30
|----Branch 50      |-Leaf 40
|         |----Branch 50
|                   |----Branch 50
|                             |----Leaf 50
|-Leaf 60

我能够识别最低级别的成员(叶节点)、根节点和分支本身。我无法确定该分支是否有其他分支链接到自身较低或直接链接到叶节点。这种关系非常自下而上。 IE:分支没有提及它的孩子是谁,但孩子知道父母是谁。

如果有不清楚的地方,请询问,我会尝试更好地解释问题。

任何帮助将不胜感激。

【问题讨论】:

  • 这对我来说是一个独立于语言的问题,更多的是对如何正确解决问题的理解。不过,我目前确实在 C# 中工作;-)

标签: recursion tree hierarchical-trees


【解决方案1】:

好的,左派来一刀。

我会用一些伪代码来做这个

foreach leaf in knownLeafs
    parent = leaf.parent //get the leaf parent
    parent.total = parent.total + leaf.value //add leaf value to parent total
    while parent.parent != null //loop until no more parents, this would be the root
    {
        current = parent
        parent = parent.parent //move up the structure
        parent.total = parent.total + current.total
    }
next leaf

你需要创建一个函数,给定一个节点,返回父节点

node GetParentNodeFrom(node)

新的伪代码看起来像这样

foreach leaf in knownLeafs
parent = GetParentNodeFrom(leaf) //get the leaf parent

parent.total = parent.total + leaf.value //add leaf value to parent total
while GetParentNodeFrom(parent) != null //loop until no more parents, this would be the root
{
    current = parent
    parent = GetParentNodeFrom(current) //move up the structure
    parent.total = parent.total + current.total
}
next leaf

对不起,我的错误,您应该只将叶子值向上移动,而不是总数。 查看使用的新叶子值。

foreach leaf in knownLeafs
parent = GetParentNodeFrom(leaf) //get the leaf parent
leafValue = leaf.value
parent.total = parent.total + leafValue //add leaf value to parent total
while GetParentNodeFrom(parent) != null //loop until no more parents, this would be the root
{
    current = parent
    parent = GetParentNodeFrom(current) //move up the structure
    parent.total = parent.total + leafValue
}
next leaf

【讨论】:

  • 感谢 Astander!我也要研究一下你的想法。似乎我不是唯一一个在这类问题上苦苦挣扎的人。
  • while循环好像有点奇怪,你在枚举什么?我明白你在做什么,遍历父母的父母的父母。我不知道深度,也不知道如何暗示等效。 "while parent.parent != null //循环直到没有更多的父母,这将是根"
  • 刚刚看到你的更新,这更有意义。我会回来报告的。
  • 好的,我已经设法在 C# 中实现了这一点,这个想法是有道理的。我现在正在跟踪父枚举的逻辑,它是重复的 - 几乎每次迭代都会复合值。请记住,每个父节点都可以直接链接到叶节点或另一个分支(向下),如果我们向上遍历,我们可以将我们的总数与各种流结合起来。
  • :-) 我完全错过了!, parent.total = parent.total + current.total 将是一个复合迭代。 parent.total = parent.total + leafValue 是正确的,并带走了兄弟和分支复合。我还要感谢您对此问题的快速响应和帮助。在我 8/9 年的开发生涯中,这是我第一次遇到如此奇怪的暗示(父节点没有重新收集叶节点)和反向遍历,叶节点向上是唯一可能的计算方式对现有代码进行大量更改。你从我这里得到+1!谢谢。
【解决方案2】:

您想确定树中所有节点的总和吗?

树行走适合优雅的递归解决方案:

public int SumTree (TreeNode n) {
    if(n.isLeafNode) return n.value;
    return SumTree(n.left) + SumTree(n.right);
}

假设一棵二叉树。

【讨论】:

  • 它不是二叉树,但是,如果你能简单解释一下它背后的前提,我也许可以充实我自己的treeObject来满足这种逻辑。从您提供的代码中,我需要知道 n.left 和 n.right 在结构方面代表什么。只要我能理解如何正确实现它,这绝对看起来是一个非常简单的解决方案。
  • .left 和 .right 代表该特定分支的子级。如果它不是二叉树,则可以改为遍历其所有子节点并在每个子节点上调用 SumTree。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-28
  • 2021-05-20
  • 2015-05-15
  • 2011-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多