## gate.util
Class RBTreeMap

    java.lang.Object
      |
      +--java.util.AbstractMap
            |
            +--gate.util.RBTreeMap

All Implemented Interfaces:
  Cloneable, Map, Serializable, SortedMap

public class RBTreeMap
extends AbstractMap
implements SortedMap, Cloneable, Serializable

Slightly modified implementation of java.util.TreeMap in order to return the closest neighbours in the case of a failed search.

See Also:
  Serialized Form

Inner Class Summary
`(package private) static class` `RBTreeMap.Entry` Node in the Tree.
`private class` `RBTreeMap.Iterator` RBTreeMap Iterator.
`private class` `RBTreeMap.SubMap`

Inner classes inherited from class java.util.Map
`Map.Entry`

Field Summary
`private static boolean` `BLACK`
`private Comparator` `comparator` The Comparator used to maintain order in this RBTreeMap, or null if this RBTreeMap uses its elements natural ordering.
`private static boolean` `DEBUG` Debug flag
`private static int` `ENTRIES`
`private Set` `entrySet`
`private static int` `KEYS`
`private Set` `keySet` These fields are initialized to contain an instance of the appropriate view the first time this view is requested.
`private int` `modCount` The number of structural modifications to the tree.
`private static boolean` `RED`
`private RBTreeMap.Entry` `root`
`(package private) static long` `serialVersionUID` Freeze the serialization UID.
`private int` `size` The number of entries in the tree
`private Collection` `values`
`private static int` `VALUES`

Constructor Summary
`RBTreeMap()` Constructs a new, empty map, sorted according to the keys' natural order.
`RBTreeMap(Comparator c)` Constructs a new, empty map, sorted according to the given comparator.
`RBTreeMap(Map m)` Constructs a new map containing the same mappings as the given map, sorted according to the keys' natural order.
`RBTreeMap(SortedMap m)` Constructs a new map containing the same mappings as the given `SortedMap`, sorted according to the same ordering.

