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

# | User | Rating |
---|---|---|

1 | tourist | 3707 |

2 | Benq | 3672 |

3 | ksun48 | 3575 |

4 | Radewoosh | 3562 |

5 | Miracle03 | 3480 |

6 | maroonrk | 3406 |

7 | ecnerwala | 3400 |

8 | peehs_moorhsum | 3384 |

9 | sunset | 3338 |

10 | Um_nik | 3320 |

# | User | Contrib. |
---|---|---|

1 | 1-gon | 208 |

2 | YouKn0wWho | 203 |

3 | Um_nik | 197 |

4 | Errichto | 181 |

4 | sus | 181 |

6 | awoo | 179 |

7 | tourist | 175 |

8 | -is-this-fft- | 171 |

8 | SecondThread | 171 |

10 | Ashishgup | 170 |

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

↑

↓

Codeforces (c) Copyright 2010-2021 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Oct/28/2021 08:36:13 (k2).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|

let f be the matrix representation of your trie

let f[k] be the list of links for the k-th node

let 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.

Notes: Root node is at f[0] sz is the numbers of nodes currently in trie

I guess would be easy to you implement other functions.

can you explain it by taking an example?

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 ???

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.

It's my implementation of trie without using recursion and with using 2D array.

Nice code!! Thanks a lot.. Helped a lot

chrome Can you please explain your code. It would be really nice and helpful.

Thanks chrome!

thanks,,can you plz add a delete operation?

nice code :p

You can find it in PrinceOfPersia's blog on data structures.

My code here

I like this because 'add' and 'query' are in the same function (:

Why is this used instead of making nodes by using struct?

1) because accessing values in arrays can benefit from caching while it won't be the case with struct since every node will be allocated wherever memory is available on heap(i.e. not contiguously).

2) Dynamically allocating memory on heap is slower(i.e. using

`new`

when using struct).There might be other reasons but the above are the only one's which i can think of now.

does this have any significant benefit in competitions?

In this problem : https://www.codechef.com/SEPT15/problems/REBXOR/ my solution using normal trie(created using dynamic allocation) failed with TLE, while other's solution using 2D trie implementation passes easily.