Package com.zybooks.dsaj.searchtree
Class TreeMap<K,V>
java.lang.Object
com.zybooks.dsaj.map.AbstractMap<K,V>
com.zybooks.dsaj.map.AbstractSortedMap<K,V>
com.zybooks.dsaj.searchtree.TreeMap<K,V>
- Type Parameters:
K
- The key type (keys must be unique and comparable)V
- The value type
- Direct Known Subclasses:
AVLTreeMap
,RBTreeMap
,SplayTreeMap
An implementation of a sorted map using a binary search tree.
-
Nested Class Summary
Modifier and TypeClassDescriptionprotected static class
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.Nested classes/interfaces inherited from class com.zybooks.dsaj.map.AbstractMap
AbstractMap.MapEntry<K,
V> -
Field Summary
Modifier and TypeFieldDescriptionprotected TreeMap.BalanceableBinaryTree<K,
V> Representation of the underlying tree structure. -
Constructor Summary
ConstructorDescriptionTreeMap()
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. -
Method Summary
Modifier and TypeMethodDescriptionceilingEntry
(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).entrySet()
Returns an iterable collection of all key-value entries of the map.Returns the entry having the least key (or null if map is empty).floorEntry
(K key) Returns the entry with greatest key less than or equal to given key (or null if no such key exists).Returns the value associated with the specified key, or null if no such entry exists.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
Returns the entry having the greatest key (or null if map is empty).lowerEntry
(K key) Returns the entry with greatest key strictly less than given key (or null if no such key exists).oneSidedAncestor
(Position<Entry<K, V>> p, boolean smaller) Returns entry of p's nearest ancestor with key that is smaller/larger than p.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.Removes the entry with the specified key, if present, and returns its associated value.restructure
(Position<Entry<K, V>> x) root()
protected void
protected void
int
size()
Returns the number of entries in the map.Returns an iterable containing all entries with keys in the range fromfromKey
inclusive totoKey
exclusive.Returns the position with the maximum key in the subtree rooted at p.Returns position with the minimal key in the subtree rooted at Position p.Methods inherited from class com.zybooks.dsaj.map.AbstractSortedMap
compare, compare, compare, compare
Methods inherited from class com.zybooks.dsaj.map.AbstractMap
isEmpty, keySet, values
-
Field Details
-
tree
Representation of the underlying tree structure.
-
-
Constructor Details
-
TreeMap
public TreeMap()Constructs an empty map using the natural ordering of keys. -
TreeMap
Constructs an empty map using the given comparator to order keys.- Parameters:
comp
- comparator defining the order of keys in the map
-
-
Method Details
-
size
public int size()Returns the number of entries in the map.- Returns:
- number of entries in the map
-
root
-
parent
-
left
-
right
-
sibling
-
isRoot
-
isExternal
-
isInternal
-
set
-
rotate
-
restructure
-
treeMin
Returns position with the minimal key in the subtree rooted at Position p.- Parameters:
p
- a Position of the tree serving as root of a subtree- Returns:
- Position with minimal key in subtree
-
treeMax
Returns the position with the maximum key in the subtree rooted at p.- Parameters:
p
- a Position of the tree serving as root of a subtree- Returns:
- Position with maximum key in subtree
-
oneSidedAncestor
Returns entry of p's nearest ancestor with key that is smaller/larger than p. Returns null if no such ancestor exists. -
get
Returns the value associated with the specified key, or null if no such entry exists.- Parameters:
key
- the key whose associated value is to be returned- Returns:
- the associated value, or null if no such entry exists
-
put
Associates the given value with the given key. If an entry with the key was already in the map, this replaces the previous value with the new one and returns the old value. Otherwise, a new entry is added and null is returned.- Parameters:
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified key- Returns:
- the previous value associated with the key (or null, if no such entry)
-
remove
Removes the entry with the specified key, if present, and returns its associated value. Otherwise does nothing and returns null.- Parameters:
key
- the key whose entry is to be removed from the map- Returns:
- the previous value associated with the removed key, or null if no such entry exists
-
firstEntry
Returns the entry having the least key (or null if map is empty).- Returns:
- entry with least key (or null if map is empty)
-
lastEntry
Returns the entry having the greatest key (or null if map is empty).- Returns:
- entry with greatest key (or null if map is empty)
-
ceilingEntry
Returns the entry with least key greater than or equal to given key (or null if no such key exists).- Parameters:
key
- the target key- Returns:
- entry with least key greater than or equal to given (or null if no such entry)
-
floorEntry
Returns the entry with greatest key less than or equal to given key (or null if no such key exists).- Parameters:
key
- the target key- Returns:
- entry with greatest key less than or equal to given (or null if no such entry)
-
lowerEntry
Returns the entry with greatest key strictly less than given key (or null if no such key exists).- Parameters:
key
- the target key- Returns:
- entry with greatest key strictly less than given (or null if no such entry)
-
higherEntry
Returns the entry with least key strictly greater than given key (or null if no such key exists).- Parameters:
key
- the target key- Returns:
- entry with least key strictly greater than given (or null if no such entry)
-
entrySet
Returns an iterable collection of all key-value entries of the map.- Returns:
- iterable collection of the map's entries
-
subMap
Returns an iterable containing all entries with keys in the range fromfromKey
inclusive totoKey
exclusive.- Parameters:
fromKey
- the lowest key allowed in the desired rangetoKey
- the highest key allowed in the desired range- Returns:
- iterable with keys in desired range
-
rebalanceInsert
Rebalances the tree after an insertion of specified position. This version of the method does not do anything, but it can be overridden by subclasses.- Parameters:
p
- the position which was recently inserted
-
rebalanceDelete
Rebalances the tree after a child of specified position has been removed. This version of the method does not do anything, but it can be overridden by subclasses.- Parameters:
p
- the position of the sibling of the removed leaf
-
rebalanceAccess
Rebalances the tree after an access of specified position. This version of the method does not do anything, but it can be overridden by a subclasses.- Parameters:
p
- the Position which was recently accessed (possibly a leaf)
-
dump
protected void dump()Prints textual representation of tree structure (for debug purpose only).
-