Блог пользователя malviyanshiv

Автор malviyanshiv, история, 5 лет назад, По-английски

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.

  • Проголосовать: нравится
  • +1
  • Проголосовать: не нравится

»
5 лет назад, # |
  Проголосовать: нравится +3 Проголосовать: не нравится

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.

  • »
    »
    5 лет назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    Thanks!!!
    Edit : Can you give some points to remember while implementing it. Like size to keep or something based on your experience ?