/***************************************************************************
* Author: Isai Damier
* Title: Trie.java
* Project: geekviewpoint
* Package: datastructure
*
* Description:
* A trie is a tree data-structure that stores words by compressing common
* prefixes. To illustrate, following is a word list and its resulting trie.
*
* WORDS: rat, rats, rattle, rate, rates, rating, can, cane,
* canny, cant, cans, cat, cats, cattle, cattles.
*
* TRIE:
* ___________|____________
* | |
* r c
* a ________a___________
* t~ | |
* ____|_____ n~ t~
* | | | | _____|_____ _______|______
* s~ t e~ i | | | | | t
* l s~ n e~ n t~ s~ s~ l
* e~ g~ y~ e~
* s~
*
* Each ~ in the figure indicates where a prefix is a word.
*
* Generally, a trie has all the benefits of a hash table without
* any of the disadvantages.
*
* --------------------------------------------------------------
* | HASH TABLE | TRIE | explanations
* --------------------------------------------------------------
* Memory | O(n) | < O(n) | trie uses prefix compression.
* | | | Hence it does not store each
* | | | word explicitly
* ----------------------------------------------------------------
* Search | O(1) | O(1) | trie is technically faster.
* | | pseudo- | Given a word, computing a
* | | constant | hash takes at least as long
* | | | as traversing a trie. Plus,
* | | | trie has no collision.
* ----------------------------------------------------------------
*
* Tries are particularly superior to hash tables when it comes to solving
* problems such as word puzzles like boggle. In such puzzles the objective
* is to find how many words in a given list are valid. So if for example
* at a particular instance in boggle you have a list of one billion words
* all starting with zh-, whereas the dictionary has no words starting with
* zh-; then: if the dictionary is a hash table, you must compute the entire
* hashcode for each word and do one billion look-ups; if on the other hand
* the dictionary is a trie, you only do the equivalent of partially
* computing one hashcode! That's a saving of over one billion fold!
*
* This implementations of trie uses an array to store the children
* nodes, where the numerical value of each char serves as index.
**************************************************************************/
package algorithms.trie;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class Trie {
//the root only serves to anchor the trie.
private TrieNode root;
public Trie() {
root = new TrieNode('\0');
}//constructor
/*************************************************************************
* Function: getWordList
* @param word
* @return List<String>
*
* Description: Return a lexicographically ordered list of all the words
* on the trie.
*
* Technical Details: This is a recursive pre-order depth-first traversal
* algorithm, pre-order is to Trie what in-order is to BST. In addition,
* because each prefix assembled is an optimal substructure, this is
* also a greedy algorithm: where the words are assembled during
* traversal and as soon as a word is encountered, it is added to the
* list of results.
*
* 0] initialize an empty list to hold the words to be added.
* 1] for each child node of the root
* 2] call the recursive function getWordList to assemble
* all words stemming from the given prefix.
* 3] return the list
************************************************************************/
public List<String> getWordList() {
List<String> result = new ArrayList<String>();
for (TrieNode n : root.next) {
if (null != n) {
getWordList(result, n.value + "", n);
}
}
return result;
}//getWordList
/**
* Description: this function is the recursive portion of the
* overloaded function above.
*
* 0] if the given node n is a word, add the word to the result
* 1] if n is a prefix, for each of its children
* call this recursive function getWordList to assemble
* all words stemming from the given child.
*/
private void getWordList(List<String> result, String word, TrieNode n) {
if (n.word) {
result.add(word);
}
for (TrieNode t : n.next) {
if (null != t) {
getWordList(result, word + t.value, t);
}
}
}
}
package algorithms.graph;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.Test;
import static org.junit.Assert.*;
/***
* The dictionary is assembled from the works of poet
* Percy Bysshe Shelley (1792-1822)
***/
public class TrieTest {
/**
* Test of getWordList method, of class Trie.
*/
@Test
public void testGetWordList() {
System.out.println("getWordList");
Trie trie = new Trie();
List<String> dictionary = Arrays.asList("His", "soul", "had", "wedded", "Wisdom",
"and", "her", "dower", "is", "love", "justice", "clothed", "in",
"which", "he", "sate", "apart", "from", "men", "as", "a",
"lonely", "tower", "pitying", "the", "tumult", "of", "their",
"dark", "estate");
for (String w : dictionary) {
trie.addWord(w);
}
List result = trie.getWordList();
Collections.sort(dictionary);
//Collections.sort(result);// result is already sorted by default
assertEquals(dictionary, result);
}
}