– there Do, or do not is no trie With apologies to George

advertisement
Do, or do not – there
is no trie
With apologies to George
Lucas, and more importantly
to Yoda.
What is a trie?
Trie is a recursive structure.
 Each node in the trie will refer to another
trie.

Trie class

Has an instance variable for an array that
will hold elements of type Trie
 private

Trie[] _trieNodes;
boolean variable to signify whether or not
the particular trie/node (remember that
each node IS a trie) is a valid end of word.
 Initialized
to false at first.
Trie Class Methods
Helper method to convert a character to
an int index.
 While not strictly required, will be
beneficial.
 This method should map a given character
to its appropriate array index. A=0, B=1,
etc.

Method to add a word to the trie

Every word in the dictionary file must be added
to the trie.
 void

Throughout the method, a local variable of type
Trie is recommended to keep track of where you
are in the overall Trie. Remember that each part
of the subtree(ie) is a trie itself.
 Trie

addWord(String word)
workingTrie;
Start at the root of the trie - store it in
workingTrie.
Method to add a word to the trie

In a loop:
 Parse
string character by character and convert
character to index
 If element at index is null,

Create a new Trie and store it at index
workingTrie to the trie at the character index of
the current level
 Set

workingTrie = workingTrie._trieNodes[index];
 Once
the end of the word is reached, set the boolean
to true since this node represents a valid/actual word.
Method to add a word to the trie
Note that the actual character is never
explicitly stored in the trie.
 If desired, you could store the character,
however it is not necessary. If a given
index is null, that particular character is not
in the current level of the trie. If the index
contains an element (a trie) than it is in the
current level.

Method to test whether a given string is
a valid word found in the trie

boolean isWord(String word)
 Same
basic structure of the addWord(..) method
 Parse string character by character
 If a given index is not found, word is not valid
 If the string is fully parsed return the value of the
boolean of that node.

The boolean will be false if it not a valid end of word, and true
if it is a valid word.
Download