All Packages Class Hierarchy This Package Previous Next Index
Class com.trumphurst.utils.PatriciaTree
java.lang.Object
|
+----java.util.Dictionary
|
+----com.trumphurst.utils.PatriciaTree
- public class PatriciaTree
- extends Dictionary
A Dictionary which stores String/Object pairs in a radix trie structure,
enabling them to be retrieved in sorted order. For details of the
Patricia algorithm, see "Algorithms" by Robert Sedgewick (Addison-Wesley).
Comparisons are made at the bit level, and only enough bits to uniquely
identify an individual key need to be compared to find it. This makes it
a very efficient way of storing items with large keys.
Note that the other Dictionaries in Java can use arbitrary Objects as
keys, because they do not sort in order, and thus only need hashCode and
isequal to compare keys, both of which exist for all objects. This class
needs to access arbitrary bits in the key to do its comparisons. As most
keys in real-life are Strings, PatriciaTree merely calls toString on its
key to convert it to a String (a no-effect operation if the key is already
a String), and uses the String as its real key. Unfortunately, this
precludes using (say) Integers directly as keys (they would be sorted in
alphabetical order, so that 100 is less than 2). It would be necessary to
create a new class whose toString method returned a String which would sort
in the correct order.
The alternative would be to create an interface containing the necessary
comparison functions, and force the key to implement the interface. However
this precludes using any existing class as the key, forcing even the
most common case (Strings as keys) to do extra work.
-
PatriciaTree()
-
-
elements()
- Returns an enumeration of the elements.
-
get(Object)
- Gets the object associated with the specified key in the PatriciaTree.
-
isEmpty()
- Returns true if the PatriciaTree contains no elements.
-
keys()
- Returns an enumeration of the PatriciaTree's keys.
-
print()
- Function to print the tree to System.out.
-
put(Object, Object)
- Puts the specified element into the PatriciaTree, using the specified
key.
-
remove(Object)
- This function is supposed to remove items from the tree, but it is not
implemented yet, and does nothing but return null.
-
size()
- Returns the number of elements contained within the PatriciaTree.
PatriciaTree
public PatriciaTree()
print
public void print()
- Function to print the tree to System.out. Not used in a production
environment, but helps to understand what is going on during
debugging of PatriciaTree itself.
elements
public Enumeration elements()
- Returns an enumeration of the elements. Use the Enumeration methods
on the returned object to fetch the elements sequentially in key
order.
- Overrides:
- elements in class Dictionary
- See Also:
- keys, Enumeration
get
public Object get(Object key)
- Gets the object associated with the specified key in the PatriciaTree.
- Parameters:
- key - the key in the hash table. Note that key.toString() is the
used as the actual key (as Strings can be ranked in order).
- Returns:
- s the element for the key, or null if the key
is not defined in the hash table.
- Overrides:
- get in class Dictionary
- See Also:
- put, toString, toString
isEmpty
public final boolean isEmpty()
- Returns true if the PatriciaTree contains no elements.
- Overrides:
- isEmpty in class Dictionary
keys
public Enumeration keys()
- Returns an enumeration of the PatriciaTree's keys. Use the Enumeration
methods on the returned object to fetch the keys sequentially in order.
- Overrides:
- keys in class Dictionary
- See Also:
- elements, Enumeration
put
public Object put(Object key,
Object value)
- Puts the specified element into the PatriciaTree, using the specified
key. The element may be retrieved by doing a get() with the same
key. The key and the element cannot be null.
- Parameters:
- key - the specified key. Note that key.toString() is the
used as the actual key (as Strings can be ranked in order).
- value - the specified element
- Returns:
- the old value of the key, or null if it did not have one.
- Throws: NullPointerException
- If the value of the specified
key or element is null.
- Overrides:
- put in class Dictionary
- See Also:
- get, toString, toString
remove
public Object remove(Object key)
- This function is supposed to remove items from the tree, but it is not
implemented yet, and does nothing but return null.
- Overrides:
- remove in class Dictionary
size
public final int size()
- Returns the number of elements contained within the PatriciaTree.
- Overrides:
- size in class Dictionary
All Packages Class Hierarchy This Package Previous Next Index