Class PatriciaTrie<K,V>
- All Implemented Interfaces:
Serializable,Map<K,,V> SortedMap<K,,V> Trie<K,V>
PATRICIA Trie
Practical Algorithm to Retrieve Information Coded in Alphanumeric
A PATRICIA Trie is a compressed Trie. Instead of storing
all data at the edges of the Trie (and having empty internal nodes),
PATRICIA stores data in every node. This allows for very efficient traversal,
insert, delete, predecessor, successor, prefix, range, and Trie.select(Object)
operations. All operations are performed at worst in O(K) time, where K
is the number of bits in the largest item in the tree. In practice,
operations actually take O(A(K)) time, where A(K) is the average number of
bits of all items in the tree.
Most importantly, PATRICIA requires very few comparisons to keys while doing any operation. While performing a lookup, each comparison (at most K of them, described above) will perform a single bit comparison against the given key, instead of comparing the entire key to another key.
The Trie can return operations in lexicographical order using the
Trie.traverse(Cursor), 'prefix', 'submap', or 'iterator' methods. The
Trie can also scan for items that are 'bitwise' (using an XOR
metric) by the 'select' method. Bitwise closeness is determined by the
KeyAnalyzer returning true or false for a bit being set or not in
a given key.
Any methods here that take an Object argument may throw a
ClassCastException if the method is expecting an instance of K
and it isn't K.
- Author:
- Roger Kapsi, Sam Berlin
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K extends Object,V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object, V extends Object> -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final KeyAnalyzer<? super K>TheKeyAnalyzerthat's being used to build the PATRICIATrie -
Constructor Summary
ConstructorsConstructorDescriptionPatriciaTrie(KeyAnalyzer<? super K> keyAnalyzer) PatriciaTrie(KeyAnalyzer<? super K> keyAnalyzer, Map<? extends K, ? extends V> m) -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Comparator<? super K>booleanentrySet()firstKey()keySet()lastKey()Returns a view of thisTrieof all elements that are prefixed by the given key.Returns theMap.Entrywhose key is closest in a bitwise XOR metric to the given key.Iterates through theTrie, starting with the entry whose bitwise value is closest in an XOR metric to the given key.Returns the key that is closest in a bitwise XOR metric to the provided key.selectValue(K key) Returns the value whose key is closest in a bitwise XOR metric to the provided key.intsize()toString()Traverses theTriein lexicographical order.values()Methods inherited from class java.util.AbstractMap
clone, containsValue, equals, hashCode, isEmpty, putAllMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, containsValue, equals, forEach, getOrDefault, hashCode, isEmpty, merge, putAll, putIfAbsent, remove, replace, replace, replaceAll
-
Field Details
-
keyAnalyzer
TheKeyAnalyzerthat's being used to build the PATRICIATrie
-
-
Constructor Details
-
PatriciaTrie
-
PatriciaTrie
-
-
Method Details
-
comparator
- Specified by:
comparatorin interfaceSortedMap<K,V>
-
prefixMap
Description copied from interface:TrieReturns a view of thisTrieof all elements that are prefixed by the given key.In a
Triewith fixed size keys, this is essentially aMap.get(Object)operation.For example, if the
Triecontains 'Anna', 'Anael', 'Analu', 'Andreas', 'Andrea', 'Andres', and 'Anatole', then a lookup of 'And' would return 'Andreas', 'Andrea', and 'Andres'. -
firstKey
-
lastKey
-
headMap
-
subMap
-
tailMap
-
clear
public void clear()- Specified by:
clearin interfaceMap<K,V> - Overrides:
clearin classAbstractMap<K,V>
-
size
public int size()- Specified by:
sizein interfaceMap<K,V> - Overrides:
sizein classAbstractMap<K,V>
-
put
- Specified by:
putin interfaceMap<K,V> - Overrides:
putin classAbstractMap<K,V>
-
get
- Specified by:
getin interfaceMap<K,V> - Overrides:
getin classAbstractMap<K,V>
-
select
Description copied from interface:TrieReturns theMap.Entrywhose key is closest in a bitwise XOR metric to the given key. This is NOT lexicographic closeness. For example, given the keys:- D = 1000100
- H = 1001000
- L = 1001100
Triecontained 'H' and 'L', a lookup of 'D' would return 'L', because the XOR distance between D & L is smaller than the XOR distance between D & H.- Returns:
- The
Map.Entrywhose key is closest in a bitwise XOR metric to the provided key.
-
select
Description copied from interface:TrieIterates through theTrie, starting with the entry whose bitwise value is closest in an XOR metric to the given key. After the closest entry is found, theTriewill call select on that entry and continue calling select for each entry (traversing in order of XOR closeness, NOT lexicographically) until the cursor returnsCursor.Decision.EXIT.The cursor can return
Cursor.Decision.CONTINUEto continue traversing.Cursor.Decision.REMOVE_AND_EXITis used to remove the current element and stop traversing.Note: The
Cursor.Decision.REMOVEoperation is not supported.- Returns:
- The entry the cursor returned
Cursor.Decision.EXITon, or null if it continued till the end.
-
traverse
Description copied from interface:TrieTraverses theTriein lexicographical order.Cursor.select(java.util.Map.Entry)will be called on each entry.The traversal will stop when the cursor returns
Cursor.Decision.EXIT,Cursor.Decision.CONTINUEis used to continue traversing andCursor.Decision.REMOVEis used to remove the element that was selected and continue traversing.Cursor.Decision.REMOVE_AND_EXITis used to remove the current element and stop traversing.- Returns:
- The entry the cursor returned
Cursor.Decision.EXITon, or null if it continued till the end.
-
containsKey
- Specified by:
containsKeyin interfaceMap<K,V> - Overrides:
containsKeyin classAbstractMap<K,V>
-
entrySet
-
keySet
-
values
-
remove
- Specified by:
removein interfaceMap<K,V> - Overrides:
removein classAbstractMap<K,V> - Throws:
ClassCastException- if provided key is of an incompatible type
-
selectKey
Description copied from interface:TrieReturns the key that is closest in a bitwise XOR metric to the provided key. This is NOT lexicographic closeness! For example, given the keys:- D = 1000100
- H = 1001000
- L = 1001100
Triecontained 'H' and 'L', a lookup of 'D' would return 'L', because the XOR distance between D & L is smaller than the XOR distance between D & H. -
selectValue
Description copied from interface:TrieReturns the value whose key is closest in a bitwise XOR metric to the provided key. This is NOT lexicographic closeness! For example, given the keys:- D = 1000100
- H = 1001000
- L = 1001100
Triecontained 'H' and 'L', a lookup of 'D' would return 'L', because the XOR distance between D & L is smaller than the XOR distance between D & H.- Specified by:
selectValuein interfaceTrie<K,V> - Returns:
- The value whose key is closest in a bitwise XOR metric to the provided key.
-
toString
- Overrides:
toStringin classAbstractMap<K,V>
-