public final class BinaryTree extends AbstractMap
This Map is intended for applications that need to be able to look up a key-value pairing by either key or value, and need to do so with equal efficiency.
While that goal could be accomplished by taking a pair of TreeMaps and redirecting requests to the appropriate TreeMap (e.g., containsKey would be directed to the TreeMap that maps values to keys, containsValue would be directed to the TreeMap that maps keys to values), there are problems with that implementation, particularly when trying to keep the two TreeMaps synchronized with each other. And if the data contained in the TreeMaps is large, the cost of redundant storage becomes significant.
This solution keeps the data properly synchronized and minimizes the data storage. The red-black algorithm is based on TreeMap's, but has been modified to simultaneously map a tree node by key and by value. This doubles the cost of put operations (but so does using two TreeMaps), and nearly doubles the cost of remove operations (there is a savings in that the lookup of the node to be removed only has to be performed once). And since only one node contains the key and value, storage is significantly less than that required by two TreeMaps.
There are some limitations placed on data kept in this Map. The biggest one is this:
When performing a put operation, neither the key nor the value may already exist in the Map. In the java.util Map implementations (HashMap, TreeMap), you can perform a put with an already mapped key, and neither cares about duplicate values at all ... but this implementation's put method with throw an IllegalArgumentException if either the key or the value is already in the Map.
Obviously, that same restriction (and consequence of failing to heed that restriction) applies to the putAll method.
The Map.Entry instances returned by the appropriate methods will not allow setValue() and will throw an UnsupportedOperationException on attempts to call that method.
New methods are added to take advantage of the fact that values are kept sorted independently of their keys:
Object getKeyForValue(Object value) is the opposite of get; it takes a value and returns its key, if any.
Object removeValue(Object value) finds and removes the specified value and returns the now un-used key.
Set entrySetByValue() returns the Map.Entry's in a Set whose iterator will iterate over the Map.Entry's in ascending order by their corresponding values.
Set keySetByValue() returns the keys in a Set whose iterator will iterate over the keys in ascending order by their corresponding values.
Collection valuesByValue() returns the values in a Collection whose iterator will iterate over the values in ascending order.
Modifier and Type | Class and Description |
---|---|
private class |
BinaryTree.BinaryTreeIterator |
private static class |
BinaryTree.Node |
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
Modifier and Type | Field and Description |
---|---|
private static String[] |
_data_name |
private Set[] |
_entry_set |
private static int |
_INDEX_COUNT |
private static int |
_INDEX_SUM |
private static int |
_KEY |
private Set[] |
_key_set |
private static int |
_MINIMUM_INDEX |
private int |
_modifications |
private BinaryTree.Node[] |
_root |
private int |
_size |
private static int |
_VALUE |
private Collection[] |
_value_collection |
Constructor and Description |
---|
BinaryTree()
Construct a new BinaryTree
|
BinaryTree(Map map)
Constructs a new BinaryTree from an existing Map, with keys and
values sorted
|
Modifier and Type | Method and Description |
---|---|
private static void |
checkKey(Object key)
check a key for validity (non-null and implements Comparable)
|
private static void |
checkKeyAndValue(Object key,
Object value)
check a key and a value for validity (non-null and implements
Comparable)
|
private static void |
checkNonNullComparable(Object o,
int index)
check if an object is fit to be proper input ...
|
private static void |
checkValue(Object value)
check a value for validity (non-null and implements Comparable)
|
void |
clear()
Removes all mappings from this map
|
private static int |
compare(Comparable o1,
Comparable o2)
Compare two objects
|
boolean |
containsKey(Object key)
Returns true if this map contains a mapping for the specified
key.
|
boolean |
containsValue(Object value)
Returns true if this map maps one or more keys to the
specified value.
|
private static void |
copyColor(BinaryTree.Node from,
BinaryTree.Node to,
int index)
copy the color from one node to another, dealing with the fact
that one or both nodes may, in fact, be null
|
private Object |
doGet(Comparable o,
int index)
common get logic, used to get by key or get by value
|
private void |
doRedBlackDelete(BinaryTree.Node deleted_node)
complicated red-black delete stuff.
|
private void |
doRedBlackDeleteFixup(BinaryTree.Node replacement_node,
int index)
complicated red-black delete stuff.
|
private void |
doRedBlackInsert(BinaryTree.Node inserted_node,
int index)
complicated red-black insert stuff.
|
private Object |
doRemove(Comparable o,
int index)
common remove logic (remove by key or remove by value)
|
Set |
entrySet()
Returns a set view of the mappings contained in this map.
|
Set |
entrySetByValue()
Returns a set view of the mappings contained in this map.
|
Object |
get(Object key)
Returns the value to which this map maps the specified
key.
|
private static BinaryTree.Node |
getGrandParent(BinaryTree.Node node,
int index)
get a node's grandparent.
|
Object |
getKeyForValue(Object value)
Returns the key to which this map maps the specified value.
|
private static BinaryTree.Node |
getLeftChild(BinaryTree.Node node,
int index)
get a node's left child.
|
private static BinaryTree.Node |
getParent(BinaryTree.Node node,
int index)
get a node's parent.
|
private static BinaryTree.Node |
getRightChild(BinaryTree.Node node,
int index)
get a node's right child.
|
private void |
grow()
bump up the size and note that the map has changed
|
private void |
insertValue(BinaryTree.Node newNode)
insert a node by its value
|
private static boolean |
isBlack(BinaryTree.Node node,
int index)
is the specified black red? if the node does not exist, sure,
it's black, thank you
|
private static boolean |
isLeftChild(BinaryTree.Node node,
int index)
is this node its parent's left child? mind you, the node, or
its parent, may not exist.
|
private static boolean |
isRed(BinaryTree.Node node,
int index)
is the specified node red? if the node does not exist, no, it's
black, thank you
|
private static boolean |
isRightChild(BinaryTree.Node node,
int index)
is this node its parent's right child? mind you, the node, or
its parent, may not exist.
|
Set |
keySet()
Returns a set view of the keys contained in this map.
|
Set |
keySetByValue()
Returns a set view of the keys contained in this map.
|
private static BinaryTree.Node |
leastNode(BinaryTree.Node node,
int index)
find the least node from a given node.
|
private BinaryTree.Node |
lookup(Comparable data,
int index)
do the actual lookup of a piece of data
|
private static void |
makeBlack(BinaryTree.Node node,
int index)
force a node (if it exists) black
|
private static void |
makeRed(BinaryTree.Node node,
int index)
force a node (if it exists) red
|
private void |
modify()
increment the modification count -- used to check for
concurrent modification of the map through the map and through
an Iterator from one of its Set or Collection views
|
private BinaryTree.Node |
nextGreater(BinaryTree.Node node,
int index)
get the next larger node from the specified node
|
private int |
oppositeIndex(int index)
Get the opposite index of the specified index
|
Object |
put(Object key,
Object value)
Associates the specified value with the specified key in this
map.
|
Object |
remove(Object key)
Removes the mapping for this key from this map if present
|
Object |
removeValue(Object value)
Removes the mapping for this value from this map if present
|
private void |
rotateLeft(BinaryTree.Node node,
int index)
do a rotate left.
|
private void |
rotateRight(BinaryTree.Node node,
int index)
do a rotate right.
|
private void |
shrink()
decrement the size and note that the map has changed
|
int |
size()
Returns the number of key-value mappings in this map.
|
private void |
swapPosition(BinaryTree.Node x,
BinaryTree.Node y,
int index)
swap two nodes (except for their content), taking care of
special cases where one is the other's parent ...
|
Collection |
values()
Returns a collection view of the values contained in this
map.
|
Collection |
valuesByValue()
Returns a collection view of the values contained in this
map.
|
private BinaryTree.Node[] _root
private int _size
private int _modifications
private Set[] _key_set
private Set[] _entry_set
private Collection[] _value_collection
private static final int _KEY
private static final int _VALUE
private static final int _INDEX_SUM
private static final int _MINIMUM_INDEX
private static final int _INDEX_COUNT
private static final String[] _data_name
public BinaryTree()
public BinaryTree(Map map) throws ClassCastException, NullPointerException, IllegalArgumentException
map
- the map whose mappings are to be placed in this map.ClassCastException
- if the keys in the map are not
Comparable, or are not mutually
comparable; also if the values in
the map are not Comparable, or
are not mutually ComparableNullPointerException
- if any key or value in the map
is nullIllegalArgumentException
- if there are duplicate keys
or duplicate values in the
mappublic Object getKeyForValue(Object value) throws ClassCastException, NullPointerException
value
- value whose associated key is to be returned.ClassCastException
- if the value is of an
inappropriate type for this map.NullPointerException
- if the value is nullpublic Object removeValue(Object value)
value
- value whose mapping is to be removed from the map.public Set entrySetByValue()
The difference between this method and entrySet is that entrySet's iterator() method returns an iterator that iterates over the mappings in ascending order by key. This method's iterator method iterates over the mappings in ascending order by value.
public Set keySetByValue()
The difference between this method and keySet is that keySet's iterator() method returns an iterator that iterates over the keys in ascending order by key. This method's iterator method iterates over the keys in ascending order by value.
public Collection valuesByValue()
The difference between this method and values is that values's iterator() method returns an iterator that iterates over the values in ascending order by key. This method's iterator method iterates over the values in ascending order by key.
private Object doRemove(Comparable o, int index)
o
- the key, or value, that we're looking forindex
- _KEY or _VALUEprivate Object doGet(Comparable o, int index)
o
- the key or value that we're looking forindex
- _KEY or _VALUEprivate int oppositeIndex(int index)
index
- _KEY or _VALUEprivate BinaryTree.Node lookup(Comparable data, int index)
data
- the key or value to be looked upindex
- _KEY or _VALUEprivate static int compare(Comparable o1, Comparable o2)
o1
- the first objecto2
- the second objectprivate static BinaryTree.Node leastNode(BinaryTree.Node node, int index)
node
- the node from which we will start searchingindex
- _KEY or _VALUEprivate BinaryTree.Node nextGreater(BinaryTree.Node node, int index)
node
- the node to be searched fromindex
- _KEY or _VALUEprivate static void copyColor(BinaryTree.Node from, BinaryTree.Node to, int index)
from
- the node whose color we're copying; may be nullto
- the node whose color we're changing; may be nullindex
- _KEY or _VALUEprivate static boolean isRed(BinaryTree.Node node, int index)
node
- the node (may be null) in questionindex
- _KEY or _VALUEprivate static boolean isBlack(BinaryTree.Node node, int index)
node
- the node (may be null) in questionindex
- _KEY or _VALUEprivate static void makeRed(BinaryTree.Node node, int index)
node
- the node (may be null) in questionindex
- _KEY or _VALUEprivate static void makeBlack(BinaryTree.Node node, int index)
node
- the node (may be null) in questionindex
- _KEY or _VALUEprivate static BinaryTree.Node getGrandParent(BinaryTree.Node node, int index)
node
- the node (may be null) in questionindex
- _KEY or _VALUEprivate static BinaryTree.Node getParent(BinaryTree.Node node, int index)
node
- the node (may be null) in questionindex
- _KEY or _VALUEprivate static BinaryTree.Node getRightChild(BinaryTree.Node node, int index)
node
- the node (may be null) in questionindex
- _KEY or _VALUEprivate static BinaryTree.Node getLeftChild(BinaryTree.Node node, int index)
node
- the node (may be null) in questionindex
- _KEY or _VALUEprivate static boolean isLeftChild(BinaryTree.Node node, int index)
node
- the node (may be null) in questionindex
- _KEY or _VALUEprivate static boolean isRightChild(BinaryTree.Node node, int index)
node
- the node (may be null) in questionindex
- _KEY or _VALUEprivate void rotateLeft(BinaryTree.Node node, int index)
node
- the node to be rotatedindex
- _KEY or _VALUEprivate void rotateRight(BinaryTree.Node node, int index)
node
- the node to be rotatedindex
- _KEY or _VALUEprivate void doRedBlackInsert(BinaryTree.Node inserted_node, int index)
inserted_node
- the node to be insertedindex
- _KEY or _VALUEprivate void doRedBlackDelete(BinaryTree.Node deleted_node)
deleted_node
- the node to be deletedprivate void doRedBlackDeleteFixup(BinaryTree.Node replacement_node, int index)
replacement_node
- the node being replacedindex
- _KEY or _VALUEprivate void swapPosition(BinaryTree.Node x, BinaryTree.Node y, int index)
x
- one nodey
- another nodeindex
- _KEY or _VALUEprivate static void checkNonNullComparable(Object o, int index)
o
- the object being checkedindex
- _KEY or _VALUE (used to put the right word in the
exception message)NullPointerException
- if o is nullClassCastException
- if o is not Comparableprivate static void checkKey(Object key)
key
- the key to be checkedNullPointerException
- if key is nullClassCastException
- if key is not Comparableprivate static void checkValue(Object value)
value
- the value to be checkedNullPointerException
- if value is nullClassCastException
- if value is not Comparableprivate static void checkKeyAndValue(Object key, Object value)
key
- the key to be checkedvalue
- the value to be checkedNullPointerException
- if key or value is nullClassCastException
- if key or value is not Comparableprivate void modify()
private void grow()
private void shrink()
private void insertValue(BinaryTree.Node newNode) throws IllegalArgumentException
newNode
- the node to be insertedIllegalArgumentException
- if the node already exists
in the value mappingpublic int size()
size
in interface Map
size
in class AbstractMap
public boolean containsKey(Object key) throws ClassCastException, NullPointerException
containsKey
in interface Map
containsKey
in class AbstractMap
key
- key whose presence in this map is to be tested.ClassCastException
- if the key is of an inappropriate
type for this map.NullPointerException
- if the key is nullpublic boolean containsValue(Object value)
containsValue
in interface Map
containsValue
in class AbstractMap
value
- value whose presence in this map is to be tested.public Object get(Object key) throws ClassCastException, NullPointerException
get
in interface Map
get
in class AbstractMap
key
- key whose associated value is to be returned.ClassCastException
- if the key is of an inappropriate
type for this map.NullPointerException
- if the key is nullpublic Object put(Object key, Object value) throws ClassCastException, NullPointerException, IllegalArgumentException
put
in interface Map
put
in class AbstractMap
key
- key with which the specified value is to be
associated.value
- value to be associated with the specified key.ClassCastException
- if the class of the specified key
or value prevents it from being
stored in this map.NullPointerException
- if the specified key or value
is nullIllegalArgumentException
- if the key duplicates an
existing key, or if the
value duplicates an
existing valuepublic Object remove(Object key)
remove
in interface Map
remove
in class AbstractMap
key
- key whose mapping is to be removed from the map.public void clear()
clear
in interface Map
clear
in class AbstractMap
public Set keySet()
keySet
in interface Map
keySet
in class AbstractMap
public Collection values()
values
in interface Map
values
in class AbstractMap
public Set entrySet()
entrySet
in interface Map
entrySet
in class AbstractMap
Copyright © 2016 Open Microscopy Environment