what
Trie, is actually a more general tree, also prefix tree.
Used for massive words process, dictionary representation, and lexicographic sorting, word frequency calc.
How
I. TrieNode
TrieNode has mainly two parts:
children
, to link next possible characters appear in a words. If only considering ‘A-Z’, there is 26 children at most- info used to record current TrieNode
isWord
, denotes previous character is a word or not.cnt
, denotes number of words consist of current prefix
Note: this representation is a little different from binary tree, in which info data are stored for current Node. However, in Trie, info data are stored for previous Node, as follows:
1 | For dictionary: { "a", "bd", "c" }, the Trie would be represented as following: |
Implementation
interfaces
- build (constructor)
- isWord
- countWord
- insert
- delete
- hasPrefix
- getPrefixNode
Time Complexity: [
n
words,K
is number of characters of the longest word ]- build: O(nK)
- isWord: O(K)
- countWord: O(K)
- insert: O(K)
- delete: O(K)
Space Complexity: [
N
is the number of all characters appears in the dictionary ]- too much space: O(N) +O(N^2) +O(N^3) +O(N^4) + O(N^K)
- good news is: when it is a really dictionary, all the space will be filled up, so not so much waste
delete
might be a little complex. The to-be-deleted word possibly :1
2
3
4
5
6when need delete a node, what should really to be done????
- delete the key from HashMap<Character, Node>
- reset to null when using Node[]
So, always ask two questions:
- the node to be deleted, is part of other long word [by checking 'children']?
- the node itself is the end of a word? [by checking 'isWord' & 'cnt']?null
, empty string, not exists in the Trie- contains other short words: “abc”, “ab”
- recursively from bottom to top delete node, till the longest prefix
- has same words as itself: “abc”, “abc”
- don’t delete node, only update
cnt
- don’t delete node, only update
- being a prefix of other long words: “abc”, “abcde”
- don’t delete node, only update
isWord
- don’t delete node, only update
- share prefix with other words: “abc”, “abd”
- recursively from bottom to top delete node, till the longest prefix
- only itself in the dictionary, not affect others “abc”
- delte all nodes
1 |
|