天天看點

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

原文出處: SylvanasSun's Blog 紅黑樹: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();
} 
           

NavigableMap

然後是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都是不同的。
Map源碼分析:TreeMap( jdk 1.8 )和紅黑樹

盡管二叉查找樹看起來很美好,但事與願違,二叉查找樹在極端情況下會變得并不是那麼有效率,假設我們有一個有序的整數序列:

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中的集合視圖是非常多的,包括各種局部視圖和不同排序的視圖,有興趣的讀者可以自己去看看源碼,後面的内容不會再對集合視圖進行過多的解釋了。

繼續閱讀