Note that there are some explanatory texts on larger screens.

plurals
  1. POBuild trie faster
    primarykey
    data
    text
    <p>I'm making an mobile app which needs thousands of fast string lookups and prefix checks. To speed this up, I made a Trie out of my word list, which has about 180,000 words. </p> <p>Everything's great, but the only problem is that building this huge trie (it has about 400,000 nodes) takes about <strong>10 seconds</strong> currently on my phone, which is really slow.</p> <p>Here's the code that builds the trie.</p> <pre><code>public SimpleTrie makeTrie(String file) throws Exception { String line; SimpleTrie trie = new SimpleTrie(); BufferedReader br = new BufferedReader(new FileReader(file)); while( (line = br.readLine()) != null) { trie.insert(line); } br.close(); return trie; } </code></pre> <p>The <code>insert</code> method which runs on <code>O(length of key)</code></p> <pre><code>public void insert(String key) { TrieNode crawler = root; for(int level=0 ; level &lt; key.length() ; level++) { int index = key.charAt(level) - 'A'; if(crawler.children[index] == null) { crawler.children[index] = getNode(); } crawler = crawler.children[index]; } crawler.valid = true; } </code></pre> <p>I'm looking for intuitive methods to build the trie faster. Maybe I build the trie just once on my laptop, store it somehow to the disk, and load it from a file in the phone? But I don't know how to implement this.</p> <p>Or are there any other prefix data structures which will take less time to build, but have similar lookup time complexity?</p> <p>Any suggestions are appreciated. Thanks in advance.</p> <p><strong>EDIT</strong></p> <p>Someone suggested using Java Serialization. I tried it, but it was <strong>very</strong> slow with this code:</p> <pre><code>public void serializeTrie(SimpleTrie trie, String file) { try { ObjectOutput out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file))); out.writeObject(trie); out.close(); } catch (IOException e) { e.printStackTrace(); } } public SimpleTrie deserializeTrie(String file) { try { ObjectInput in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file))); SimpleTrie trie = (SimpleTrie)in.readObject(); in.close(); return trie; } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); return null; } } </code></pre> <p>Can this above code be made faster?</p> <p>My trie: <a href="http://pastebin.com/QkFisi09" rel="noreferrer">http://pastebin.com/QkFisi09</a></p> <p>Word list: <a href="http://www.isc.ro/lists/twl06.zip" rel="noreferrer">http://www.isc.ro/lists/twl06.zip</a></p> <p>Android IDE used to run code: <a href="http://play.google.com/store/apps/details?id=com.jimmychen.app.sand" rel="noreferrer">http://play.google.com/store/apps/details?id=com.jimmychen.app.sand</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload