Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何编写uuu iter_uuuuufor从叶节点迭代回根节点?_Python_Search_Tree_Iterator - Fatal编程技术网

Python 如何编写uuu iter_uuuuufor从叶节点迭代回根节点?

Python 如何编写uuu iter_uuuuufor从叶节点迭代回根节点?,python,search,tree,iterator,Python,Search,Tree,Iterator,我正在实现一个搜索算法(BFS),并拥有以下节点类: class Node: def __init__(self, state=None, action=None, path_cost=None, parent=None): self._state = state self._action = action self._path_cost = path_cost self._parent = parent def

我正在实现一个搜索算法(BFS),并拥有以下
节点
类:

class Node:
    def __init__(self, state=None, action=None, path_cost=None, parent=None):
        self._state = state
        self._action = action
        self._path_cost = path_cost
        self._parent = parent
    def path_cost(self):
        self._cost = self._node.path_cost
        node = self._node.parent
        while node:
            self._cost += node.path_cost
            node = node.parent

        return self._cost
我的BFS解算器返回解决方案节点(子节点)。例如,使用此节点,我可以按如下方式计算总路径成本(此代码是另一个
Summary
类的一部分):


通过在
节点
中创建一个自定义的
iter\uuuuuuuu
方法,是否有更好的方法来实现这一点?

类似于此生成器函数的功能将起作用:

class Node:
    def __iter__(self):
        node = self
        while node:
            yield node
            node = node._parent

# elsewhere
cost = sum(n.path_cost for n in self._node)
    # ....

谢谢你的回答。虽然我看到了它是如何工作的,但我的具体问题是
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
能否用来解决这个问题?如果没有,我需要了解为什么没有更新。
\uuuu iter\uuuu
本身可以是发电机功能。