I am struggling to find a good article on implementing trie using array. I did get some code implementing the same and attempted to read them but unable to beat my doubts!!! Please share the idea or link of some good article.
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
I am struggling to find a good article on implementing trie using array. I did get some code implementing the same and attempted to read them but unable to beat my doubts!!! Please share the idea or link of some good article.
Name |
---|
To implement tries as arrays, we assign each unique prefix that occurs in input a unique code (number).
(I am explaining with regard to binary numbers but idea is easily extensible when operating with words)
The size of your trie should be Number of Unique Prefixes * Alphabet size. For binary numbers, this turns out to be $$$(N * log(Max)) * 2$$$.
Let the code of some prefix P be X then trie[X]['0'] stores the code of the prefix : X + '0'.
We initialize the trie 2D array with some sentinel value (for instance -1) and each time we encounter a new prefix we give it a new code and store it according to the scheme described above. This allows us to form the chains that is otherwise created by pointers.
Refer this code by dragoon for this problem for a clean implementation.
Thanks!!!
Edit : Can you give some points to remember while implementing it. Like size to keep or something based on your experience ?