[英]Trie - Implementation in java
I know there is plenty of material available regarding it but i had quite specific questions. 我知道有很多关于它的材料,但我有非常具体的问题。 I have a file containing postal codes and i have to create trie data structure using those codes.
我有一个包含邮政编码的文件,我必须使用这些代码创建trie数据结构。 I have written my implementation which is -
我写的是我的实现 -
public class Trie{
TrieNode root = null;
public void addWord(String zipCodeStr){
if(root==null){
root = new TrieNode();
}
TrieNode current = root;
for(char c : zipCodeStr.toCharArray()){
if(current.childern[Character.getNumericValue(c)]==null){
current.childern[Character.getNumericValue(c)] = new TrieNode();
}
current = current.childern[Character.getNumericValue(c)];
}
current.isWord = true;
}
public boolean exists(String zipCodeStr){
boolean result = true;
TrieNode current = root;
for(char c : zipCodeStr.toCharArray()){
if(current.childern[Character.getNumericValue(c)]==null){
result = false;
break;
}
current = current.childern[Character.getNumericValue(c)];
}
if(result && current.isWord){
result = true;
}else{
result = false;
}
return result;
}
private static class TrieNode{
TrieNode[] childern = new TrieNode[10];
boolean isWord = false;
public TrieNode() {
}
}
}
Here, i am not storing any value as position gives that information. 在这里,我没有存储任何值,因为position提供了该信息。
Questions - i) Can it be further improvised? 问题 - i)可以进一步即兴创作吗? ii) Raw text file size containing 27000+ codes is around 190kb and i checked the size of trie object using a profiler it came out to be much more.
ii)包含27000+代码的原始文本文件大小约为190kb,我使用分析器检查了trie对象的大小。
Are these two size related?
这两个尺寸有关吗? Should trie size be less than raw text file size?
trie size是否应小于原始文本文件大小?
Thanks, Ouney 谢谢,Ouney
Supposing that ~9/10 nodes are leafs (don't contain children), you can significantly decrease space that whole structure occupies by lazy initialization of children
array: 假设~9 / 10节点是叶子(不包含子节点),您可以通过
children
数组的延迟初始化显着减少整个结构占用的空间:
private static class TrieNode {
TrieNode[] children = null;
boolean isWord = false;
}
Now you need to create a new array only if it is actually needed: 现在,只有在实际需要时才需要创建新数组:
public void addWord(String zipCodeStr) {
if (root == null){
root = new TrieNode();
}
TrieNode current = root;
for (char c : zipCodeStr.toCharArray()) {
if (current.children == null) {
current.children = new TrieNode[10];
}
if (current.children[Character.getNumericValue(c)] == null) {
current.children[Character.getNumericValue(c)] = new TrieNode();
}
current = current.children[Character.getNumericValue(c)];
}
current.isWord = true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.