I have been toying with a trie
data structure for practice (there is not course work related). This class is used to store substrings of a string. For a string of length n
there are n(n+1)/2
total substrings. In particular this implementation of a trie
preserves natural ordering and is more efficient than a TreeMap
or TreeSet
on random strings. As well storing a single character rather than the entire string saves on memory.
I think for storing substrings a suffix array may be the better way to go but I wanted to make sure this trie class is reasonably optimized for speed before starting a new project.
class Trie
{
final Trie my_parent;
final Trie[] my_children;
final char my_value;
public Trie(final Trie the_parent, final char the_value)
{
my_parent = the_parent;
my_value = the_value;
my_children = new Trie[26];
}
public int insertIterative(final char[] the_text)
{
int number = 0;
Trie parent = this;
for(int ator = 0; ator < the_text.length; ator++)
{
final int key = the_text[ator] - 97;
Trie child = parent.my_children[key];
if(child == null)
{
child = new Trie(parent, the_text[ator]);
parent.my_children[key] = child;
number++;
}
parent = child;
}
return number;
}
public String getString()
{
final StringBuilder builder = new StringBuilder();
Trie parent = this;
while(parent.my_parent != null)
{
builder.append(parent.my_value);
parent = parent.my_parent;
}
return builder.reverse().toString();
}
}