- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我用 Python 编写了一个 Trie 作为一个类。搜索和插入功能很清楚,但现在我尝试编写 python 函数 __str__
,以便我可以在屏幕上打印它。但是我的功能不起作用!
class Trie(object):
def __init__(self):
self.children = {}
self.val = None
def __str__(self):
s = ''
if self.children == {}: return ' | '
for i in self.children:
s = s + i + self.children[i].__str__()
return s
def insert(self, key, val):
if not key:
self.val = val
return
elif key[0] not in self.children:
self.children[key[0]] = Trie()
self.children[key[0]].insert(key[1:], val)
现在如果我创建一个 Trie 对象:
tr = Trie()
tr.insert('hallo', 54)
tr.insert('hello', 69)
tr.insert('hellas', 99)
当我现在打印 Trie 时,出现了条目 hello 和 hellas 不完整的问题。
print tr
hallo | ellas | o
我该如何解决这个问题?
最佳答案
为什么不让 str 实际上以存储的格式转储数据:
def __str__(self):
if self.children == {}:
s = str(self.val)
else:
s = '{'
comma = False
for i in self.children:
if comma:
s = s + ','
else:
comma = True
s = s + "'" + i + "':" + self.children[i].__str__()
s = s + '}'
return s
结果是:
{'h':{'a':{'l':{'l':{'o':54}}},'e':{'l':{'l':{'a':{'s':99},'o':69}}}}}
关于python - 在 Python 中实现 Trie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16932772/
我正在尝试做的事情: 构建一个移动网络应用程序,用户可以在玩拼字游戏时获得帮助找到要玩的单词 用户通过输入任意数量的字母和 0 个或多个通配符来获得单词建议 我是如何尝试做到这一点的: 使用 MySQ
假设我有一个 trie 包含多个字符串的数据结构。要在 trie 中查找字符串,我会从根开始,然后按顺序跟随标有字符串适当字符的指针,直到到达给定节点。 现在假设我想为同一组字符串构建一个“反向 tr
我正在阅读 Ingersoll、Morton 和 Farris 撰写的 Taming Text,但我不明白 solr 的数字 trie 实现如何帮助搜索文本?我对 solr.TrieField fie
我正在阅读 Ingersoll、Morton 和 Farris 的 Taming Text,但我不明白 solr 的数字 trie 实现如何帮助搜索文本?我对 solr 的 solr.TrieFiel
我正在开发一个 Trie 数据结构,其中每个节点代表一个词。所以词 st, stack, stackoverflow 和 overflow 将被排列为 root --st ---stack -----
trie 和radix trie 数据结构是一回事吗? 如果它们不相同,那么 radix trie (AKA Patricia trie) 是什么意思? 最佳答案 基数树是 trie 的压缩版本。在
我用过 video理解前缀特里树(虽然最终我试图最终得到后缀特里树)但是示例代码的链接被破坏所以我从视频中想出了这个,有两个功能,即插入和搜索如下 void insert(string word)
本文关键词:Leetcode, 力扣,Trie, 前缀树,字典树,208,Python, C++, Java 题目地址:https://leetcode.com/problems/implement
这是我使用 trie.c 和 trie.h 制作的主文件。该程序的目的是存储字典文件中的单词。 node* x = (node*)malloc(sizeof(node)); x = insert("b
因此,我必须搜索缺少字母(用于填字游戏)的单词,并且还必须维护剩余空格的可能单词列表。 现在我的问题是,我已经用谷歌搜索了 burst-trie 是否是最快的搜索算法。但是,如果我在 trie 中编写
我正在尝试将这个 trie 实现用于 ocaml:http://www.lri.fr/~filliatr/ftp/ocaml/ds/trie.ml.html 这是我对模块“M”的实现: module
我试图将 trie 中的所有单词放入字符串中,单词由 eow 字段表示,对于 trie 数据结构中的某个字符为 true,因此 trie 可以有字母但没有单词,例如“abc”在 trie 中,但“c”
我不太了解尝试字符串匹配中使用的实际算法。 我想知道为什么似乎更关注字符串匹配的后缀尝试而不是前缀尝试。我们可以不使用前缀尝试来进行子字符串匹配吗?换句话说,后缀尝试相对于前缀尝试有什么优势? 最佳答
当你构建一个特里树时,你是否将字符串/句子存储在其分支的末尾,以便在分支的末尾轻松访问它?有些人这样做,我有时也这样做,但我应该这样做吗? 有时(尤其是使用 LeetCode),我会收到此错误: Li
鉴于以下情况... (def inTree '((1 2) (1 2 3) (1 2 4 5 9) (1 2 4 10 15) (1 2 4 20 25))) 你如何将它转换
我想为高棉语(一种单词之间没有空格的语言)添加一个开源 Java 单词分割程序。开发人员已经很长时间没有开发它了,我无法联系他们了解详细信息(http://sourceforge.net/projec
我有一个字典文件(仅包含小写字母和撇号的单词),它作为特里树加载。 我有一个检查函数,它检查文件中的单词是否存在于特里树中树,无论字母大小写。 一切正常,除了撇号的单词总是拼写错误。 这是我的函数 b
我一直在练习 trie 数据结构(与类(class)作业无关)。此类用于存储字符串的子字符串。对于长度为 n 的字符串,总共有 n(n+1)/2 个子字符串。特别是 trie 的这种实现保留了自然顺序
所以我创建了一个包含大量数据的 trie,我的搜索算法非常快,但我想看看是否有人知道我如何才能让它更快。 bool search (string word) { int wordLen
我正在尝试用 C++ 实现 Trie,但出现运行时错误... 这是我的代码: #include using namespace std; struct trie{ bool word = f
我是一名优秀的程序员,十分优秀!