public class TreeMap<K,V> extends AbstractSortedMap<K,V>
Modifier and Type | Class and Description |
---|---|
protected static class |
TreeMap.BalanceableBinaryTree<K,V>
A specialized version of the LinkedBinaryTree class with
additional mutators to support binary search tree operations, and
a specialized node class that includes an auxiliary instance
variable for balancing data.
|
AbstractMap.MapEntry<K,V>
Modifier and Type | Field and Description |
---|---|
protected TreeMap.BalanceableBinaryTree<K,V> |
tree
Representation of the underlying tree structure.
|
Constructor and Description |
---|
TreeMap()
Constructs an empty map using the natural ordering of keys.
|
TreeMap(Comparator<K> comp)
Constructs an empty map using the given comparator to order keys.
|
Modifier and Type | Method and Description |
---|---|
Entry<K,V> |
ceilingEntry(K key)
Returns the entry with least key greater than or equal to given key
(or null if no such key exists).
|
protected void |
dump()
Prints textual representation of tree structure (for debug purpose only).
|
Iterable<Entry<K,V>> |
entrySet()
Returns an iterable collection of all key-value entries of the map.
|
Entry<K,V> |
firstEntry()
Returns the entry having the least key (or null if map is empty).
|
Entry<K,V> |
floorEntry(K key)
Returns the entry with greatest key less than or equal to given key
(or null if no such key exists).
|
V |
get(K key)
Returns the value associated with the specified key, or null if no such entry exists.
|
Entry<K,V> |
higherEntry(K key)
Returns the entry with least key strictly greater than given key
(or null if no such key exists).
|
protected boolean |
isExternal(Position<Entry<K,V>> p) |
protected boolean |
isInternal(Position<Entry<K,V>> p) |
protected boolean |
isRoot(Position<Entry<K,V>> p) |
Entry<K,V> |
lastEntry()
Returns the entry having the greatest key (or null if map is empty).
|
protected Position<Entry<K,V>> |
left(Position<Entry<K,V>> p) |
Entry<K,V> |
lowerEntry(K key)
Returns the entry with greatest key strictly less than given key
(or null if no such key exists).
|
protected Position<Entry<K,V>> |
parent(Position<Entry<K,V>> p) |
V |
put(K key,
V value)
Associates the given value with the given key.
|
protected void |
rebalanceAccess(Position<Entry<K,V>> p)
Rebalances the tree after an access of specified position.
|
protected void |
rebalanceDelete(Position<Entry<K,V>> p)
Rebalances the tree after a child of specified position has been
removed.
|
protected void |
rebalanceInsert(Position<Entry<K,V>> p)
Rebalances the tree after an insertion of specified position.
|
V |
remove(K key)
Removes the entry with the specified key, if present, and returns
its associated value.
|
protected Entry<K,V> |
remove(Position<Entry<K,V>> p) |
protected Position<Entry<K,V>> |
restructure(Position<Entry<K,V>> x) |
protected Position<Entry<K,V>> |
right(Position<Entry<K,V>> p) |
protected Position<Entry<K,V>> |
root() |
protected void |
rotate(Position<Entry<K,V>> p) |
protected void |
set(Position<Entry<K,V>> p,
Entry<K,V> e) |
protected Position<Entry<K,V>> |
sibling(Position<Entry<K,V>> p) |
int |
size()
Returns the number of entries in the map.
|
Iterable<Entry<K,V>> |
subMap(K fromKey,
K toKey)
Returns an iterable containing all entries with keys in the range from
fromKey inclusive to toKey exclusive. |
protected Position<Entry<K,V>> |
treeMax(Position<Entry<K,V>> p)
Returns the position with the maximum key in the subtree rooted at p.
|
protected Position<Entry<K,V>> |
treeMin(Position<Entry<K,V>> p)
Returns position with the minimal key in the subtree rooted at Position p.
|
checkKey, compare, compare, compare, compare
isEmpty, keySet, values
protected TreeMap.BalanceableBinaryTree<K,V> tree
public TreeMap()
public TreeMap(Comparator<K> comp)
comp
- comparator defining the order of keys in the mappublic int size()
protected Position<Entry<K,V>> treeMin(Position<Entry<K,V>> p)
p
- a Position of the tree serving as root of a subtreeprotected Position<Entry<K,V>> treeMax(Position<Entry<K,V>> p)
p
- a Position of the tree serving as root of a subtreepublic V get(K key) throws IllegalArgumentException
key
- the key whose associated value is to be returnedIllegalArgumentException
public V put(K key, V value) throws IllegalArgumentException
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified keyIllegalArgumentException
public V remove(K key) throws IllegalArgumentException
key
- the key whose entry is to be removed from the mapIllegalArgumentException
public Entry<K,V> firstEntry()
public Entry<K,V> lastEntry()
public Entry<K,V> ceilingEntry(K key) throws IllegalArgumentException
IllegalArgumentException
- if the key is not compatible with the mappublic Entry<K,V> floorEntry(K key) throws IllegalArgumentException
IllegalArgumentException
- if the key is not compatible with the mappublic Entry<K,V> lowerEntry(K key) throws IllegalArgumentException
IllegalArgumentException
- if the key is not compatible with the mappublic Entry<K,V> higherEntry(K key) throws IllegalArgumentException
IllegalArgumentException
- if the key is not compatible with the mappublic Iterable<Entry<K,V>> entrySet()
public Iterable<Entry<K,V>> subMap(K fromKey, K toKey) throws IllegalArgumentException
fromKey
inclusive to toKey
exclusive.IllegalArgumentException
- if fromKey
or toKey
is not compatible with the mapprotected void rebalanceInsert(Position<Entry<K,V>> p)
p
- the position which was recently insertedprotected void rebalanceDelete(Position<Entry<K,V>> p)
p
- the position of the sibling of the removed leafprotected void rebalanceAccess(Position<Entry<K,V>> p)
p
- the Position which was recently accessed (possibly a leaf)protected void dump()