【问题标题】:Recursively sum and print all nodes names and values from a tree in Python在 Python 中递归地求和并打印树中的所有节点名称和值
【发布时间】:2020-11-04 10:50:02
【问题描述】:

我从这个link找到了代码:

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

非常感谢。

【问题讨论】:

    标签: python-3.x class recursion tree


    【解决方案1】:
    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
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-03
    • 1970-01-01
    • 2020-12-08
    • 2017-10-30
    • 1970-01-01
    • 2022-09-27
    相关资源
    最近更新 更多