后序遍历二叉树的Python代码

huangapple go评论51阅读模式
英文:

Tree postorder traversal in python

问题

我正在尝试构建一个逻辑,通过将子节点的点数的10%作为奖励添加到父节点,从而将点数从叶子节点添加到父节点。在这个示例中,节点b的额外点数应为6,因此b的总点数应为56,同样对于节点a,它应为111.6(100+11.6)。我正在使用bigtree,但也可以使用其他库。

from bigtree import nested_dict_to_tree, print_tree

path_dict = {
   "name": "a",
   "points": 100,
   "children": [
      {
         "name": "b",
         "points": 50,
         "children": [
            {"name": "d", "points": 40},
            {"name": "e", "points": 20},
         ],
      },
      {"name": "c", "points": 60},
   ],
}
root = nested_dict_to_tree(path_dict)

print_tree(root, attr_list=["points"])
英文:

I am trying to build a logic to add points from leaves to the paernt by adding 10% of points as reward to the parent from the sum of child node points , here in this example extra points for b have to be 6 so total b points have to be 56 , similarly for node a it should be 111.6 (100+11.6)` . I am using bigtree but fine to use some other libraries also.

from bigtree import nested_dict_to_tree, print_tree

path_dict = {
   "name": "a",
   "points": 100,
   "children": [
      {
         "name": "b",
         "points": 50,
         "children": [
            {"name": "d", "points": 40},
            {"name": "e", "points": 20},
         ],
      },
      {"name": "c", "points": 60},
   ],
}
root = nested_dict_to_tree(path_dict)

print_tree(root, attr_list=["points"])

答案1

得分: 1

另一种方法是定义一个自定义的 Node 类,执行递归计算并将其存储为另一个类属性 points_sum(而不更改原始的 points 属性)。

来源:bigtree 文档关于扩展节点

from bigtree import Node, print_tree, nested_dict_to_tree

class PointNode(Node):

    def __init__(self, name: str, **kwargs):
        super().__init__(name, **kwargs)

    @property
    def points_sum(self):
        if self.is_leaf:
            return self.points
        return self.points + 0.1 * sum([child.points_sum for child in self.children])

# 提供的代码
path_dict = {
   "name": "a",
   "points": 100,
   "children": [
      {
         "name": "b",
         "points": 50,
         "children": [
            {"name": "d", "points": 40},
            {"name": "e", "points": 20},
         ],
      },
      {"name": "c", "points": 60},
   ],
}
root = nested_dict_to_tree(path_dict, node_type=PointNode)

print_tree(root, attr_list=["points", "points_sum"])

这将产生以下输出:

a [points=100, points_sum=111.6]
├── b [points=50, points_sum=56.0]
   ├── d [points=40, points_sum=40]
   └── e [points=20, points_sum=20]
└── c [points=60, points_sum=60]

免责声明:我是 bigtree 的作者 后序遍历二叉树的Python代码

英文:

Another way would be to define a custom Node class that performs the recursive computation and stores it as another class attribute points_sum (without changing the original points attribute).

Source: bigtree Documentation on extending Nodes

from bigtree import Node, print_tree, nested_dict_to_tree


class PointNode(Node):

    def __init__(self, name: str, **kwargs):
        super().__init__(name, **kwargs)

    @property
    def points_sum(self):
        if self.is_leaf:
            return self.points
        return self.points + 0.1 * sum([child.points_sum for child in self.children])


# Your provided code
path_dict = {
   "name": "a",
   "points": 100,
   "children": [
      {
         "name": "b",
         "points": 50,
         "children": [
            {"name": "d", "points": 40},
            {"name": "e", "points": 20},
         ],
      },
      {"name": "c", "points": 60},
   ],
}
root = nested_dict_to_tree(path_dict, node_type=PointNode)

print_tree(root, attr_list=["points", "points_sum"])

This will result in the output

a [points=100, points_sum=111.6]
├── b [points=50, points_sum=56.0]
│   ├── d [points=40, points_sum=40]
│   └── e [points=20, points_sum=20]
└── c [points=60, points_sum=60]

Disclaimer: I'm the author of bigtree 后序遍历二叉树的Python代码

huangapple
  • 本文由 发表于 2023年2月16日 17:46:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75470444.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定