查找从根到一个特定叶的所有路径

时间:2018-12-31 15:05:49

标签: python-3.x recursion tree

我正在尝试打印所有可能的路径以从根获得特定的叶子。每个路径都需要用“ True”列表表示(如果我使用肯定选项,则用“ False”表示)。树上的叶子是病,彼此结点是一种症状。我试图通过运行一个包含路径的列表和另一个包含所有可能路径(如果有多个路径)的列表来执行此操作。我不断收到错误消息:“ AttributeError:'NoneType'对象没有属性'data'”

我的代码分为一个主要功能和一个辅助功能。诊断程序是我正在使用的类。

def paths_to_illness(self, illness):
    temp_lst = []
    final_lst = []
    self.path_helper(illness, temp_lst, final_lst)
    return final_lst

def path_helper(self, illness, path_lst, total_paths_lst):
    if illness == self.root.data and (not self.root.positive_child or
                                      not self.root.negative_child):
        total_paths_lst.append(path_lst)
        path_lst.pop()
        return
    if not self.root.negative_child or not self.root.positive_child:
        path_lst.pop()
    if self.root.negative_child:
        path_lst.append(False)
        neg = Diagnoser(self.root.negative_child)
        neg.path_helper(illness, path_lst, total_paths_lst)
    else:
        path_lst.append(True)
        pos = Diagnoser(self.root.positive_child)
        pos.path_helper(illness, path_lst, total_paths_lst)

感谢您的帮助。

0 个答案:

没有答案
相关问题