90 |
* @since 1.2 |
* @since 1.2 |
91 |
* @status updated to 1.4 |
* @status updated to 1.4 |
92 |
*/ |
*/ |
93 |
public class TreeMap extends AbstractMap |
public class TreeMap<K, V> extends AbstractMap<K, V> |
94 |
implements SortedMap, Cloneable, Serializable |
implements SortedMap<K, V>, Cloneable, Serializable |
95 |
{ |
{ |
96 |
// Implementation note: |
// Implementation note: |
97 |
// A red-black tree is a binary search tree with the additional properties |
// A red-black tree is a binary search tree with the additional properties |
162 |
* |
* |
163 |
* @author Eric Blake <ebb9@email.byu.edu> |
* @author Eric Blake <ebb9@email.byu.edu> |
164 |
*/ |
*/ |
165 |
private static final class Node extends AbstractMap.BasicMapEntry |
private static final class Node<K, V> extends AbstractMap.BasicMapEntry<K, V> |
166 |
{ |
{ |
167 |
// All fields package visible for use by nested classes. |
// All fields package visible for use by nested classes. |
168 |
/** The color of this node. */ |
/** The color of this node. */ |
169 |
int color; |
int color; |
170 |
|
|
171 |
/** The left child node. */ |
/** The left child node. */ |
172 |
Node left = nil; |
Node<K, V> left = nil; |
173 |
/** The right child node. */ |
/** The right child node. */ |
174 |
Node right = nil; |
Node<K, V> right = nil; |
175 |
/** The parent node. */ |
/** The parent node. */ |
176 |
Node parent = nil; |
Node<K, V> parent = nil; |
177 |
|
|
178 |
/** |
/** |
179 |
* Simple constructor. |
* Simple constructor. |
180 |
* @param key the key |
* @param key the key |
181 |
* @param value the value |
* @param value the value |
182 |
*/ |
*/ |
183 |
Node(Object key, Object value, int color) |
Node(K key, V value, int color) |
184 |
{ |
{ |
185 |
super(key, value); |
super(key, value); |
186 |
this.color = color; |
this.color = color; |
417 |
* @return the first key |
* @return the first key |
418 |
* @throws NoSuchElementException if the map is empty |
* @throws NoSuchElementException if the map is empty |
419 |
*/ |
*/ |
420 |
public Object firstKey() |
public K firstKey() |
421 |
{ |
{ |
422 |
if (root == nil) |
if (root == nil) |
423 |
throw new NoSuchElementException(); |
throw new NoSuchElementException(); |
438 |
* @see #put(Object, Object) |
* @see #put(Object, Object) |
439 |
* @see #containsKey(Object) |
* @see #containsKey(Object) |
440 |
*/ |
*/ |
441 |
public Object get(Object key) |
public V get(Object key) |
442 |
{ |
{ |
443 |
// Exploit fact that nil.value == null. |
// Exploit fact that nil.value == null. |
444 |
return getNode(key).value; |
return getNode(key).value; |
459 |
* @throws NullPointerException if toKey is null, but the comparator does not |
* @throws NullPointerException if toKey is null, but the comparator does not |
460 |
* tolerate null elements |
* tolerate null elements |
461 |
*/ |
*/ |
462 |
public SortedMap headMap(Object toKey) |
public SortedMap<K, V> headMap(K toKey) |
463 |
{ |
{ |
464 |
return new SubMap(nil, toKey); |
return new SubMap(nil, toKey); |
465 |
} |
} |
518 |
* @return the last key |
* @return the last key |
519 |
* @throws NoSuchElementException if the map is empty |
* @throws NoSuchElementException if the map is empty |
520 |
*/ |
*/ |
521 |
public Object lastKey() |
public K lastKey() |
522 |
{ |
{ |
523 |
if (root == nil) |
if (root == nil) |
524 |
throw new NoSuchElementException("empty"); |
throw new NoSuchElementException("empty"); |
618 |
* @throws NullPointerException if key is null, but the comparator does |
* @throws NullPointerException if key is null, but the comparator does |
619 |
* not tolerate nulls |
* not tolerate nulls |
620 |
*/ |
*/ |
621 |
public Object remove(Object key) |
public V remove(K key) |
622 |
{ |
{ |
623 |
Node n = getNode(key); |
Node<K, V> n = getNode(key); |
624 |
if (n == nil) |
if (n == nil) |
625 |
return null; |
return null; |
626 |
// Note: removeNode can alter the contents of n, so save value now. |
// Note: removeNode can alter the contents of n, so save value now. |
627 |
Object result = n.value; |
V result = n.value; |
628 |
removeNode(n); |
removeNode(n); |
629 |
return result; |
return result; |
630 |
} |
} |
658 |
* comparator does not tolerate null elements |
* comparator does not tolerate null elements |
659 |
* @throws IllegalArgumentException if fromKey is greater than toKey |
* @throws IllegalArgumentException if fromKey is greater than toKey |
660 |
*/ |
*/ |
661 |
public SortedMap subMap(Object fromKey, Object toKey) |
public SortedMap<K, V> subMap(K fromKey, K toKey) |
662 |
{ |
{ |
663 |
return new SubMap(fromKey, toKey); |
return new SubMap(fromKey, toKey); |
664 |
} |
} |
678 |
* @throws NullPointerException if fromKey is null, but the comparator |
* @throws NullPointerException if fromKey is null, but the comparator |
679 |
* does not tolerate null elements |
* does not tolerate null elements |
680 |
*/ |
*/ |
681 |
public SortedMap tailMap(Object fromKey) |
public SortedMap<K, V> tailMap(K fromKey) |
682 |
{ |
{ |
683 |
return new SubMap(fromKey, nil); |
return new SubMap(fromKey, nil); |
684 |
} |
} |
926 |
* |
* |
927 |
* @return the first node |
* @return the first node |
928 |
*/ |
*/ |
929 |
final Node firstNode() |
final Node<K, V> firstNode() |
930 |
{ |
{ |
931 |
// Exploit fact that nil.left == nil. |
// Exploit fact that nil.left == nil. |
932 |
Node node = root; |
Node node = root; |
942 |
* @param key the key to search for |
* @param key the key to search for |
943 |
* @return the node where the key is found, or nil |
* @return the node where the key is found, or nil |
944 |
*/ |
*/ |
945 |
final Node getNode(Object key) |
final Node<K, V> getNode(K key) |
946 |
{ |
{ |
947 |
Node current = root; |
Node current = root; |
948 |
while (current != nil) |
while (current != nil) |
1732 |
return count; |
return count; |
1733 |
} |
} |
1734 |
|
|
1735 |
public SortedMap subMap(Object fromKey, Object toKey) |
public SortedMap<K, V> subMap(K fromKey, K toKey) |
1736 |
{ |
{ |
1737 |
if (! keyInRange(fromKey) || ! keyInRange(toKey)) |
if (! keyInRange(fromKey) || ! keyInRange(toKey)) |
1738 |
throw new IllegalArgumentException("key outside range"); |
throw new IllegalArgumentException("key outside range"); |
1739 |
return new SubMap(fromKey, toKey); |
return new SubMap(fromKey, toKey); |
1740 |
} |
} |
1741 |
|
|
1742 |
public SortedMap tailMap(Object fromKey) |
public SortedMap<K, V> tailMap(K fromKey) |
1743 |
{ |
{ |
1744 |
if (! keyInRange(fromKey)) |
if (! keyInRange(fromKey)) |
1745 |
throw new IllegalArgumentException("key outside range"); |
throw new IllegalArgumentException("key outside range"); |