Method Summary
`(package private) void` `addAllForTreeSet(SortedSet set, Object defaultVal)` Intended to be called only from TreeSet.addAll
`private static RBTreeMap.Entry` `buildFromSorted(int level, int lo, int hi, int redLevel, Iterator it, ObjectInputStream str, Object defaultVal)` Recursive "helper method" that does the real work of the of the previous method.
`private void` `buildFromSorted(int size, Iterator it, ObjectInputStream str, Object defaultVal)` Linear time tree building algorithm from sorted data.
`void` `clear()` Removes all mappings from this RBTreeMap.
`Object` `clone()` Returns a shallow copy of this `RBTreeMap` instance.
`private static boolean` `colorOf(RBTreeMap.Entry p)` Balancing operations.
`Comparator` `comparator()` Returns the comparator used to order this map, or `null` if this map uses its keys' natural order.
`private int` `compare(Object k1, Object k2)` Compares two keys using the correct comparison method for this RBTreeMap.
`private static int` `computeRedLevel(int sz)` Find the level down to which to assign all nodes BLACK.
`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 void` `decrementSize()`
`private void` `deleteEntry(RBTreeMap.Entry p)` Delete node p, and then rebalance the tree.
`Set` `entrySet()` Returns a set view of the mappings contained in this map.
`private RBTreeMap.Entry` `firstEntry()` Returns the first Entry in the RBTreeMap (according to the RBTreeMap's key-sort function).
`Object` `firstKey()` Returns the first (lowest) key currently in this sorted map.
`private void` `fixAfterDeletion(RBTreeMap.Entry x)` From CLR
`private void` `fixAfterInsertion(RBTreeMap.Entry x)` From CLR
`Object` `get(Object key)` Returns the value to which this map maps the specified key.
`private RBTreeMap.Entry` `getCeilEntry(Object key)` Gets the entry corresponding to the specified key; if no such entry exists, returns the entry for the least key greater than the specified key; if no such entry exists (i.e., the greatest key in the Tree is less than the specified key), returns `null`.
`Object[]` `getClosestMatch(Object key)` Returns a pair of values: (glb,lub).
`private RBTreeMap.Entry` `getEntry(Object key)` Returns this map's entry for the given key, or `null` if the map does not contain an entry for the key.
`Object` `getNextOf(Object key)` Returns the value associated to the next key in the map if an exact match doesn't exist.
`private RBTreeMap.Entry` `getPrecedingEntry(Object key)` Returns the entry for the greatest key less than the specified key; if no such entry exists (i.e., the least key in the Tree is greater than the specified key), returns `null`.
`SortedMap` `headMap(Object toKey)` Returns a view of the portion of this map whose keys are strictly less than `toKey`.
`private void` `incrementSize()`
`private static Object` `key(RBTreeMap.Entry e)` Returns the key corresonding to the specified Entry.
`Set` `keySet()` Returns a Set view of the keys contained in this map.
`private RBTreeMap.Entry` `lastEntry()` Returns the last Entry in the RBTreeMap (according to the RBTreeMap's key-sort function).
`Object` `lastKey()` Returns the last (highest) key currently in this sorted map.
`private static RBTreeMap.Entry` `leftOf(RBTreeMap.Entry p)`
`private static RBTreeMap.Entry` `parentOf(RBTreeMap.Entry p)`
`Object` `put(Object key, Object value)` Associates the specified value with the specified key in this map.
`void` `putAll(Map map)` Copies all of the mappings from the specified map to this map.
`private void` `readObject(ObjectInputStream s)` Reconstitute the `RBTreeMap` instance from a stream (i.e., deserialize it).
`(package private) void` `readTreeSet(int size, ObjectInputStream s, Object defaultVal)` Intended to be called only from TreeSet.readObject
`Object` `remove(Object key)` Removes the mapping for this key from this RBTreeMap if present.
`private static RBTreeMap.Entry` `rightOf(RBTreeMap.Entry p)`
`private void` `rotateLeft(RBTreeMap.Entry p)` From CLR
`private void` `rotateRight(RBTreeMap.Entry p)` From CLR
`private static void` `setColor(RBTreeMap.Entry p, boolean c)`
`int` `size()` Returns the number of key-value mappings in this map.
`SortedMap` `subMap(Object fromKey, Object toKey)` Returns a view of the portion of this map whose keys range from `fromKey`, inclusive, to `toKey`, exclusive.
`private RBTreeMap.Entry` `successor(RBTreeMap.Entry t)` Returns the successor of the specified Entry, or null if no such.
`private void` `swapPosition(RBTreeMap.Entry x, RBTreeMap.Entry y)` Swap the linkages of two nodes in a tree.
`SortedMap` `tailMap(Object fromKey)` Returns a view of the portion of this map whose keys are greater than or equal to `fromKey`.
`private static boolean` `valEquals(Object o1, Object o2)` Test two values for equality.
`Collection` `values()` Returns a collection view of the values contained in this map.
`private boolean` `valueSearchNonNull(RBTreeMap.Entry n, Object value)`
`private boolean` `valueSearchNull(RBTreeMap.Entry n)`
`private void` `writeObject(ObjectOutputStream s)` Save the state of the `RBTreeMap` instance to a stream (i.e., serialize it).

Methods inherited from class java.util.AbstractMap
`equals, hashCode, isEmpty, toString`

Methods inherited from class java.lang.Object
`<clinit>, finalize, getClass, notify, notifyAll, registerNatives, wait, wait, wait`

Methods inherited from interface java.util.Map
`equals, hashCode, isEmpty`

Field Detail

### DEBUG

    private static final boolean DEBUG

  Debug flag

### serialVersionUID

    static final long serialVersionUID

  Freeze the serialization UID.

### comparator

    private Comparator comparator

  The Comparator used to maintain order in this RBTreeMap, or null if this RBTreeMap uses its elements natural ordering.

### root

    private transient RBTreeMap.Entry root

### size

    private transient int size

  The number of entries in the tree

### modCount

    private transient int modCount

  The number of structural modifications to the tree.

### keySet

    private transient Set keySet

  These fields are initialized to contain an instance of the appropriate view the first time this view is requested. The views are stateless, so there's no reason to create more than one of each.

### entrySet

    private transient Set entrySet

### values

    private transient Collection values

### KEYS

    private static final int KEYS

### VALUES

    private static final int VALUES

### ENTRIES

    private static final int ENTRIES

### RED

    private static final boolean RED

### BLACK

    private static final boolean BLACK

Constructor Detail

### RBTreeMap

    public RBTreeMap()

  Constructs a new, empty map, sorted according to the keys' natural order. All keys inserted into the map must implement the `Comparable` interface. Furthermore, all such keys must be mutually comparable: `k1.compareTo(k2)` must not throw a ClassCastException for any elements `k1` and `k2` in the map. If the user attempts to put a key into the map that violates this constraint (for example, the user attempts to put a string key into a map whose keys are integers), the `put(Object key, Object value)` call will throw a `ClassCastException`.
  See Also:
    `Comparable`

### RBTreeMap

    public RBTreeMap(Comparator c)

  Constructs a new, empty map, sorted according to the given comparator. All keys inserted into the map must be mutually comparable by the given comparator: `comparator.compare(k1, k2)` must not throw a `ClassCastException` for any keys `k1` and `k2` in the map. If the user attempts to put a key into the map that violates this constraint, the `put(Object key, Object value)` call will throw a `ClassCastException`.

### RBTreeMap

    public RBTreeMap(Map m)

  Constructs a new map containing the same mappings as the given map, sorted according to the keys' natural order. All keys inserted into the new map must implement the `Comparable` interface. Furthermore, all such keys must be mutually comparable: `k1.compareTo(k2)` must not throw a `ClassCastException` for any elements `k1` and `k2` in the map. This method runs in n*log(n) time.
  Throws:
    `ClassCastException` - the keys in t are not Comparable, or are not mutually comparable.

### RBTreeMap

    public RBTreeMap(SortedMap m)

  Constructs a new map containing the same mappings as the given `SortedMap`, sorted according to the same ordering. This method runs in linear time.

Method Detail

### incrementSize

    private void incrementSize()

### decrementSize

    private void decrementSize()

### size

    public int size()

  Returns the number of key-value mappings in this map.
  Specified by:
    `size` in interface `Map`
  Overrides:
    `size` in class `AbstractMap`
  Returns:
    the number of key-value mappings in this map.

### containsKey

    public boolean containsKey(Object key)

  Returns `true` if this map contains a mapping for the specified key.
  Specified by:
    `containsKey` in interface `Map`
  Overrides:
    `containsKey` in class `AbstractMap`
  Parameters:
    `key` - key whose presence in this map is to be tested.
  Returns:
    `true` if this map contains a mapping for the specified key.
  Throws:
    `ClassCastException` - if the key cannot be compared with the keys currently in the map.
    `NullPointerException` - key is `null` and this map uses natural ordering, or its comparator does not tolerate `null` keys.

### containsValue

    public boolean containsValue(Object value)

  Returns `true` if this map maps one or more keys to the specified value. More formally, returns `true` if and only if this map contains at least one mapping to a value `v` such that `(value==null ? v==null : value.equals(v))`. This operation will probably require time linear in the Map size for most implementations of Map.
  Specified by:
    `containsValue` in interface `Map`
  Overrides:
    `containsValue` in class `AbstractMap`
  Parameters:
    `value` - value whose presence in this Map is to be tested.
  Since:
    JDK1.2

### valueSearchNull

    private boolean valueSearchNull(RBTreeMap.Entry n)

### valueSearchNonNull

    private boolean valueSearchNonNull(RBTreeMap.Entry n,
                                       Object value)

### getClosestMatch

    public Object[] getClosestMatch(Object key)

  Returns a pair of values: (glb,lub). If the given key is found in the map then glb=lub=the value associated with the given key. If the key is not in the map: glb=the value associated with the greatest key in the map that is lower than the given key; or null if the given key is smaller than any key in the map. lub=the value associated with the smaller key in the map that is greater than the given key; or null the given key is greater than any key in the map. If the map is empty it returns (null,null).

### getNextOf

    public Object getNextOf(Object key)

  Returns the value associated to the next key in the map if an exact match doesn't exist. If there is an exact match, the method will return the value associated to the given key.
  Parameters:
    `key` - the key for wich the look-up will be done.
  Returns:
    the value associated to the given key or the next available value

### get

    public Object get(Object key)

  Returns the value to which this map maps the specified key. Returns `null` if the map contains no mapping for this key. A return value of `null` does not necessarily indicate that the map contains no mapping for the key; it's also possible that the map explicitly maps the key to `null`. The `containsKey` operation may be used to distinguish these two cases.
  Specified by:
    `get` in interface `Map`
  Overrides:
    `get` in class `AbstractMap`
  Parameters:
    `key` - key whose associated value is to be returned.
  Returns:
    the value to which this map maps the specified key, or `null` if the map contains no mapping for the key.
  Throws:
    `ClassCastException` - key cannot be compared with the keys currently in the map.
    `NullPointerException` - key is `null` and this map uses natural ordering, or its comparator does not tolerate `null` keys.
  See Also:
    `containsKey(Object)`

### comparator

    public Comparator comparator()

  Returns the comparator used to order this map, or `null` if this map uses its keys' natural order.
  Specified by:
    `comparator` in interface `SortedMap`
  Returns:
    the comparator associated with this sorted map, or `null` if it uses its keys' natural sort method.

### firstKey

    public Object firstKey()

  Returns the first (lowest) key currently in this sorted map.
  Specified by:
    `firstKey` in interface `SortedMap`
  Returns:
    the first (lowest) key currently in this sorted map.
  Throws:
    `NoSuchElementException` - Map is empty.

### lastKey

    public Object lastKey()

  Returns the last (highest) key currently in this sorted map.
  Specified by:
    `lastKey` in interface `SortedMap`
  Returns:
    the last (highest) key currently in this sorted map.
  Throws:
    `NoSuchElementException` - Map is empty.

### putAll

    public void putAll(Map map)

  Copies all of the mappings from the specified map to this map. These mappings replace any mappings that this map had for any of the keys currently in the specified map.
  Specified by:
    `putAll` in interface `Map`
  Overrides:
    `putAll` in class `AbstractMap`
  Parameters:
    `t` - Mappings to be stored in this map.
  Throws:
    `ClassCastException` - class of a key or value in the specified map prevents it from being stored in this map.
    `NullPointerException` - this map does not permit `null` keys and a specified key is `null`.

### getEntry

    private RBTreeMap.Entry getEntry(Object key)

  Returns this map's entry for the given key, or `null` if the map does not contain an entry for the key.
  Returns:
    this map's entry for the given key, or `null` if the map does not contain an entry for the key.
  Throws:
    `ClassCastException` - if the key cannot be compared with the keys currently in the map.
    `NullPointerException` - key is `null` and this map uses natural order, or its comparator does not tolerate * `null` keys.

### getCeilEntry

    private RBTreeMap.Entry getCeilEntry(Object key)

  Gets the entry corresponding to the specified key; if no such entry exists, returns the entry for the least key greater than the specified key; if no such entry exists (i.e., the greatest key in the Tree is less than the specified key), returns `null`.

### getPrecedingEntry

    private RBTreeMap.Entry getPrecedingEntry(Object key)

  Returns the entry for the greatest key less than the specified key; if no such entry exists (i.e., the least key in the Tree is greater than the specified key), returns `null`.

### key

    private static Object key(RBTreeMap.Entry e)

  Returns the key corresonding to the specified Entry. Throw NoSuchElementException if the Entry is `null`.

### put

    public Object put(Object key,
                      Object value)

  Associates the specified value with the specified key in this map. If the map previously contained a mapping for this key, the old value is replaced.
  Specified by:
    `put` in interface `Map`
  Overrides:
    `put` in class `AbstractMap`
  Parameters:
    `key` - key with which the specified value is to be associated.
    `value` - value to be associated with the specified key.
  Returns:
    previous value associated with specified key, or `null` if there was no mapping for key. A `null` return can also indicate that the map previously associated `null` with the specified key.
  Throws:
    `ClassCastException` - key cannot be compared with the keys currently in the map.
    `NullPointerException` - key is `null` and this map uses natural order, or its comparator does not tolerate `null` keys.

### remove

    public Object remove(Object key)

  Removes the mapping for this key from this RBTreeMap if present.
  Specified by:
    `remove` in interface `Map`
  Overrides:
    `remove` in class `AbstractMap`
  Returns:
    previous value associated with specified key, or `null` if there was no mapping for key. A `null` return can also indicate that the map previously associated `null` with the specified key.
  Throws:
    `ClassCastException` - key cannot be compared with the keys currently in the map.
    `NullPointerException` - key is `null` and this map uses natural order, or its comparator does not tolerate `null` keys.

### clear

    public void clear()

  Removes all mappings from this RBTreeMap.
  Specified by:
    `clear` in interface `Map`
  Overrides:
    `clear` in class `AbstractMap`

### clone

    public Object clone()

  Returns a shallow copy of this `RBTreeMap` instance. (The keys and values themselves are not cloned.)
  Overrides:
    `clone` in class `Object`
  Returns:
    a shallow copy of this Map.

### keySet

    public Set keySet()

  Returns a Set view of the keys contained in this map. The set's iterator will return the keys in ascending order. The map is backed by this `RBTreeMap` instance, so changes to this map are reflected in the Set, and vice-versa. The Set supports element removal, which removes the corresponding mapping from the map, via the `Iterator.remove`, `Set.remove`, `removeAll`, `retainAll`, and `clear` operations. It does not support the `add` or `addAll` operations.
  Specified by:
    `keySet` in interface `Map`
  Overrides:
    `keySet` in class `AbstractMap`
  Returns:
    a set view of the keys contained in this RBTreeMap.

### values

    public Collection values()

  Returns a collection view of the values contained in this map. The collection's iterator will return the values in the order that their corresponding keys appear in the tree. The collection is backed by this `RBTreeMap` instance, so changes to this map are reflected in the collection, and vice-versa. The collection supports element removal, which removes the corresponding mapping from the map through the `Iterator.remove`, `Collection.remove`, `removeAll`, `retainAll`, and `clear` operations. It does not support the `add` or `addAll` operations.
  Specified by:
    `values` in interface `Map`
  Overrides:
    `values` in class `AbstractMap`
  Returns:
    a collection view of the values contained in this map.

### entrySet

    public Set entrySet()

  Returns a set view of the mappings contained in this map. The set's iterator returns the mappings in ascending key order. Each element in the returned set is a `Map.Entry`. The set is backed by this map, so changes to this map are reflected in the set, and vice-versa. The set supports element removal, which removes the corresponding mapping from the RBTreeMap, through the `Iterator.remove`, `Set.remove`, `removeAll`, `retainAll` and `clear` operations. It does not support the `add` or `addAll` operations.
  Specified by:
    `entrySet` in interface `Map`
  Overrides:
    `entrySet` in class `AbstractMap`
  Returns:
    a set view of the mappings contained in this map.
  See Also:
    `Map.Entry`

### subMap

    public SortedMap subMap(Object fromKey,
                            Object toKey)

  Returns a view of the portion of this map whose keys range from `fromKey`, inclusive, to `toKey`, exclusive. (If `fromKey` and `toKey` are equal, the returned sorted map is empty.) The returned sorted map is backed by this map, so changes in the returned sorted map are reflected in this map, and vice-versa. The returned sorted map supports all optional map operations.

  The sorted map returned by this method will throw an `IllegalArgumentException` if the user attempts to insert a key less than `fromKey` or greater than or equal to `toKey`.

  Note: this method always returns a half-open range (which includes its low endpoint but not its high endpoint). If you need a closed range (which includes both endpoints), and the key type allows for calculation of the successor a given key, merely request the subrange from `lowEndpoint` to `successor(highEndpoint)`. For example, suppose that `m` is a sorted map whose keys are strings. The following idiom obtains a view containing all of the key-value mappings in `m` whose keys are between `low` and `high`, inclusive:

          SortedMap sub = m.submap(low, high+"\0");

  A similar technique can be used to generate an open range (which contains neither endpoint). The following idiom obtains a view containing all of the key-value mappings in `m` whose keys are between `low` and `high`, exclusive:

          SortedMap sub = m.subMap(low+"\0", high);

  Specified by:
    `subMap` in interface `SortedMap`
  Parameters:
    `fromKey` - low endpoint (inclusive) of the subMap.
    `toKey` - high endpoint (exclusive) of the subMap.
  Returns:
    a view of the portion of this map whose keys range from `fromKey`, inclusive, to `toKey`, exclusive.
  Throws:
    `NullPointerException` - if `fromKey` or `toKey` is `null` and this map uses natural order, or its comparator does not tolerate `null` keys.
    `IllegalArgumentException` - if `fromKey` is greater than `toKey`.

### headMap

    public SortedMap headMap(Object toKey)

  Returns a view of the portion of this map whose keys are strictly less than `toKey`. The returned sorted map is backed by this map, so changes in the returned sorted map are reflected in this map, and vice-versa. The returned sorted map supports all optional map operations.

  The sorted map returned by this method will throw an `IllegalArgumentException` if the user attempts to insert a key greater than or equal to `toKey`.

  Note: this method always returns a view that does not contain its (high) endpoint. If you need a view that does contain this endpoint, and the key type allows for calculation of the successor a given key, merely request a headMap bounded by `successor(highEndpoint)`. For example, suppose that suppose that `m` is a sorted map whose keys are strings. The following idiom obtains a view containing all of the key-value mappings in `m` whose keys are less than or equal to `high`:

           SortedMap head = m.headMap(high+"\0");

  Specified by:
    `headMap` in interface `SortedMap`
  Parameters:
    `toKey` - high endpoint (exclusive) of the headMap.
  Returns:
    a view of the portion of this map whose keys are strictly less than `toKey`.
  Throws:
    `NullPointerException` - if `toKey` is `null` and this map uses natural order, or its comparator does * not tolerate `null` keys.

### tailMap

    public SortedMap tailMap(Object fromKey)

  Returns a view of the portion of this map whose keys are greater than or equal to `fromKey`. The returned sorted map is backed by this map, so changes in the returned sorted map are reflected in this map, and vice-versa. The returned sorted map supports all optional map operations.

  The sorted map returned by this method will throw an `IllegalArgumentException` if the user attempts to insert a key less than `fromKey`.

  Note: this method always returns a view that contains its (low) endpoint. If you need a view that does not contain this endpoint, and the element type allows for calculation of the successor a given value, merely request a tailMap bounded by `successor(lowEndpoint)`. For For example, suppose that suppose that `m` is a sorted map whose keys are strings. The following idiom obtains a view containing all of the key-value mappings in `m` whose keys are strictly greater than `low`:

           SortedMap tail = m.tailMap(low+"\0");

  Specified by:
    `tailMap` in interface `SortedMap`
  Parameters:
    `fromKey` - low endpoint (inclusive) of the tailMap.
  Returns:
    a view of the portion of this map whose keys are greater than or equal to `fromKey`.
  Throws:
    `NullPointerException` - fromKey is `null` and this map uses natural ordering, or its comparator does not tolerate `null` keys.

### compare

    private int compare(Object k1,
                        Object k2)

  Compares two keys using the correct comparison method for this RBTreeMap.

### valEquals

    private static boolean valEquals(Object o1,
                                     Object o2)

  Test two values for equality. Differs from o1.equals(o2) only in that it copes with with `null` o1 properly.

### firstEntry

    private RBTreeMap.Entry firstEntry()

  Returns the first Entry in the RBTreeMap (according to the RBTreeMap's key-sort function). Returns null if the RBTreeMap is empty.

### lastEntry

    private RBTreeMap.Entry lastEntry()

  Returns the last Entry in the RBTreeMap (according to the RBTreeMap's key-sort function). Returns null if the RBTreeMap is empty.

### successor

    private RBTreeMap.Entry successor(RBTreeMap.Entry t)

  Returns the successor of the specified Entry, or null if no such.

### colorOf

    private static boolean colorOf(RBTreeMap.Entry p)

  Balancing operations. Implementations of rebalancings during insertion and deletion are slightly different than the CLR version. Rather than using dummy nilnodes, we use a set of accessors that deal properly with null. They are used to avoid messiness surrounding nullness checks in the main algorithms.

### parentOf

    private static RBTreeMap.Entry parentOf(RBTreeMap.Entry p)

### setColor

    private static void setColor(RBTreeMap.Entry p,
                                 boolean c)

### leftOf

    private static RBTreeMap.Entry leftOf(RBTreeMap.Entry p)

### rightOf

    private static RBTreeMap.Entry rightOf(RBTreeMap.Entry p)

### rotateLeft

    private void rotateLeft(RBTreeMap.Entry p)

  From CLR

### rotateRight

    private void rotateRight(RBTreeMap.Entry p)

  From CLR

### fixAfterInsertion

    private void fixAfterInsertion(RBTreeMap.Entry x)

  From CLR

### deleteEntry

    private void deleteEntry(RBTreeMap.Entry p)

  Delete node p, and then rebalance the tree.

### fixAfterDeletion

    private void fixAfterDeletion(RBTreeMap.Entry x)

  From CLR

### swapPosition

    private void swapPosition(RBTreeMap.Entry x,
                              RBTreeMap.Entry y)

  Swap the linkages of two nodes in a tree.

### writeObject

    private void writeObject(ObjectOutputStream s)
                      throws IOException

  Save the state of the `RBTreeMap` instance to a stream (i.e., serialize it).

### readObject

    private void readObject(ObjectInputStream s)
                     throws IOException,
                            ClassNotFoundException

  Reconstitute the `RBTreeMap` instance from a stream (i.e., deserialize it).

### readTreeSet

    void readTreeSet(int size,
                     ObjectInputStream s,
                     Object defaultVal)
               throws IOException,
                      ClassNotFoundException

  Intended to be called only from TreeSet.readObject

### addAllForTreeSet

    void addAllForTreeSet(SortedSet set,
                          Object defaultVal)

  Intended to be called only from TreeSet.addAll

### buildFromSorted

    private void buildFromSorted(int size,
                                 Iterator it,
                                 ObjectInputStream str,
                                 Object defaultVal)
                          throws IOException,
                                 ClassNotFoundException

  Linear time tree building algorithm from sorted data. Can accept keys and/or values from iterator or stream. This leads to too many parameters, but seems better than alternatives. The four formats that this method accepts are: 1) An iterator of Map.Entries. (it != null, defaultVal == null). 2) An iterator of keys. (it != null, defaultVal != null). 3) A stream of alternating serialized keys and values. (it == null, defaultVal == null). 4) A stream of serialized keys. (it == null, defaultVal != null). It is assumed that the comparator of the RBTreeMap is already set prior to calling this method.
  Parameters:
    `size` - the number of keys (or key-value pairs) to be read from the iterator or stream.
    `it` - If non-null, new entries are created from entries or keys read from this iterator.
    `it` - If non-null, new entries are created from keys and possibly values read from this stream in serialized form. Exactly one of it and str should be non-null.
    `defaultVal` - if non-null, this default value is used for each value in the map. If null, each value is read from iterator or stream, as described above.
  Throws:
    `IOException` - propagated from stream reads. This cannot occur if str is null.
    `ClassNotFoundException` - propagated from readObject. This cannot occur if str is null.

### buildFromSorted

    private static RBTreeMap.Entry buildFromSorted(int level,
                                                   int lo,
                                                   int hi,
                                                   int redLevel,
                                                   Iterator it,
                                                   ObjectInputStream str,
                                                   Object defaultVal)
                                            throws IOException,
                                                   ClassNotFoundException

  Recursive "helper method" that does the real work of the of the previous method. Identically named parameters have identical definitions. Additional parameters are documented below. It is assumed that the comparator and size fields of the RBTreeMap are already set prior to calling this method. (It ignores both fields.)
  Parameters:
    `level` - the current level of tree. Initial call should be 0.
    `lo` - the first element index of this subtree. Initial should be 0.
    `hi` - the last element index of this subtree. Initial should be size-1.
    `redLevel` - the level at which nodes should be red. Must be equal to computeRedLevel for tree of this size.

### computeRedLevel

    private static int computeRedLevel(int sz)

  Find the level down to which to assign all nodes BLACK. This is the last `full' level of the complete binary tree produced by buildTree. The remaining nodes are colored RED. (This makes a `nice' set of color assignments wrt future insertions.) This level number is computed by finding the number of splits needed to reach the zeroeth node. (The answer is ~lg(N), but in any case must be computed by same quick O(lg(N)) loop.)