-2

Objective is to create a trie data structure. I had seen Tree::Trie and used it. It converts data into trie structure only after the file(database) is read. So this will make processing slow as each time a lookup is needed entire data is converted into trie.

Is there a way where I can create a trie for one time and use it the trie structure for lookups.

Dada
  • 6,313
  • 7
  • 24
  • 43
Nagaraju
  • 1,853
  • 2
  • 27
  • 46
  • 1
    If you look at the [sources](http://cpansearch.perl.org/src/AVIF/Tree-Trie-1.5/Trie.pm) of `Tree::Trie`, you'll see that the data are added to the trie as soon as you `add` them (you can see `add` calling `_add_internal`). And the documentation states as much. – Dada Aug 09 '16 at 12:30

1 Answers1

5

If you look again at the synopsis in the Tree::Trie documentation that you linked to, you will see that the trie is only created once (my($trie) = new Tree::Trie; - although this should be written as my $trie = Tree::Trie->new; rather than using indirect object notation) and data is only added to it once ($trie->add(...);), and then the trie is used for multiple lookups (my(@all) = $trie->lookup(""); and my(@ms) = $trie->lookup("m");).

The way to create the trie once and then use it for lookups, then, is to simply keep the $trie variable around (in scope) and use it for all your lookups instead of creating new Tree::Trie instances each time.

If this answer isn't useful to you, please update your question to include a small, self-contained, runnable example program showing how you're using Tree::Trie and we can show you how to modify it so that the trie only gets built once.

Dave Sherohman
  • 45,363
  • 14
  • 64
  • 102
  • I want the database to be stored in trie format. Something like the database will be converted to trie structure which means database might be similar to a object file. In simple words is there a way where I can compile the database so that a trie structureed object file is generated which can be used for lookup. – Nagaraju Aug 09 '16 at 07:31