歡迎關注個人公衆號「彤哥讀源碼」,查看更多源碼系列文章, 與彤哥一塊兒暢遊源碼的海洋。java
TreeMap使用紅黑樹存儲元素,能夠保證元素按key值的大小進行遍歷。git
TreeMap實現了Map、SortedMap、NavigableMap、Cloneable、Serializable等接口。app
SortedMap規定了元素能夠按key的大小來遍歷,它定義了一些返回部分map的方法。ui
public interface SortedMap<K,V> extends Map<K,V> {
// key的比較器
Comparator<? super K> comparator();
// 返回fromKey(包含)到toKey(不包含)之間的元素組成的子map
SortedMap<K,V> subMap(K fromKey, K toKey);
// 返回小於toKey(不包含)的子map
SortedMap<K,V> headMap(K toKey);
// 返回大於等於fromKey(包含)的子map
SortedMap<K,V> tailMap(K fromKey);
// 返回最小的key
K firstKey();
// 返回最大的key
K lastKey();
// 返回key集合
Set<K> keySet();
// 返回value集合
Collection<V> values();
// 返回節點集合
Set<Map.Entry<K, V>> entrySet();
}
複製代碼
NavigableMap是對SortedMap的加強,定義了一些返回離目標key最近的元素的方法。this
public interface NavigableMap<K,V> extends SortedMap<K,V> {
// 小於給定key的最大節點
Map.Entry<K,V> lowerEntry(K key);
// 小於給定key的最大key
K lowerKey(K key);
// 小於等於給定key的最大節點
Map.Entry<K,V> floorEntry(K key);
// 小於等於給定key的最大key
K floorKey(K key);
// 大於等於給定key的最小節點
Map.Entry<K,V> ceilingEntry(K key);
// 大於等於給定key的最小key
K ceilingKey(K key);
// 大於給定key的最小節點
Map.Entry<K,V> higherEntry(K key);
// 大於給定key的最小key
K higherKey(K key);
// 最小的節點
Map.Entry<K,V> firstEntry();
// 最大的節點
Map.Entry<K,V> lastEntry();
// 彈出最小的節點
Map.Entry<K,V> pollFirstEntry();
// 彈出最大的節點
Map.Entry<K,V> pollLastEntry();
// 返回倒序的map
NavigableMap<K,V> descendingMap();
// 返回有序的key集合
NavigableSet<K> navigableKeySet();
// 返回倒序的key集合
NavigableSet<K> descendingKeySet();
// 返回從fromKey到toKey的子map,是否包含起止元素能夠本身決定
NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive);
// 返回小於toKey的子map,是否包含toKey本身決定
NavigableMap<K,V> headMap(K toKey, boolean inclusive);
// 返回大於fromKey的子map,是否包含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);
}
複製代碼
TreeMap只使用到了紅黑樹,因此它的時間複雜度爲O(log n),咱們再來回顧一下紅黑樹的特性。spa
(1)每一個節點或者是黑色,或者是紅色。指針
(2)根節點是黑色。code
(3)每一個葉子節點(NIL)是黑色。(注意:這裏葉子節點,是指爲空(NIL或NULL)的葉子節點!)排序
(4)若是一個節點是紅色的,則它的子節點必須是黑色的。繼承
(5)從一個節點到該節點的子孫節點的全部路徑上包含相同數目的黑節點。
/** * 比較器,若是沒傳則key要實現Comparable接口 */
private final Comparator<? super K> comparator;
/** * 根節點 */
private transient Entry<K,V> root;
/** * 元素個數 */
private transient int size = 0;
/** * 修改次數 */
private transient int modCount = 0;
複製代碼
(1)comparator
按key的大小排序有兩種方式,一種是key實現Comparable接口,一種方式經過構造方法傳入比較器。
(2)root
根節點,TreeMap沒有桶的概念,全部的元素都存儲在一顆樹中。
存儲節點,典型的紅黑樹結構。
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;
}
複製代碼
/** * 默認構造方法,key必須實現Comparable接口 */
public TreeMap() {
comparator = null;
}
/** * 使用傳入的comparator比較兩個key的大小 */
public TreeMap(Comparator<? super K> comparator) {
this.comparator = comparator;
}
/** * key必須實現Comparable接口,把傳入map中的全部元素保存到新的TreeMap中 */
public TreeMap(Map<? extends K, ? extends V> m) {
comparator = null;
putAll(m);
}
/** * 使用傳入map的比較器,並把傳入map中的全部元素保存到新的TreeMap中 */
public TreeMap(SortedMap<K, ? extends V> m) {
comparator = m.comparator();
try {
buildFromSorted(m.size(), m.entrySet().iterator(), null, null);
} catch (java.io.IOException cannotHappen) {
} catch (ClassNotFoundException cannotHappen) {
}
}
複製代碼
構造方法主要分紅兩類,一類是使用comparator比較器,一類是key必須實現Comparable接口。
其實,筆者認爲這兩種比較方式能夠合併成一種,當沒有傳comparator的時候,能夠用如下方式來給comparator賦值,這樣後續全部的比較操做均可以使用同樣的邏輯處理了,而不用每次都檢查comparator爲空的時候又用Comparable來實現一遍邏輯。
// 若是comparator爲空,則key必須實現Comparable接口,因此這裏確定能夠強轉
// 這樣在構造方法中統一替換掉,後續的邏輯就都一致了
comparator = (k1, k2) -> ((Comparable<? super K>)k1).compareTo(k2);
複製代碼
獲取元素,典型的二叉查找樹的查找方法。
public V get(Object key) {
// 根據key查找元素
Entry<K,V> p = getEntry(key);
// 找到了返回value值,沒找到返回null
return (p==null ? null : p.value);
}
final Entry<K,V> getEntry(Object key) {
// 若是comparator不爲空,使用comparator的版本獲取元素
if (comparator != null)
return getEntryUsingComparator(key);
// 若是key爲空返回空指針異常
if (key == null)
throw new NullPointerException();
// 將key強轉爲Comparable
@SuppressWarnings("unchecked")
Comparable<? super K> k = (Comparable<? super K>) key;
// 從根元素開始遍歷
Entry<K,V> p = root;
while (p != null) {
int cmp = k.compareTo(p.key);
if (cmp < 0)
// 若是小於0從左子樹查找
p = p.left;
else if (cmp > 0)
// 若是大於0從右子樹查找
p = p.right;
else
// 若是相等說明找到了直接返回
return p;
}
// 沒找到返回null
return null;
}
final Entry<K,V> getEntryUsingComparator(Object key) {
@SuppressWarnings("unchecked")
K k = (K) key;
Comparator<? super K> cpr = comparator;
if (cpr != null) {
// 從根元素開始遍歷
Entry<K,V> p = root;
while (p != null) {
int cmp = cpr.compare(k, p.key);
if (cmp < 0)
// 若是小於0從左子樹查找
p = p.left;
else if (cmp > 0)
// 若是大於0從右子樹查找
p = p.right;
else
// 若是相等說明找到了直接返回
return p;
}
}
// 沒找到返回null
return null;
}
複製代碼
(1)從root遍歷整個樹;
(2)若是待查找的key比當前遍歷的key小,則在其左子樹中查找;
(3)若是待查找的key比當前遍歷的key大,則在其右子樹中查找;
(4)若是待查找的key與當前遍歷的key相等,則找到了該元素,直接返回;
(5)從這裏能夠看出是否有comparator分化成了兩個方法,可是內部邏輯如出一轍,所以可見筆者comparator = (k1, k2) -> ((Comparable<? super K>)k1).compareTo(k2);
這種改造的必要性。
我是一條美麗的分割線,前方高能,請作好準備。
(1)每一個節點或者是黑色,或者是紅色。
(2)根節點是黑色。
(3)每一個葉子節點(NIL)是黑色。(注意:這裏葉子節點,是指爲空(NIL或NULL)的葉子節點!)
(4)若是一個節點是紅色的,則它的子節點必須是黑色的。
(5)從一個節點到該節點的子孫節點的全部路徑上包含相同數目的黑節點。
左旋,就是以某個節點爲支點向左旋轉。
整個左旋過程以下:
(1)將 y的左節點 設爲 x的右節點,即將 β 設爲 x的右節點;
(2)將 x 設爲 y的左節點的父節點,即將 β的父節點 設爲 x;
(3)將 x的父節點 設爲 y的父節點;
(4)若是 x的父節點 爲空節點,則將y設置爲根節點;若是x是它父節點的左(右)節點,則將y設置爲x父節點的左(右)節點;
(5)將 x 設爲 y的左節點;
(6)將 x的父節點 設爲 y;
讓咱們來看看TreeMap中的實現:
/** * 以p爲支點進行左旋 * 假設p爲圖中的x */
private void rotateLeft(Entry<K,V> p) {
if (p != null) {
// p的右節點,即y
Entry<K,V> r = p.right;
// (1)將 y的左節點 設爲 x的右節點
p.right = r.left;
// (2)將 x 設爲 y的左節點的父節點(若是y的左節點存在的話)
if (r.left != null)
r.left.parent = p;
// (3)將 x的父節點 設爲 y的父節點
r.parent = p.parent;
// (4)...
if (p.parent == null)
// 若是 x的父節點 爲空,則將y設置爲根節點
root = r;
else if (p.parent.left == p)
// 若是x是它父節點的左節點,則將y設置爲x父節點的左節點
p.parent.left = r;
else
// 若是x是它父節點的右節點,則將y設置爲x父節點的右節點
p.parent.right = r;
// (5)將 x 設爲 y的左節點
r.left = p;
// (6)將 x的父節點 設爲 y
p.parent = r;
}
}
複製代碼
右旋,就是以某個節點爲支點向右旋轉。
整個右旋過程以下:
(1)將 x的右節點 設爲 y的左節點,即 將 β 設爲 y的左節點;
(2)將 y 設爲 x的右節點的父節點,即 將 β的父節點 設爲 y;
(3)將 y的父節點 設爲 x的父節點;
(4)若是 y的父節點 是 空節點,則將x設爲根節點;若是y是它父節點的左(右)節點,則將x設爲y的父節點的左(右)節點;
(5)將 y 設爲 x的右節點;
(6)將 y的父節點 設爲 x;
讓咱們來看看TreeMap中的實現:
/** * 以p爲支點進行右旋 * 假設p爲圖中的y */
private void rotateRight(Entry<K,V> p) {
if (p != null) {
// p的左節點,即x
Entry<K,V> l = p.left;
// (1)將 x的右節點 設爲 y的左節點
p.left = l.right;
// (2)將 y 設爲 x的右節點的父節點(若是x有右節點的話)
if (l.right != null) l.right.parent = p;
// (3)將 y的父節點 設爲 x的父節點
l.parent = p.parent;
// (4)...
if (p.parent == null)
// 若是 y的父節點 是 空節點,則將x設爲根節點
root = l;
else if (p.parent.right == p)
// 若是y是它父節點的右節點,則將x設爲y的父節點的右節點
p.parent.right = l;
else
// 若是y是它父節點的左節點,則將x設爲y的父節點的左節點
p.parent.left = l;
// (5)將 y 設爲 x的右節點
l.right = p;
// (6)將 y的父節點 設爲 x
p.parent = l;
}
}
複製代碼
未完待續,下一節咱們一塊兒探討紅黑樹插入元素的操做。
如今公衆號文章沒辦法留言了,若是有什麼疑問或者建議請直接在公衆號給我留言。
歡迎關注個人公衆號「彤哥讀源碼」,查看更多源碼系列文章, 與彤哥一塊兒暢遊源碼的海洋。