我正在尝试在 Java 中为文本编辑器实现一个包含 203675 个单词的 trie 结构。
以前,我使用 ArrayList 来存储单词,这需要 90 兆字节的空间。所以我想用一个 trie 来最小化空间消耗。
这是我到目前为止所拥有的,但现在空间消耗为 250 兆字节。这种增加的原因是什么?
package TextEditor;
import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;
class Vertex {
int words;
Map<Character, Vertex> child;
public Vertex() {
words = 0;
child = new HashMap<>();
}
}
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>=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 && v.words != 0) {
if(!store) {
System.out.println(s);
}
else {
if(s.length() >= startsWith.length()) {
list.add(s);
}
}
}
for (Map.Entry<Character, Vertex> entry : v.child.entrySet()) {
Character c = entry.getKey();
if((startsWith == null) || (ind>=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<String> 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 >= 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);
}
}
有我可以使用的 trie 结构的工作示例吗?