Map源碼分析:TreeMap( jdk 1.8 )和紅黑樹

紅黑樹:https://github.com/CarpenterLee/JCFInternals/blob/master/markdown/5-TreeSet%20and%20TreeMap.md

TreeMap


TreeMap是基於紅黑樹(一種自平衡的二叉查找樹)實現的一個保證有序性的Map,在繼承關係結構圖中可以得知TreeMap實現了NavigableMap接口,而該接口又繼承了SortedMap接口,我們先來看看這兩個接口定義了一些什麼功能。

SortedMap


首先是SortedMap接口,實現該接口的實現類應當按照自然排序保證key的有序性,所謂自然排序即是根據key的compareTo()函數(需要實現Comparable接口)或者在構造函數中傳入的Comparator實現類來進行排序,集合視圖遍歷元素的順序也應當與key的順序一致。SortedMap接口還定義了以下幾個有效利用有序性的函數:

package java.util;
public interface SortedMap<K,V> extends Map<K,V> {
    /**
     * 用於在此Map中對key進行排序的比較器,如果爲null,則使用key的compareTo()函數進行比較。
     */
    Comparator<? super K> comparator();
    /**
     * 返回一個key的範圍爲從fromKey到toKey的局部視圖(包括fromKey,不包括toKey,包左不包右),
     * 如果fromKey和toKey是相等的,則返回一個空視圖。
     * 返回的局部視圖同樣是此Map的集合視圖,所以對它的操作是會與Map互相影響的。
     */
    SortedMap<K,V> subMap(K fromKey, K toKey);
    /**
     * 返回一個嚴格地小於toKey的局部視圖。
     */
    SortedMap<K,V> headMap(K toKey);
    /**
     * 返回一個大於或等於fromKey的局部視圖。
     */
    SortedMap<K,V> tailMap(K fromKey);
    /**
     * 返回當前Map中的第一個key(最小)。
     */
    K firstKey();
    /**
     * 返回當前Map中的最後一個key(最大)。
     */
    K lastKey();
    Set<K> keySet();
    Collection<V> values();
    Set<Map.Entry<K, V>> entrySet();
} 

然後是SortedMap的子接口NavigableMap,該接口擴展了一些用於導航(Navigation)的方法,像函數lowerEntry(key)會根據傳入的參數key返回一個小於key的最大的一對鍵值對,例如,我們如下調用lowerEntry(6),那麼將返回key爲5的鍵值對,如果沒有key爲5,則會返回key爲4的鍵值對,以此類推,直到返回null(實在找不到的情況下)。

public static void main(String[] args) {
    NavigableMap<Integer, Integer> map = new TreeMap<>();
    for (int i = 0; i < 10; i++)
        map.put(i, i);
 
    assert map.lowerEntry(6).getKey() == 5;
    assert map.lowerEntry(5).getKey() == 4;
    assert map.lowerEntry(0).getKey() == null;
}

NavigableMap定義的都是一些類似於lowerEntry(key)的方法和以逆序、升序排序的集合視圖,這些方法利用有序性實現了相比SortedMap接口更加靈活的操作。

