英文:
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
属性)。
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
的作者
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论