为什么我的 trie 设置中同一个字符的根不同?我怎样才能打印 trie 本身?
Why is the root different for the same character in my trie set-up? And how can I print the trie itself?
我正在设置 trie
,这是我的代码:
class Trie:
def __init__(self):
self.root = {}
self.endSymbol = "*"
def add(self, word):
node = self.root
for letter in word:
if letter not in node:
node[letter] = {}
node = node[letter]
node[self.endSymbol] = word
def multiStringSearch(smallStrings):
trie = Trie()
for word in smallStrings:
trie.add(word)
return trie
print(multiStringSearch(["abc", "mnopqr", "wyz", "no", "e", "tuuv","abb"])
两个问题:
- 我怎样才能
print out the trie
?
- 出于某种原因,上面没有生成正确的 trie,例如,对于
'n'
,"mnopqr"
和 "no"
应该在 same root
之下,但它们通过上述构造分开出现。
感谢 John 和 Ervin 的贡献,我可以检查一下,trie 设置会产生以下哪些结果?:
- 用于打印出
trie
:
class Trie:
def __init__(self):
self.root = {}
self.endSymbol = "*"
def add(self, word):
node = self.root
for letter in word:
if letter not in node:
node[letter] = {}
node = node[letter]
node[self.endSymbol] = word
def __str__(self):
return str(self.root)
def multiStringSearch(smallStrings):
trie = Trie()
for word in smallStrings:
trie.add(word)
return trie
if __name__ == "__main__":
trie = multiStringSearch(["abc", "mnopqr", "wyz", "no", "e", "tuuv","abb"])
print(trie)
- 不,它们不应该在同一个根下,因为它们没有共同的前缀。在你的例子中
abb
和 abc
应该在同一个根下,如果你打印出 `trie.
ad 1) 以下代码将打印带缩进的树(以便于检查树)
class Trie:
def __init__(self):
self.root = {}
self.endSymbol = "*"
def add(self, word):
node = self.root
for letter in word:
if letter not in node:
node[letter] = {}
node = node[letter]
node[self.endSymbol] = word
def __str__(self):
return self.__format(self.root)
def __format(self, node, indent = 0):
s = ''
for key in node:
s += ' ' * indent
s += key
if key == self.endSymbol:
s += ' => ' + node[key]
s += '\n'
else:
s += '\n'
s += self.__format(node[key], indent + 1)
return s
def multiStringSearch(smallStrings):
trie = Trie()
for word in smallStrings:
trie.add(word)
return trie
print(multiStringSearch(["abc", "mnopqr", "wyz", "no", "e", "tuuv","abb"]))
输出:
a
b
c
* => abc
b
* => abb
m
n
o
p
q
r
* => mnopqr
w
y
z
* => wyz
n
o
* => no
e
* => e
t
u
u
v
* => tuuv
我正在设置 trie
,这是我的代码:
class Trie:
def __init__(self):
self.root = {}
self.endSymbol = "*"
def add(self, word):
node = self.root
for letter in word:
if letter not in node:
node[letter] = {}
node = node[letter]
node[self.endSymbol] = word
def multiStringSearch(smallStrings):
trie = Trie()
for word in smallStrings:
trie.add(word)
return trie
print(multiStringSearch(["abc", "mnopqr", "wyz", "no", "e", "tuuv","abb"])
两个问题:
- 我怎样才能
print out the trie
? - 出于某种原因,上面没有生成正确的 trie,例如,对于
'n'
,"mnopqr"
和"no"
应该在same root
之下,但它们通过上述构造分开出现。
感谢 John 和 Ervin 的贡献,我可以检查一下,trie 设置会产生以下哪些结果?:
- 用于打印出
trie
:
class Trie:
def __init__(self):
self.root = {}
self.endSymbol = "*"
def add(self, word):
node = self.root
for letter in word:
if letter not in node:
node[letter] = {}
node = node[letter]
node[self.endSymbol] = word
def __str__(self):
return str(self.root)
def multiStringSearch(smallStrings):
trie = Trie()
for word in smallStrings:
trie.add(word)
return trie
if __name__ == "__main__":
trie = multiStringSearch(["abc", "mnopqr", "wyz", "no", "e", "tuuv","abb"])
print(trie)
- 不,它们不应该在同一个根下,因为它们没有共同的前缀。在你的例子中
abb
和abc
应该在同一个根下,如果你打印出 `trie.
ad 1) 以下代码将打印带缩进的树(以便于检查树)
class Trie:
def __init__(self):
self.root = {}
self.endSymbol = "*"
def add(self, word):
node = self.root
for letter in word:
if letter not in node:
node[letter] = {}
node = node[letter]
node[self.endSymbol] = word
def __str__(self):
return self.__format(self.root)
def __format(self, node, indent = 0):
s = ''
for key in node:
s += ' ' * indent
s += key
if key == self.endSymbol:
s += ' => ' + node[key]
s += '\n'
else:
s += '\n'
s += self.__format(node[key], indent + 1)
return s
def multiStringSearch(smallStrings):
trie = Trie()
for word in smallStrings:
trie.add(word)
return trie
print(multiStringSearch(["abc", "mnopqr", "wyz", "no", "e", "tuuv","abb"]))
输出:
a
b
c
* => abc
b
* => abb
m
n
o
p
q
r
* => mnopqr
w
y
z
* => wyz
n
o
* => no
e
* => e
t
u
u
v
* => tuuv