package java.util;
public interface NavigableMap<K,V> extends SortedMap<K,V> {
    /**
     * 返回一個小於指定key的最大的一對鍵值對,如果找不到則返回null。
     */
    Map.Entry<K,V> lowerEntry(K key);
    /**
     * 返回一個小於指定key的最大的一個key,如果找不到則返回null。
     */
    K lowerKey(K key);
    /**
     * 返回一個小於或等於指定key的最大的一對鍵值對,如果找不到則返回null。
     */
    Map.Entry<K,V> floorEntry(K key);
    /**
     * 返回一個小於或等於指定key的最大的一個key,如果找不到則返回null。
     */
    K floorKey(K key);
    /**
     * 返回一個大於或等於指定key的最小的一對鍵值對,如果找不到則返回null。
     */
    Map.Entry<K,V> ceilingEntry(K key);
    /**
     * 返回一個大於或等於指定key的最小的一個key,如果找不到則返回null。
     */
    K ceilingKey(K key);
    /**
     * 返回一個大於指定key的最小的一對鍵值對,如果找不到則返回null。
     */
    Map.Entry<K,V> higherEntry(K key);
    /**
     * 返回一個大於指定key的最小的一個key,如果找不到則返回null。
     */
    K higherKey(K key);
    /**
     * 返回該Map中最小的鍵值對,如果Map爲空則返回null。
     */
    Map.Entry<K,V> firstEntry();
    /**
     * 返回該Map中最大的鍵值對,如果Map爲空則返回null。
     */
    Map.Entry<K,V> lastEntry();
    /**
     * 返回並刪除該Map中最小的鍵值對,如果Map爲空則返回null。
     */
    Map.Entry<K,V> pollFirstEntry();
    /**
     * 返回並刪除該Map中最大的鍵值對,如果Map爲空則返回null。
     */
    Map.Entry<K,V> pollLastEntry();
    /**
     * 返回一個以當前Map降序(逆序)排序的集合視圖
     */
    NavigableMap<K,V> descendingMap();
    /**
     * 返回一個包含當前Map中所有key的集合視圖,該視圖中的key以升序(正序)排序。
     */
    NavigableSet<K> navigableKeySet();
    /**
     * 返回一個包含當前Map中所有key的集合視圖,該視圖中的key以降序(逆序)排序。
     */
    NavigableSet<K> descendingKeySet();
    /**
     * 與SortedMap.subMap基本一致,區別在於多的兩個參數fromInclusive和toInclusive,
     * 它們代表是否包含from和to,如果fromKey與toKey相等,並且fromInclusive與toInclusive
     * 都爲true,那麼不會返回空集合。
     */
    NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive,
                             K toKey,   boolean toInclusive);
    /**
     * 返回一個小於或等於(inclusive爲true的情況下)toKey的局部視圖。
     */
    NavigableMap<K,V> headMap(K toKey, boolean inclusive);
    /**
     * 返回一個大於或等於(inclusive爲true的情況下)fromKey的局部視圖。
     */
    NavigableMap<K,V> tailMap(K fromKey, boolean inclusive);
    /**
     * 等價於subMap(fromKey, true, toKey, false)。
     */
    SortedMap<K,V> subMap(K fromKey, K toKey);
    /**
     * 等價於headMap(toKey, false)。
     */
    SortedMap<K,V> headMap(K toKey);
    /**
     * 等價於tailMap(fromKey, true)。
     */
    SortedMap<K,V> tailMap(K fromKey);
}

NavigableMap接口相對於SortedMap接口來說靈活了許多,正因爲TreeMap也實現了該接口,所以在需要數據有序而且想靈活地訪問它們的時候,使用TreeMap就非常合適了。

紅黑樹


上文我們提到TreeMap的內部實現基於紅黑樹,而紅黑樹又是二叉查找樹的一種。二叉查找樹是一種有序的樹形結構,優勢在於查找、插入的時間複雜度只有O(log n),特性如下:

  • 任意節點最多含有兩個子節點。
  • 任意節點的左、右節點都可以看做爲一棵二叉查找樹。
  • 如果任意節點的左子樹不爲空,那麼左子樹上的所有節點的值均小於它的根節點的值。
  • 如果任意節點的右子樹不爲空,那麼右子樹上的所有節點的值均大於它的根節點的值。
  • 任意節點的key都是不同的。

儘管二叉查找樹看起來很美好,但事與願違,二叉查找樹在極端情況下會變得並不是那麼有效率,假設我們有一個有序的整數序列:1,2,3,4,5,6,7,8,9,10,...,如果把這個序列按順序全部插入到二叉查找樹時會發生什麼呢?二叉查找樹會產生傾斜,序列中的每一個元素都大於它的根節點(前一個元素),左子樹永遠是空的,那麼這棵二叉查找樹就跟一個普通的鏈表沒什麼區別了,查找操作的時間複雜度只有O(n)

爲了解決這個問題需要引入自平衡的二叉查找樹,所謂自平衡,即是在樹結構將要傾斜的情況下進行修正,這個修正操作被稱爲旋轉,通過旋轉操作可以讓樹趨於平衡。

紅黑樹是平衡二叉查找樹的一種實現,它的名字來自於它的子節點是着色的,每個子節點非黑即紅,由於只有兩種顏色(兩種狀態),一般使用boolean來表示,下面爲TreeMap中實現的Entry,它代表紅黑樹中的一個節點:

// Red-black mechanics
private static final boolean RED   = false;
private static final boolean BLACK = true;
/**
 * Node in the Tree.  Doubles as a means to pass key-value pairs back to
 * user (see Map.Entry).
 */
