### StoneCold_'s blog

By StoneCold_, 5 years ago, ,

Please can anyone provide a good implementation of trie using a 2D array instead of using recursion ?

• -1

 » 5 years ago, # |   +1 let f be the matrix representation of your trielet f[k] be the list of links for the k-th nodelet f[k][x] = m, the node who represents the son of k-th node using x-th character, m = -1 is there is not a link. int MAX = Max number of nodes int CHARSET = alphabet size int ROOT = 0 int sz = 1; f[MAX][CHARSET] void init() { fill(f, -1); } void insert(char [] s) { int node = ROOT; for (int i = 0; i < size(s); i++) { if ( f[node][ s[i] ] == -1 ) f[node][ s[i] ] = sz++; node = f[node][ s[i] ]; } } Notes: Root node is at f[0] sz is the numbers of nodes currently in trieI guess would be easy to you implement other functions.
•  » » 2 years ago, # ^ |   0 can you explain it by taking an example?
•  » » 2 years ago, # ^ |   0 It is an explanation where you are storing the size of the trie ..But it is not suitable when we need to use this for finding the prefixes or calculating the xor or something like that ...Is it ???
•  » » 15 months ago, # ^ |   -8 How to mark a complete word? Using another array of same size of f? If we can't mark complete word, incomplete word will be found by searching.
 » 5 years ago, # |   0 It's my implementation of trie without using recursion and with using 2D array.
•  » » 4 years ago, # ^ |   0 Nice code!! Thanks a lot.. Helped a lot
•  » » 3 years ago, # ^ |   -22 chrome Can you please explain your code. It would be really nice and helpful.
•  » » 17 months ago, # ^ |   -12 Thanks chrome!
•  » » 9 months ago, # ^ |   -8 thanks,,can you plz add a delete operation?
 » 4 years ago, # |   -8 You can find it in PrinceOfPersia's blog on data structures.
 » 3 years ago, # |   0 My code hereI like this because 'add' and 'query' are in the same function (: