如何使用生成器迭代树的叶子

发布于 2024-09-24 16:33:49 字数 1575 浏览 2 评论 0原文

问题:

我有一个 trie 并且我想返回存储的信息在其中。有些叶子有信息(设置为值> 0),有些叶子没有。我只想返回那些有价值的叶子。

因为在所有 trie 中,每个节点上的叶子数量都是可变的,并且每个值的键实际上由到达每个叶子所需的路径组成。

我正在尝试使用生成器来遍历树后序,但我无法让它工作。我做错了什么?

我的模块:

class Node():
    '''Each leaf in the trie is a Node() class'''
    def __init__(self):
        self.children = {}
        self.value = 0

class Trie():
    '''The Trie() holds all nodes and can return a list of their values'''
    def __init__(self):
        self.root = Node()
    def add(self, key, value):
        '''Store a "value" in a position "key"'''
        node = self.root
        for digit in key:
            number = digit
            if number not in node.children:
                node.children[number] = Node()
            node = node.children[number]
        node.value = value
    def __iter__(self):
        return self.postorder(self.root)
    def postorder(self, node):
        if node:
            for child in node.children.values():
                self.postorder(child)
            # Do my printing / job related stuff here
            if node.value > 0:
                yield node.value

示例使用:

>>trie = Trie()
>>trie.add('foo', 3)
>>trie.add('foobar', 5)
>>trie.add('fobaz', 23)

>>for key in trie:
>>....print key
>>
3
5
23

我知道给出的示例很简单,可以使用任何其他数据结构来解决。然而,对于该程序来说,使用 trie 非常重要,因为它对于数据访问模式非常有益。

感谢您的帮助!

注意:我在代码块中省略了换行符,以便能够更轻松地复制粘贴。

The problem:

I have a trie and I want to return the information stored in it. Some leaves have information (set as value > 0) and some leaves do not. I would like to return only those leaves that have a value.

As in all trie's number of leaves on each node is variable, and the key to each value is actually made up of the path necessary to reach each leaf.

I am trying to use a generator to traverse the tree postorder, but I cannot get it to work. What am I doing wrong?

My module:

class Node():
    '''Each leaf in the trie is a Node() class'''
    def __init__(self):
        self.children = {}
        self.value = 0

class Trie():
    '''The Trie() holds all nodes and can return a list of their values'''
    def __init__(self):
        self.root = Node()
    def add(self, key, value):
        '''Store a "value" in a position "key"'''
        node = self.root
        for digit in key:
            number = digit
            if number not in node.children:
                node.children[number] = Node()
            node = node.children[number]
        node.value = value
    def __iter__(self):
        return self.postorder(self.root)
    def postorder(self, node):
        if node:
            for child in node.children.values():
                self.postorder(child)
            # Do my printing / job related stuff here
            if node.value > 0:
                yield node.value

Example use:

>>trie = Trie()
>>trie.add('foo', 3)
>>trie.add('foobar', 5)
>>trie.add('fobaz', 23)

>>for key in trie:
>>....print key
>>
3
5
23

I know that the example given is simple and can be solved using any other data structure. However, it is important for this program to use a trie as it is very beneficial for the data access patterns.

Thanks for the help!

Note: I have omitted newlines in the code block to be able to copy-paste with greater ease.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸里的快感 2024-10-01 16:33:49

更改

self.postorder(child)

for n in self.postorder(child):
    yield n

似乎可以使其工作。

PS 省略空白行以方便剪切和粘贴对您非常有帮助。粘贴 :)

Change

self.postorder(child)

to

for n in self.postorder(child):
    yield n

seems to make it work.

P.S. It is very helpful for you to left out the blank lines for ease of cut & paste :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文