Trie

Download

Download jar file or use maven. psjava requires Java 1.6 (or above)

<dependency>
    <groupId>org.psjava</groupId>
    <artifactId>psjava</artifactId>
    <version>0.1.19</version>
</dependency>

Example Code

// Let's construct a character trie.
// here, put three sequences.

Trie<Character> trie = GoodTrieFactory.getInstance().create();
trie.add(ArrayFromVarargs.create('A', '1'));
trie.add(ArrayFromVarargs.create('A', '2'));
trie.add(ArrayFromVarargs.create('X', 'Y', 'Z'));

// We can get the number of children.
// There is 2 child nodes from the root.

int count = trie.getRoot().getChildCount(); // must be 2

// To get a child node by key, use following methods.

boolean hasChild = trie.getRoot().hasChild('A'); // must be true

// To iterate available children, use getEdges() method.
// There will be two edge('1', '2') for 'A' node.

TrieNode<Character> nodeA = trie.getRoot().getChild('A');
for (Character c : nodeA.getEdges()) {
	TrieNode<Character> child = nodeA.getChild(c);
}

Implementation


Copyright 2014 psjava team. View on GitHub