def BFS(self, rootnode):
visited = []
queue = []
queue.append(rootnode)
while queue:
print(type(queue))
curr_node = (queue.pop())
for node in curr_node.nodes:
if node not in visited:
visited.append(node)
queue.append(node)
...
嗨,所以我试图遍历这个trie对象,但是我无法迭代它的值。数据结构保存节点,和 每个节点包含两个值,一个字符值和一个孩子的字典(在nodes = {}中保存)。
我目前正在获取一个AttributeError:str在“for curr_node.nodes中的节点”行中没有属性节点: 我似乎无法弄清楚为什么。我检查了返回类型Node的根节点的类型。我检查了返回类dict的队列节点对象的第一个值的类型。但是当我试图迭代那个字典时 我得到了属性错误。 *我也知道dict中的每个键都与一个节点配对。
我是否正确地完成了dicts?
*这是最初的通话 trie.BFS(trie.root.nodes [ '#'])
由于
答案 0 :(得分:0)
如果没有看到节点的类定义,很难分辨出问题所在。但如果我不得不猜测,curr_node.nodes包含一个str值。
以下轻微更改可能帮助。
if node not in visited and not isinstance(node,str):
visited.append(node)
queue.append(node)
另外,如果访问是一个集合,它会更有效。