特里的复杂性和搜索 [英] Trie complexity and searching
问题描述
什么是创建单词的列表的一个线索的复杂性,什么是复杂搜索另一组单词在线索? 我应该使用线索字符串搜索,当我有哈希表?
在创建线索的复杂性是 O(宽*长)
,其中是W
是字的数目,和→
是词的平均长度:您需要执行→
查找平均每个 W的
词语的设定。
也是一样的找单词后:执行→
步骤为每个是W
字。 P>
哈希插入和查找具有相同的复杂性:每个字,你需要检查的平等,这需要 0(1)
,为的总体复杂性 O(宽*长)
。
如果你需要查找整个单词,哈希表比较简单。但是,你不能看由他们使用哈希表preFIX字;如果preFIX为基础的查找是不感兴趣的您,请使用哈希表;否则,使用一个线索。
What is the complexity of creating a trie of a list of words and what is complexity of searching other set of word in that trie? Should I use trie for string searching, when i have hashtable?
The complexity of creating a trie is O(W*L)
, where W
is the number of words, and L
is an average length of the word: you need to perform L
lookups on the average for each of the W
words in the set.
Same goes for looking up words later: you perform L
steps for each of the W
words.
Hash insertions and lookups have the same complexity: for each word you need to check equality, which takes O(L)
, for the overall complexity of O(W*L)
.
If you need to look up entire words, hash table is easier. However, you cannot look up words by their prefix using a hash table; If prefix-based lookups are of no interest to you, use a hash table; otherwise, use a trie.
这篇关于特里的复杂性和搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!