Skip to content

Instantly share code, notes, and snippets.

@okoye
Created May 28, 2010 09:31
Show Gist options
  • Save okoye/416964 to your computer and use it in GitHub Desktop.
Save okoye/416964 to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
/**
* @author Chuka Okoye
*
*/
public class TrieStructure
{
private TrieNode rootNode;
/**
* Simple constructor to initialize our Trie Data Structure with an empty element
* @param nothing
* */
public TrieStructure()
{
//Should only create the empty root node
rootNode = new TrieNode('.',false);
}
/**
* Function responsible for inserting new words into a Trie Data Struture
* @param word, a word to be inserted into Trie Data Structure
* @param occurIndex, the index in the document the word occurs
* @return true if insert successful
*/
public boolean putWord(String word, int occurIndex)
{
if(checkValidity(word))
{
char[] charArray = word.toLowerCase().toCharArray();
ArrayList<TrieNode> tempNodeList = rootNode.childNodes;
boolean inserted;
for(int i=0; i<charArray.length; i++)
{
inserted = false;
while(inserted == false)
{
if(rootNode.childNodes.isEmpty()) //test for first insertion
{
inserted = true;
TrieNode temp;
if(charArray.length == 1) //test for endWord
{
temp = new TrieNode(charArray[0],true);
// System.out.println("ROOTSPECIAL: IF: "+temp.character);
}
else
{
temp = new TrieNode(charArray[0], false);
// System.out.println("ROOTSPECIAL: ELSE: "+temp.character);
}
rootNode.childNodes.add(temp);
tempNodeList = rootNode.childNodes.get(0).childNodes;
}
else
{
int index = getIndexChar(charArray[i],tempNodeList);
inserted = true;
//Two cases, either current char exists already or does not.
if(index != -1)
{
if((charArray.length-1) == i) //test for endWord [REFACTOR]
{
TrieNode pointer = tempNodeList.get(index);
// System.out.println("NORMAL: IF: EXISTS");
pointer.endWord = true;
}
tempNodeList.get(index).index.add(occurIndex);
tempNodeList = tempNodeList.get(index).childNodes;
}
else
{
if((charArray.length-1) == i) //test for endWord [REFACTOR]
{
TrieNode aNode = new TrieNode(charArray[i],true);
aNode.index.add(occurIndex);
tempNodeList.add(aNode);
tempNodeList = tempNodeList.get(tempNodeList.indexOf(aNode)).childNodes;
// System.out.println("NORMAL: ELSE:IF: "+aNode.character);
}
else
{
TrieNode aNode = new TrieNode(charArray[i], false);
aNode.index.add(occurIndex);
// System.out.println("NORMAL: ELSE:ELSE: "+aNode.character);
tempNodeList.add(aNode);
tempNodeList = tempNodeList.get(tempNodeList.indexOf(aNode)).childNodes;
}
}
}
}
}
return true;
}
return false;
}
/**
* A function to traverse the Trie tree and retrieve all indexes a word occurs in a document.
* @param word, the word whose indexes in document should be returned
* @return ArrayList<Integer>, a list containing all indexes of specified word
*/
public ArrayList<Integer> getWordIndexes(String word)
{
if(word.equals(""))
return new ArrayList<Integer>();
ArrayList<TrieNode> tempNodeList = rootNode.childNodes;
char[] charArray = word.toLowerCase().toCharArray();
int index = -1;
for(int i=0; i<charArray.length; i++)
{
index = getIndexChar(charArray[i], tempNodeList);
if(index != -1)
{
if(i != (charArray.length-1))
tempNodeList = tempNodeList.get(index).childNodes;
}
else
return new ArrayList<Integer>();
}
return tempNodeList.get(index).index;
}
/**
* A function to search through a list of nodes to find a corresponding character
* @param value, value to be checked
* @param nodes, a list whose characters will be checked against
* @return the index of the node containing such character. -1 if not found
*/
private int getIndexChar(char value, ArrayList<TrieNode> nodes)
{
for(int i=0; i<nodes.size(); i++)
{
// System.out.println("Comparing "+value+" "+nodes.get(i).character);
if(nodes.get(i).character == value)
return i;
}
// System.out.println("Returning -1 for "+value);
return -1;
}
/**
* Makes sure only the letters a-z, A-Z and 0-9 exist in the string
* TODO Implement
* @param value, string to be verified
* @return true if it is a valid string
*/
private boolean checkValidity(String value)
{
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment