tlwg / libdatrie

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

complexity for searching

flackbash opened this issue · comments

The Readme suggests that this algorithm has a O(1) time complexity for searching.
However, every other source I read so far about time complexity of tries claims that the complexity is O(M) for searching where M is the maximum length of all inserted words. Which also does make more sense to me intuitively because in my imagination, the algorithm works like this: for every letter in the prefix I enter, the algorithm takes the corresponding path in the tree. This means it has to follow at least the number of letters in my prefix and at most M edges.
Can you explain to me where (or if) I'm wrong here?

Surely you are not wrong. The intention of O(1) statement is to claim that it's not dependent on the database size, just like how hash table does similarly on non-colliding cases, despite the fact that it still takes O(m) on hash function evaluation.

Anyway, I agree to add some more precision to the description. Let me revise it like this:

Before:

Trie is a kind of digital search tree, an efficient indexing method with O(1) time complexity for searching.

After:

Trie is a kind of digital search tree, an efficient indexing method in which search time is independent of database size. It only takes O(m) search time, where m is the length of the search string.

Thanks a lot for the clarification!