Package adams.core
Class Trie
- java.lang.Object
-
- adams.core.Trie
-
- All Implemented Interfaces:
Serializable,Cloneable,Iterable<String>,Collection<String>
public class Trie extends Object implements Serializable, Cloneable, Collection<String>
A class representing a Trie data structure for strings. See also Trie on WikiPedia.- Author:
- fracpete (fracpete at waikato dot ac dot nz)
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classTrie.TrieIteratorRepresents an iterator over a triestatic classTrie.TrieNodeRepresents a node in the trie.
-
Field Summary
Fields Modifier and Type Field Description protected intm_HashCodethe hash codeprotected booleanm_RecalcHashCodewhether the structure got modified and the hash code needs to be re-calculatedprotected Trie.TrieNodem_Rootthe root node
-
Constructor Summary
Constructors Constructor Description Trie()initializes the data structure
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanadd(String o)Ensures that this collection contains the specified element.booleanaddAll(Collection<? extends String> c)Adds all of the elements in the specified collection to this collectionvoidclear()Removes all of the elements from this collectionObjectclone()returns a deep copy of itselfbooleancontains(Object o)Returns true if this collection contains the specified element.booleancontainsAll(Collection<?> c)Returns true if this collection contains all of the elements in the specified collection.booleancontainsPrefix(String prefix)checks whether the given prefix is stored in the triebooleanequals(Object o)Compares the specified object with this collection for equality.StringgetCommonPrefix()returns the common prefix for all the nodesTrie.TrieNodegetRoot()returns the root node of the trieList<String>getWithPrefix(String prefix)returns all stored strings that match the given prefixinthashCode()Returns the hash code value for this collection.booleanisEmpty()Returns true if this collection contains no elements.Iterator<String>iterator()Returns an iterator over the elements in this collection.static voidmain(String[] args)Only for testing (prints the built Trie).booleanremove(Object o)Removes a single instance of the specified element from this collection, if it is present.booleanremoveAll(Collection<?> c)Removes all this collection's elements that are also contained in the specified collectionbooleanretainAll(Collection<?> c)Retains only the elements in this collection that are contained in the specified collectionintsize()Returns the number of elements in this collection.Object[]toArray()Returns an array containing all of the elements in this collection.<T> T[]toArray(T[] a)Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array.StringtoString()returns the trie in string representationprotected StringtoString(Trie.TrieNode node)returns the node as String-
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Collection
parallelStream, removeIf, spliterator, stream, toArray
-
-
-
-
Field Detail
-
m_Root
protected Trie.TrieNode m_Root
the root node
-
m_HashCode
protected int m_HashCode
the hash code
-
m_RecalcHashCode
protected boolean m_RecalcHashCode
whether the structure got modified and the hash code needs to be re-calculated
-
-
Method Detail
-
add
public boolean add(String o)
Ensures that this collection contains the specified element.- Specified by:
addin interfaceCollection<String>- Parameters:
o- the string to add- Returns:
- true if the structure changed
-
addAll
public boolean addAll(Collection<? extends String> c)
Adds all of the elements in the specified collection to this collection- Specified by:
addAllin interfaceCollection<String>- Parameters:
c- the collection to add
-
clear
public void clear()
Removes all of the elements from this collection- Specified by:
clearin interfaceCollection<String>
-
clone
public Object clone()
returns a deep copy of itself
-
contains
public boolean contains(Object o)
Returns true if this collection contains the specified element.- Specified by:
containsin interfaceCollection<String>- Parameters:
o- the object to check for in trie- Returns:
- true if found
-
containsAll
public boolean containsAll(Collection<?> c)
Returns true if this collection contains all of the elements in the specified collection.- Specified by:
containsAllin interfaceCollection<String>- Parameters:
c- the collection to look for in the trie- Returns:
- true if all elements were found
-
containsPrefix
public boolean containsPrefix(String prefix)
checks whether the given prefix is stored in the trie- Parameters:
prefix- the prefix to check- Returns:
- true if the prefix is part of the trie
-
equals
public boolean equals(Object o)
Compares the specified object with this collection for equality.- Specified by:
equalsin interfaceCollection<String>- Overrides:
equalsin classObject- Parameters:
o- the object to check for equality
-
getCommonPrefix
public String getCommonPrefix()
returns the common prefix for all the nodes- Returns:
- the result of the search
-
getRoot
public Trie.TrieNode getRoot()
returns the root node of the trie- Returns:
- the root node
-
getWithPrefix
public List<String> getWithPrefix(String prefix)
returns all stored strings that match the given prefix- Parameters:
prefix- the prefix that all strings must have- Returns:
- all strings that match the prefix
-
hashCode
public int hashCode()
Returns the hash code value for this collection.- Specified by:
hashCodein interfaceCollection<String>- Overrides:
hashCodein classObject- Returns:
- the hash code
-
isEmpty
public boolean isEmpty()
Returns true if this collection contains no elements.- Specified by:
isEmptyin interfaceCollection<String>- Returns:
- true if empty
-
iterator
public Iterator<String> iterator()
Returns an iterator over the elements in this collection.
-
remove
public boolean remove(Object o)
Removes a single instance of the specified element from this collection, if it is present.- Specified by:
removein interfaceCollection<String>- Parameters:
o- the object to remove- Returns:
- true if this collection changed as a result of the call
-
removeAll
public boolean removeAll(Collection<?> c)
Removes all this collection's elements that are also contained in the specified collection- Specified by:
removeAllin interfaceCollection<String>- Parameters:
c- the collection to remove- Returns:
- true if the collection changed
-
retainAll
public boolean retainAll(Collection<?> c)
Retains only the elements in this collection that are contained in the specified collection- Specified by:
retainAllin interfaceCollection<String>- Parameters:
c- the collection to use as reference- Returns:
- true if this collection changed as a result of the call
-
size
public int size()
Returns the number of elements in this collection.- Specified by:
sizein interfaceCollection<String>- Returns:
- the number of nodes in the tree
-
toArray
public Object[] toArray()
Returns an array containing all of the elements in this collection.- Specified by:
toArrayin interfaceCollection<String>- Returns:
- the stored strings as array
-
toArray
public <T> T[] toArray(T[] a)
Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array.- Specified by:
toArrayin interfaceCollection<String>- Parameters:
a- the array into which the elements of this collection are to be stored- Returns:
- an array containing the elements of this collection
-
toString
protected String toString(Trie.TrieNode node)
returns the node as String- Parameters:
node- the node to turn into a string- Returns:
- the node as string
-
toString
public String toString()
returns the trie in string representation
-
main
public static void main(String[] args)
Only for testing (prints the built Trie). Arguments are added to the Trie. If not arguments provided then a few default strings are uses for building.- Parameters:
args- commandline arguments
-
-