在Python中递归求和并打印树中的所有节点名称和值

Xarena

我从此链接中找到了代码

class Node:
    def __init__(self, name, weight, children):
        self.children = children
        self.weight = weight
        self.weight_plus_children = weight

    def get_all_weight(self):
        if self.children is None:
          return self.weight_plus_children
        else:
          for child in self.children:
            print("child.get_all_weight()", child.get_weigth_with_children())
            self.weight_plus_children += child.get_weigth_with_children()

        return self.weight_plus_children

    def get_weigth_with_children(self):
        return self.weight_plus_children

leaf1 = Node('C1', 58, None)
leaf2 = Node('C2', 7, None)
leaf3 = Node('C3', 10, None)
leaf4 = Node('C4', 20, None)

subroot = Node('B1', 50, [leaf1, leaf2])
subroot1 = Node('B2', 50, [leaf3, leaf4])

root = Node('A', 100, [subroot, subroot1])

print(subroot.get_all_weight())
print(subroot1.get_all_weight())
print(root.get_all_weight())

出:

child.get_all_weight() 58
child.get_all_weight() 7
115
child.get_all_weight() 10
child.get_all_weight() 20
80
child.get_all_weight() 115
child.get_all_weight() 80
295

现在,child.get_all_weight()我希望代替,在输出中显示节点名称:

我如何才能产生如下类似的结果(如果很难实现,则不必完全相同)?

Value of leaf C1: 58
Value of leaf C2: 7
Sum of nodes B1: 115

Value of leaf C3: 10
Value of leaf C4: 20
Sum of nodes B2: 80

Sum of nodes A: 295

非常感谢。

安莫尔·辛格·贾吉(Anmol Singh Jaggi)
from collections import deque

class Node:
    def __init__(self, name, weight, children):
        self.name = name
        self.children = children
        self.weight = weight
        self.weight_plus_children = weight

    def get_all_weight(self):
        if self.children is None:
          return self.weight_plus_children
        for child in self.children:
            self.weight_plus_children += child.get_all_weight()
        return self.weight_plus_children

    
def print_tree(root: Node):
    queue = deque()
    queue.append(root)
    while queue:
        front = queue.popleft()
        print('{} = {}'.format(front.name, front.weight_plus_children))
        if front.children:
            for child in front.children:
                if child is not None:
                    queue.append(child)


leaf1 = Node('C1', 58, None)
leaf2 = Node('C2', 7, None)
leaf3 = Node('C3', 10, None)
leaf4 = Node('C4', 20, None)

subroot = Node('B1', 50, [leaf1, leaf2])
subroot1 = Node('B2', 50, [leaf3, leaf4])

root = Node('A', 100, [subroot, subroot1])

root.get_all_weight()

print_tree(root)

输出:

A = 295
B1 = 115
B2 = 80
C1 = 58
C2 = 7
C3 = 10
C4 = 20

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

PHP 从嵌套的 XML 字符串中检索所有节点名称和节点值

如何从 XmlDocument 中读取带有节点名称的所有节点值

对Python中n元树的所有节点求和

如何在二叉查找树中给定值下的所有节点求和?

遍历递归定义的树中的所有节点

在n数组树中查找具有相同名称的节点的所有子节点和子节点

如何使用Java从XML文档中获取所有节点名称的列表

Python:如何从列表中的dict中对所有具有不同名称的值求和

使用 XSLT 以 XML 格式打印两个标记之间的所有节点名称

在Go中递归打印地图中的所有值

通过指针从 kd 树中递归打印节点

杰克逊JSON:从JSON树中获取节点名称

更改JTree中的节点名称

在ESB中更改xml节点名称

在Java Jung中添加节点名称

zookeeper 中的节点名称限制

在python中以点名称导入

如何在 XSLT 中访问具有动态节点名称的节点的子节点

如何在 Tensorflow 中打印和加载所有变量的名称和值

通过递归找到树中从节点到根的所有父代

当打印出树中的所有节点时,递归或迭代是否更有效?

如何在Firebase Android中检索节点名称和节点属性

Groovy比较两个具有未知节点名称和值的json

Groovy:查找并替换所有节点名称

Go中是否有一个函数可以打印对象的所有当前成员名称和值?

在XML C#中首次出现后更改子节点名称的值

如何使用Oracle SQL查询选择XML中的特定节点名称及其值?

如何对列中的所有值求和?

如何对字典中的所有值求和?