后序遍历二叉树的Python代码

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

Tree postorder traversal in python

问题

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

  1. from bigtree import nested_dict_to_tree, print_tree
  2. path_dict = {
  3. "name": "a",
  4. "points": 100,
  5. "children": [
  6. {
  7. "name": "b",
  8. "points": 50,
  9. "children": [
  10. {"name": "d", "points": 40},
  11. {"name": "e", "points": 20},
  12. ],
  13. },
  14. {"name": "c", "points": 60},
  15. ],
  16. }
  17. root = nested_dict_to_tree(path_dict)
  18. 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.

  1. from bigtree import nested_dict_to_tree, print_tree
  2. path_dict = {
  3. "name": "a",
  4. "points": 100,
  5. "children": [
  6. {
  7. "name": "b",
  8. "points": 50,
  9. "children": [
  10. {"name": "d", "points": 40},
  11. {"name": "e", "points": 20},
  12. ],
  13. },
  14. {"name": "c", "points": 60},
  15. ],
  16. }
  17. root = nested_dict_to_tree(path_dict)
  18. print_tree(root, attr_list=["points"])

答案1

得分: 1

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

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

  1. from bigtree import Node, print_tree, nested_dict_to_tree
  2. class PointNode(Node):
  3. def __init__(self, name: str, **kwargs):
  4. super().__init__(name, **kwargs)
  5. @property
  6. def points_sum(self):
  7. if self.is_leaf:
  8. return self.points
  9. return self.points + 0.1 * sum([child.points_sum for child in self.children])
  10. # 提供的代码
  11. path_dict = {
  12. "name": "a",
  13. "points": 100,
  14. "children": [
  15. {
  16. "name": "b",
  17. "points": 50,
  18. "children": [
  19. {"name": "d", "points": 40},
  20. {"name": "e", "points": 20},
  21. ],
  22. },
  23. {"name": "c", "points": 60},
  24. ],
  25. }
  26. root = nested_dict_to_tree(path_dict, node_type=PointNode)
  27. print_tree(root, attr_list=["points", "points_sum"])

这将产生以下输出:

  1. a [points=100, points_sum=111.6]
  2. ├── b [points=50, points_sum=56.0]
  3. ├── d [points=40, points_sum=40]
  4. └── e [points=20, points_sum=20]
  5. └── 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

  1. from bigtree import Node, print_tree, nested_dict_to_tree
  2. class PointNode(Node):
  3. def __init__(self, name: str, **kwargs):
  4. super().__init__(name, **kwargs)
  5. @property
  6. def points_sum(self):
  7. if self.is_leaf:
  8. return self.points
  9. return self.points + 0.1 * sum([child.points_sum for child in self.children])
  10. # Your provided code
  11. path_dict = {
  12. "name": "a",
  13. "points": 100,
  14. "children": [
  15. {
  16. "name": "b",
  17. "points": 50,
  18. "children": [
  19. {"name": "d", "points": 40},
  20. {"name": "e", "points": 20},
  21. ],
  22. },
  23. {"name": "c", "points": 60},
  24. ],
  25. }
  26. root = nested_dict_to_tree(path_dict, node_type=PointNode)
  27. print_tree(root, attr_list=["points", "points_sum"])

This will result in the output

  1. a [points=100, points_sum=111.6]
  2. ├── b [points=50, points_sum=56.0]
  3. ├── d [points=40, points_sum=40]
  4. └── e [points=20, points_sum=20]
  5. └── 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:

确定