Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.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 返回与Trie中的前缀匹配的字符串_Python_Trie - Fatal编程技术网

Python 返回与Trie中的前缀匹配的字符串

Python 返回与Trie中的前缀匹配的字符串,python,trie,Python,Trie,我设法构造了一个Trie,现在我想返回与Trie前缀匹配的字符串,但是我在编写搜索函数时遇到了问题 例如,如果我有一个前缀“aa”,我希望将字符串“aa”和“aac”作为输出 class Node: def __init__(self): self.children = [None] * 26 self.end = False self.value = "" class Trie: def __init__(self):

我设法构造了一个Trie,现在我想返回与Trie前缀匹配的字符串,但是我在编写搜索函数时遇到了问题

例如,如果我有一个前缀“aa”,我希望将字符串“aa”和“aac”作为输出

class Node:
    def __init__(self):
        self.children = [None] * 26
        self.end = False
        self.value = ""

class Trie:
    def __init__(self):
        self.root = Node()

    def add_word(self, key):
        word_length = len(key)
        current = self.root
        for i in range(word_length):
            position = self.ord_char(key[i])

            if current.children[position] is None:
                current.children[position] = Node()
            current = current.children[position]
            current.value = key[i]
        current.end = True

    def ord_char(self,key):
        ord_rep = ord(key) - ord('a')
        return ord_rep

    def prefix_search(self, prefix):
        lst = []
        current = self.root
        prefix_length = len(prefix)
        for c in range(prefix_length):
            c_position = self.ord_char(prefix[c])
            current = current.children[c_position]
            lst.append(current.value)
        #doesnt seem like I'm doing it right

if __name__ == "__main__":
    trie = Trie()
    trie.add_word("aa")
    trie.add_word("aac")
    trie.add_word("b")
    trie.prefix_search("aa")

我想通过搜索功能将字母表连接在一起形成最终的字符串,但我想不出更好的方法。

怎么样,在我看来,这是实现搜索的简单方法。

怎么样,在我看来,这似乎是实现搜索的简单方法。

到目前为止,
lst
值只是前缀,分为不同的字母,但现在您需要处理在
children
属性中找到的不是
None
的每个节点,以查找所有
end
设置为
True
的节点。每次你找到这样一个节点,你就有了一个完整的单词。任何节点都可以再次有多个子节点,分支成更多的字输出

您可以使用堆栈来跟踪构建列表所需处理的所有节点及其前缀。使用该节点的前缀将子节点添加到堆栈中,并逐个处理这些节点(在执行此操作时向堆栈中添加更多子节点)

请注意,首先,您不需要构建前缀字符列表,您已经将该前缀作为变量。要到达起点,只需迭代前缀本身即可:

def prefix_search(self, prefix):
    current = self.root
    # get to the starting point
    for c in prefix:
        current = current.children[self.ord_char(c)]
        if current is None:
            # prefix doesn't exist, abort with an empty list
            return []

    found = []
    stack = [(current, prefix)]
    while stack:
        current, prefix = stack.pop()

        if current.end:
            # this is a complete word, named by prefix
            found.append(prefix)

        # add the children to the stack, each with their letter added to the
        # prefix value.
        for child in current.children:
            if child is None:
                continue
            stack.append((child, prefix + child.value))

    return found
对于给定的示例trie和前缀,堆栈从
'aa'
处的节点开始。第一次
while stack:
迭代将该节点从堆栈中删除,因为该节点的
end
设置为true,
'aa'
被添加到
found
。对于
c
,该节点只有一个非
None
子节点,因此该节点使用
'aac'
添加到堆栈中

然后,
循环重复时,发现堆栈上的一个元素sees
end
被设置为将
'aac'
添加到
found
,并且不再定位子节点。堆栈保持为空,循环结束时

演示:


到目前为止,
lst
值只是前缀,分为单独的字母,但现在需要处理在
子项
属性中找到的不是
None
的每个节点,以查找将
end
设置为
True
的所有节点。每次你找到这样一个节点,你就有了一个完整的单词。任何节点都可以再次有多个子节点,分支成更多的字输出

您可以使用堆栈来跟踪构建列表所需处理的所有节点及其前缀。使用该节点的前缀将子节点添加到堆栈中,并逐个处理这些节点(在执行此操作时向堆栈中添加更多子节点)

请注意,首先,您不需要构建前缀字符列表,您已经将该前缀作为变量。要到达起点,只需迭代前缀本身即可:

def prefix_search(self, prefix):
    current = self.root
    # get to the starting point
    for c in prefix:
        current = current.children[self.ord_char(c)]
        if current is None:
            # prefix doesn't exist, abort with an empty list
            return []

    found = []
    stack = [(current, prefix)]
    while stack:
        current, prefix = stack.pop()

        if current.end:
            # this is a complete word, named by prefix
            found.append(prefix)

        # add the children to the stack, each with their letter added to the
        # prefix value.
        for child in current.children:
            if child is None:
                continue
            stack.append((child, prefix + child.value))

    return found
对于给定的示例trie和前缀,堆栈从
'aa'
处的节点开始。第一次
while stack:
迭代将该节点从堆栈中删除,因为该节点的
end
设置为true,
'aa'
被添加到
found
。对于
c
,该节点只有一个非
None
子节点,因此该节点使用
'aac'
添加到堆栈中

然后,
循环重复时,发现堆栈上的一个元素sees
end
被设置为将
'aac'
添加到
found
,并且不再定位子节点。堆栈保持为空,循环结束时

演示:


他们找到了前缀位置,他们正试图重建所有带有该前缀的单词。Trie不是单个字符串,没有任何东西可供使用。
str.startswith()
on。成本如何?提出的算法要快得多。他们找到了前缀位置,他们正试图重建所有带有该前缀的单词。Trie不是单个字符串,没有任何东西可供使用。
str.startswith()
on。成本如何?该算法速度快得多。