static final class Entry<K,V> implements Map.Entry<K,V> {
    K key;
    V value;
    Entry<K,V> left;
    Entry<K,V> right;
    Entry<K,V> parent;
    boolean color = BLACK;
    /**
     * Make a new cell with given key, value, and parent, and with
     * {@code null} child links, and BLACK color.
     */
    Entry(K key, V value, Entry<K,V> parent) {
        this.key = key;
        this.value = value;
        this.parent = parent;
    }
    /**
     * Returns the key.
     *
     * @return the key
     */
    public K getKey() {
        return key;
    }
    /**
     * Returns the value associated with the key.
     *
     * @return the value associated with the key
     */
    public V getValue() {
        return value;
    }
    /**
     * Replaces the value currently associated with the key with the given
     * value.
     *
     * @return the value associated with the key before this method was
     *         called
     */
    public V setValue(V value) {
        V oldValue = this.value;
        this.value = value;
        return oldValue;
    }
    public boolean equals(Object o) {
        if (!(o instanceof Map.Entry))
            return false;
        Map.Entry<?,?> e = (Map.Entry<?,?>)o;
        return valEquals(key,e.getKey()) && valEquals(value,e.getValue());
    }
    public int hashCode() {
        int keyHash = (key==null ? 0 : key.hashCode());
        int valueHash = (value==null ? 0 : value.hashCode());
        return keyHash ^ valueHash;
    }
    public String toString() {
        return key + "=" + value;
    }
}

任何平衡二叉查找樹的查找操作都是與二叉查找樹是一樣的,因爲查找操作並不會影響樹的結構,也就不需要進行修正,代碼如下:

public V get(Object key) {
    Entry<K,V> p = getEntry(key);
    return (p==null ? null : p.value);
}
final Entry<K,V> getEntry(Object key) {
    // 使用Comparator進行比較
    if (comparator != null)
        return getEntryUsingComparator(key);
    if (key == null)
        throw new NullPointerException();
    @SuppressWarnings("unchecked")
        Comparable<? super K> k = (Comparable<? super K>) key;
    Entry<K,V> p = root;
    // 從根節點開始,不斷比較key的大小進行查找
    while (p != null) {
        int cmp = k.compareTo(p.key);
        if (cmp < 0) // 小於,轉向左子樹
            p = p.left;
        else if (cmp > 0) // 大於,轉向右子樹
            p = p.right;
        else
            return p;
    }
    return null; // 沒有相等的key,返回null
}

而插入和刪除操作與平衡二叉查找樹的細節是息息相關的,關於紅黑樹的實現細節,我之前寫過的一篇博客紅黑樹的那點事兒已經講的很清楚了,對這方面不瞭解的讀者建議去閱讀一下,就不在這裏重複敘述了。

集合視圖


最後看一下TreeMap的集合視圖的實現,集合視圖一般都是實現了一個封裝了當前實例的類,所以對集合視圖的修改本質上就是在修改當前實例,TreeMap也不例外。

TreeMap的headMap()tailMap()以及subMap()函數都返回了一個靜態內部類AscendingSubMap,從名字上也能猜出來,爲了支持倒序,肯定也還有一個DescendingSubMap,它們都繼承於NavigableSubMap,一個繼承AbstractMap並實現了NavigableMap的抽象類:

