Python 返回与Trie中的前缀匹配的字符串
我设法构造了一个Trie,现在我想返回与Trie前缀匹配的字符串,但是我在编写搜索函数时遇到了问题 例如,如果我有一个前缀“aa”,我希望将字符串“aa”和“aac”作为输出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):
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'
添加到堆栈中
然后,循环重复时,发现堆栈上的一个元素seesend
被设置为将'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'
添加到堆栈中
然后,循环重复时,发现堆栈上的一个元素seesend
被设置为将'aac'
添加到found
,并且不再定位子节点。堆栈保持为空,循环结束时
演示:
他们找到了前缀位置,他们正试图重建所有带有该前缀的单词。Trie不是单个字符串,没有任何东西可供使用。str.startswith()
on。成本如何?提出的算法要快得多。他们找到了前缀位置,他们正试图重建所有带有该前缀的单词。Trie不是单个字符串,没有任何东西可供使用。str.startswith()
on。成本如何?该算法速度快得多。