edu.vt.middleware.dictionary
Class TernaryTree

java.lang.Object
  extended by edu.vt.middleware.dictionary.TernaryTree

public class TernaryTree
extends java.lang.Object

TernaryTree is an implementation of a ternary tree. Methods are provided for inserting strings and searching for strings. The algorithms in this class are all recursive, and have not been optimized for any particular purpose. Data which is inserted is not sorted before insertion, however data can be inserted beginning with the median of the supplied data.

Version:
$Revision: 1700 $ $Date: 2010-10-27 16:08:24 -0400 (Wed, 27 Oct 2010) $
Author:
Middleware Services

Field Summary
protected static java.util.Comparator<java.lang.Character> CASE_INSENSITIVE_COMPARATOR
          Case insensitive comparator.
protected static java.util.Comparator<java.lang.Character> CASE_SENSITIVE_COMPARATOR
          Case sensitive comparator.
protected  java.util.Comparator<java.lang.Character> comparator
          Character comparator.
 
Constructor Summary
TernaryTree()
          Creates an empty case sensitive ternary tree.
TernaryTree(boolean caseSensitive)
          Creates an empty ternary tree with the given case sensitivity.
 
Method Summary
 java.util.List<java.lang.String> getWords()
          This will return a list of all the words in this TernaryTree.
 void insert(java.lang.String word)
          This will insert the supplied word into the TernaryTree.
 void insert(java.lang.String[] words)
          This will insert the supplied array of words into the TernaryTree.
 java.lang.String[] nearSearch(java.lang.String word, int distance)
          This will return an array of strings which are near to the supplied word by the supplied distance.
 java.lang.String[] partialSearch(java.lang.String word)
          This will return an array of strings which partially match the supplied word.
 void print(java.io.Writer out)
          This will print an ASCII representation of this TernaryTree to the supplied PrintWriter.
 boolean search(java.lang.String word)
          This will return true if the supplied word has been inserted into the TernaryTree.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CASE_SENSITIVE_COMPARATOR

protected static final java.util.Comparator<java.lang.Character> CASE_SENSITIVE_COMPARATOR
Case sensitive comparator.


CASE_INSENSITIVE_COMPARATOR

protected static final java.util.Comparator<java.lang.Character> CASE_INSENSITIVE_COMPARATOR
Case insensitive comparator.


comparator

protected java.util.Comparator<java.lang.Character> comparator
Character comparator.

Constructor Detail

TernaryTree

public TernaryTree()
Creates an empty case sensitive ternary tree.


TernaryTree

public TernaryTree(boolean caseSensitive)
Creates an empty ternary tree with the given case sensitivity.

Parameters:
caseSensitive - True to create case-sensitive tree, false otherwise.
Method Detail

insert

public void insert(java.lang.String word)
This will insert the supplied word into the TernaryTree.

Parameters:
word - String to insert

insert

public void insert(java.lang.String[] words)
This will insert the supplied array of words into the TernaryTree.

Parameters:
words - String[] to insert

search

public boolean search(java.lang.String word)
This will return true if the supplied word has been inserted into the TernaryTree.

Parameters:
word - String to search for
Returns:
boolean - whether word was found

partialSearch

public java.lang.String[] partialSearch(java.lang.String word)
This will return an array of strings which partially match the supplied word. word should be of the format '.e.e.e' Where the '.' character represents any valid character. Possible results from this query include: Helene, delete, or severe Note that no substring matching occurs, results only include strings of the same length. If the supplied word does not contain the '.' character, then a regular search is performed.

NOTE This method is not supported for case insensitive ternary trees. Since the tree is built without regard to case any words returned from the tree may or may not match the case of the supplied word.

Parameters:
word - String to search for
Returns:
String[] - of matching words
Throws:
java.lang.UnsupportedOperationException - if this is a case insensitive ternary tree

nearSearch

public java.lang.String[] nearSearch(java.lang.String word,
                                     int distance)
This will return an array of strings which are near to the supplied word by the supplied distance. For the query nearSearch("fisher", 2): Possible results include: cipher, either, fishery, kosher, sister. If the supplied distance is not > 0, then a regular search is performed.

NOTE This method is not supported for case insensitive ternary trees. Since the tree is built without regard to case any words returned from the tree may or may not match the case of the supplied word.

Parameters:
word - String to search for
distance - int for valid match
Returns:
String[] - of matching words
Throws:
java.lang.UnsupportedOperationException - if this is a case insensitive ternary tree

getWords

public java.util.List<java.lang.String> getWords()
This will return a list of all the words in this TernaryTree. This is a very expensive operation, every node in the tree is traversed. The returned list cannot be modified.

Returns:
String[] - of words

print

public void print(java.io.Writer out)
           throws java.io.IOException
This will print an ASCII representation of this TernaryTree to the supplied PrintWriter. This is a very expensive operation, every node in the tree is traversed. The output produced is hard to read, but it should give an indication of whether or not your tree is balanced.

Parameters:
out - PrintWriter to print to
Throws:
java.io.IOException - if an error occurs


Copyright © 2003-2010 Virginia Tech. All Rights Reserved.