abstract static class NavigableSubMap<K,V> extends AbstractMap<K,V>
      implements NavigableMap<K,V>, java.io.Serializable {
      private static final long serialVersionUID = -2102997345730753016L;
      final TreeMap<K,V> m;
      /**
       * (fromStart, lo, loInclusive) 與 (toEnd, hi, hiInclusive)代表了兩個三元組,
       * 如果fromStart爲true,那麼範圍的下限(絕對)爲map(被封裝的TreeMap)的起始key,
       * 其他值將被忽略。
       * 如果loInclusive爲true,lo將會被包含在範圍內,否則lo是在範圍外的。
       * toEnd與hiInclusive與上述邏輯相似,只不過考慮的是上限。
       */
      final K lo, hi;
      final boolean fromStart, toEnd;
      final boolean loInclusive, hiInclusive;
      NavigableSubMap(TreeMap<K,V> m,
                      boolean fromStart, K lo, boolean loInclusive,
                      boolean toEnd,     K hi, boolean hiInclusive) {
          if (!fromStart && !toEnd) {
              if (m.compare(lo, hi) > 0)
                  throw new IllegalArgumentException("fromKey > toKey");
          } else {
              if (!fromStart) // type check
                  m.compare(lo, lo);
              if (!toEnd)
                  m.compare(hi, hi);
          }
          this.m = m;
          this.fromStart = fromStart;
          this.lo = lo;
          this.loInclusive = loInclusive;
          this.toEnd = toEnd;
          this.hi = hi;
          this.hiInclusive = hiInclusive;
      }
      // internal utilities
      final boolean tooLow(Object key) {
          if (!fromStart) {
              int c = m.compare(key, lo);
              // 如果key小於lo,或等於lo(需要lo不包含在範圍內)
              if (c < 0 || (c == 0 && !loInclusive))
                  return true;
          }
          return false;
      }
      final boolean tooHigh(Object key) {
          if (!toEnd) {
              int c = m.compare(key, hi);
              // 如果key大於hi,或等於hi(需要hi不包含在範圍內)
              if (c > 0 || (c == 0 && !hiInclusive))
                  return true;
          }
          return false;
      }
      final boolean inRange(Object key) {
          return !tooLow(key) && !tooHigh(key);
      }
      final boolean inClosedRange(Object key) {
          return (fromStart || m.compare(key, lo) >= 0)
              && (toEnd || m.compare(hi, key) >= 0);
      }
      // 判斷key是否在該視圖的範圍之內
      final boolean inRange(Object key, boolean inclusive) {
          return inclusive ? inRange(key) : inClosedRange(key);
      }
      /*
       * 以abs開頭的函數爲關係操作的絕對版本。
       */
      /*
       * 獲得最小的鍵值對:
       * 如果fromStart爲true,那麼直接返回當前map實例的第一個鍵值對即可,
       * 否則,先判斷lo是否包含在範圍內,
       * 如果是,則獲得當前map實例中大於或等於lo的最小的鍵值對,
       * 如果不是,則獲得當前map實例中大於lo的最小的鍵值對。
       * 如果得到的結果e超過了範圍的上限,那麼返回null。
       */
      final TreeMap.Entry<K,V> absLowest() {
          TreeMap.Entry<K,V> e =
              (fromStart ?  m.getFirstEntry() :
               (loInclusive ? m.getCeilingEntry(lo) :
                              m.getHigherEntry(lo)));
          return (e == null || tooHigh(e.key)) ? null : e;
      }
      // 與absLowest()相反
      final TreeMap.Entry<K,V> absHighest() {
          TreeMap.Entry<K,V> e =
              (toEnd ?  m.getLastEntry() :
               (hiInclusive ?  m.getFloorEntry(hi) :
                               m.getLowerEntry(hi)));
          return (e == null || tooLow(e.key)) ? null : e;
      }
      // 下面的邏輯就都很簡單了,注意會先判斷key是否越界,
      // 如果越界就返回絕對值。
      final TreeMap.Entry<K,V> absCeiling(K key) {
          if (tooLow(key))
              return absLowest();
          TreeMap.Entry<K,V> e = m.getCeilingEntry(key);
          return (e == null || tooHigh(e.key)) ? null : e;
      }
      final TreeMap.Entry<K,V> absHigher(K key) {
          if (tooLow(key)) 
              return absLowest();
          TreeMap.Entry<K,V> e = m.getHigherEntry(key);
          return (e == null || tooHigh(e.key)) ? null : e;
      }
      final TreeMap.Entry<K,V> absFloor(K key) {
          if (tooHigh(key))
              return absHighest();
          TreeMap.Entry<K,V> e = m.getFloorEntry(key);
          return (e == null || tooLow(e.key)) ? null : e;
      }
      final TreeMap.Entry<K,V> absLower(K key) {
          if (tooHigh(key))
              return absHighest();
          TreeMap.Entry<K,V> e = m.getLowerEntry(key);
          return (e == null || tooLow(e.key)) ? null : e;
      }
      /** 返回升序遍歷的絕對上限 */
      final TreeMap.Entry<K,V> absHighFence() {
          return (toEnd ? null : (hiInclusive ?
                                  m.getHigherEntry(hi) :
                                  m.getCeilingEntry(hi)));
      }
      /** 返回降序遍歷的絕對下限 */
      final TreeMap.Entry<K,V> absLowFence() {
          return (fromStart ? null : (loInclusive ?
                                      m.getLowerEntry(lo) :
                                      m.getFloorEntry(lo)));
      }
      // 剩下的就是實現NavigableMap的方法以及一些抽象方法
// 和NavigableSubMap中的集合視圖函數。
      // 大部分操作都是靠當前實例map的方法和上述用於判斷邊界的方法提供支持
      .....
  }

