Note that there are some explanatory texts on larger screens.

plurals
  1. POOptimizing space usage in a trie structure in a java program
    primarykey
    data
    text
    <p>I am trying to implement a trie structure in Java with 203675 words for a text editor.</p> <p>Before, I was using ArrayList to store the words and that was taking 90 megabytes of space. So I want to use a trie to minimize space consumption.</p> <p>Here is what I have so far, but now space consumption is 250 megabytes. What is the reason for this increase?</p> <pre><code>package TextEditor; import java.io.*; import java.util.*; import javax.swing.JOptionPane; class Vertex { int words; Map&lt;Character, Vertex&gt; child; public Vertex() { words = 0; child = new HashMap&lt;&gt;(); } } class Trie { private Vertex root; private InputStream openFile; private OutputStream openWriteFile; private BufferedReader readFile; private BufferedWriter writeFile; public Trie() { root = new Vertex(); } public Trie(String path) { try { root = new Vertex(); openFile = getClass().getResourceAsStream(path); readFile = new BufferedReader( new InputStreamReader(openFile)); String in = readFile.readLine(); while(readFile.ready()) { this.insert(in); try { in = readFile.readLine(); } catch (IOException ex) { JOptionPane.showMessageDialog(null, "TRIE CONSTRUCTION ERROR!!!!"); } } } catch (IOException ex) { JOptionPane.showMessageDialog(null, "TRIE CONSTRUCTION ERROR!!!!"); } } private void addWord(Vertex vertex, String s, int i) { try { if(i&gt;=s.length()) { vertex.words += 1; return; } char ind = s.charAt(i); if(!vertex.child.containsKey(ind)) { vertex.child.put(ind, new Vertex()); } addWord(vertex.child.get(ind), s, i+1); } catch(Exception e) { e.printStackTrace(); System.exit(1); } } final void insert(String s) { addWord(root, s.toLowerCase(), 0); } private void DFS(Vertex v, String s, ArrayList list, boolean store, String startsWith, int ind) { if(v != null &amp;&amp; v.words != 0) { if(!store) { System.out.println(s); } else { if(s.length() &gt;= startsWith.length()) { list.add(s); } } } for (Map.Entry&lt;Character, Vertex&gt; entry : v.child.entrySet()) { Character c = entry.getKey(); if((startsWith == null) || (ind&gt;=startsWith.length()) || (startsWith.charAt(ind) == c)) { DFS(v.child.get(c), s + c, list, store, startsWith, ind+1); } } } public void Print() { DFS(root, new String(""), null, false, null, 0); } ArrayList&lt;String&gt; getAsList(String startsWith) { ArrayList ret = new ArrayList(); DFS(root, new String(""), ret, true, startsWith, 0); return ret; } int count(Vertex vertex, String s, int i) { if(i &gt;= s.length()) { return vertex.words; } if(!vertex.child.containsKey(s.charAt(i))) { return 0; } return count(vertex.child.get(s.charAt(i)), s, i+1); } int count(String s) { return count(root, s, 0); } } </code></pre> <p>Is there a working example of a trie structure I can use?</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