一個局部視圖最重要的是要能夠判斷出傳入的key是否屬於該視圖的範圍內,在上面的代碼中可以發現NavigableSubMap提供了非常多的輔助函數用於判斷範圍,接下來我們看看NavigableSubMap的迭代器是如何實現的:


/**
 * Iterators for SubMaps
 */
abstract class SubMapIterator<T> implements Iterator<T> {
    TreeMap.Entry<K,V> lastReturned;
    TreeMap.Entry<K,V> next;
    final Object fenceKey;
    int expectedModCount;
    SubMapIterator(TreeMap.Entry<K,V> first,
                   TreeMap.Entry<K,V> fence) {
        expectedModCount = m.modCount; 
        lastReturned = null;
        next = first;
        // UNBOUNDED是一個虛擬值(一個Object對象),表示無邊界。
        fenceKey = fence == null ? UNBOUNDED : fence.key;
    }
    // 只要next不爲null並且沒有超過邊界
    public final boolean hasNext() {
        return next != null && next.key != fenceKey;
    }
    final TreeMap.Entry<K,V> nextEntry() {
        TreeMap.Entry<K,V> e = next;
        // 已經遍歷到頭或者越界了
        if (e == null || e.key == fenceKey)
            throw new NoSuchElementException();
        // modCount是一個記錄操作數的計數器
        // 如果與expectedModCount不一致
        // 則代表當前map實例在遍歷過程中已被修改過了(從其他線程)
        if (m.modCount != expectedModCount)
            throw new ConcurrentModificationException();
        // 向後移動next指針
        // successor()返回指定節點的繼任者
        // 它是節點e的右子樹的最左節點
        // 也就是比e大的最小的節點
        // 如果e沒有右子樹,則會試圖向上尋找
        next = successor(e);
        lastReturned = e; // 記錄最後返回的節點
        return e;
    }
    final TreeMap.Entry<K,V> prevEntry() {
        TreeMap.Entry<K,V> e = next;
        if (e == null || e.key == fenceKey)
            throw new NoSuchElementException();
        if (m.modCount != expectedModCount)
            throw new ConcurrentModificationException();
        // 向前移動next指針
        // predecessor()返回指定節點的前任
        // 它與successor()邏輯相反。
        next = predecessor(e);
        lastReturned = e;
        return e;
    }
    final void removeAscending() {
        if (lastReturned == null)
            throw new IllegalStateException();
        if (m.modCount != expectedModCount)
            throw new ConcurrentModificationException();
        // 被刪除的節點被它的繼任者取代
        // 執行完刪除後,lastReturned實際指向了它的繼任者
        if (lastReturned.left != null && lastReturned.right != null)
            next = lastReturned;
        m.deleteEntry(lastReturned);
        lastReturned = null;
        expectedModCount = m.modCount;
    }
    final void removeDescending() {
        if (lastReturned == null)
            throw new IllegalStateException();
        if (m.modCount != expectedModCount)
            throw new ConcurrentModificationException();
        m.deleteEntry(lastReturned);
        lastReturned = null;
        expectedModCount = m.modCount;
    }
}
final class SubMapEntryIterator extends SubMapIterator<Map.Entry<K,V>> {
    SubMapEntryIterator(TreeMap.Entry<K,V> first,
                        TreeMap.Entry<K,V> fence) {
        super(first, fence);
    }
    public Map.Entry<K,V> next() {
        return nextEntry();
    }
    public void remove() {
        removeAscending();
    }
}
final class DescendingSubMapEntryIterator extends SubMapIterator<Map.Entry<K,V>> {
    DescendingSubMapEntryIterator(TreeMap.Entry<K,V> last,
                                  TreeMap.Entry<K,V> fence) {
        super(last, fence);
    }
    public Map.Entry<K,V> next() {
        return prevEntry();
    }
    public void remove() {
        removeDescending();
    }
}

到目前爲止,我們已經針對集合視圖討論了許多,想必大家也能夠理解集合視圖的概念了,由於SortedMap與NavigableMap的緣故,TreeMap中的集合視圖是非常多的,包括各種局部視圖和不同排序的視圖,有興趣的讀者可以自己去看看源碼,後面的內容不會再對集合視圖進行過多的解釋了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章