标签:style blog http color java os io strong for
注:以下源码基于jdk1.7.0_11
public class TreeMap<K,V> extends AbstractMap<K,V> implements NavigableMap<K,V>, Cloneable, java.io.Serializable
private final Comparator<? super K> comparator;//比较器 private transient Entry<K,V> root = null;//树根 /** * The number of entries in the tree */ private transient int size = 0;//大小 /** * The number of structural modifications to the tree. */ private transient int modCount = 0;//修改次数
public TreeMap() { comparator = null;//比较器为空 } public TreeMap(Comparator<? super K> comparator) {//传入比较器 this.comparator = comparator; } public TreeMap(Map<? extends K, ? extends V> m) { comparator = null; putAll(m); } 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) { } }
static final class Entry<K,V> implements Map.Entry<K,V> { K key;//键 V value;//值 Entry<K,V> left = null;//左孩子 Entry<K,V> right = null;//右孩子 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; } public K getKey() { return key; } public V getValue() { return value; } 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 put(K key, V value) {//向红黑树中插入键值对 Entry<K,V> t = root; if (t == null) {//如果树为空 compare(key, key); // type (and possibly null) check root = new Entry<>(key, value, null); size = 1; modCount++; return null; } int cmp; Entry<K,V> parent;//父结点 // split comparator and comparable paths Comparator<? super K> cpr = comparator; if (cpr != null) {//优先通过比较器比较两个结点的大小 do { parent = t; cmp = cpr.compare(key, t.key); if (cmp < 0)//待插入结点小于当前结点 t = t.left;//进入左子树 else if (cmp > 0)//待插入结点大于当前结点 t = t.right;//进入右子树 else//当前结点等于待插入结点,覆盖原值 return t.setValue(value); } while (t != null); } else {//如果没有定义比较器,那么key必须实现Comparable接口 if (key == null)//不允许空键 throw new NullPointerException(); Comparable<? super K> k = (Comparable<? super K>) key; do { parent = t; cmp = k.compareTo(t.key); if (cmp < 0) t = t.left;//进入左子树 else if (cmp > 0) t = t.right;//进入右子树 else return t.setValue(value);//覆盖原值 } while (t != null); } //找到插入点之后,创建新结点,插入之。 Entry<K,V> e = new Entry<>(key, value, parent); if (cmp < 0)//判断是挂到左边还是右边 parent.left = e; else parent.right = e; fixAfterInsertion(e);//进行着色和旋转等操作修复红黑树 size++; modCount++; return null; }
public V get(Object key) {//查询操作 Entry<K,V> p = getEntry(key); return (p==null ? null : p.value); }
final Entry<K,V> getEntry(Object key) {//跟普通二叉排序树的查询操作一致 // Offload comparator-based version for sake of performance if (comparator != null)//存在比较器 return getEntryUsingComparator(key);//根据比较器定义的比较规则查找 if (key == null) throw new NullPointerException(); Comparable<? super K> k = (Comparable<? super K>) key; Entry<K,V> p = root; while (p != null) {//根据Comparable接口定义的比较规则查找 int cmp = k.compareTo(p.key); if (cmp < 0)//待查结点在左子树 p = p.left; else if (cmp > 0)//待查结点在右子树 p = p.right; else return p; } return null;//没找到 } final Entry<K,V> getEntryUsingComparator(Object key) {//根据比较器定义的比较规则查找 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) p = p.left; else if (cmp > 0) p = p.right; else return p; } } return null; }
public V remove(Object key) { Entry<K,V> p = getEntry(key);//首先找到待删结点 if (p == null) return null; V oldValue = p.value; deleteEntry(p);//删除结点 return oldValue; }
private void deleteEntry(Entry<K,V> p) {//删除一个结点 modCount++; size--; // If strictly internal, copy successor's element to p and then make p // point to successor. if (p.left != null && p.right != null) {//p的左右孩子都存在 Entry<K,V> s = successor(p);//找到p的直接后继(顺着p右子树一直向左) p.key = s.key;//用直接后继替代p p.value = s.value; p = s; } // p has 2 children //下面操作将释放s结点,并修复红黑树 // Start fixup at replacement node, if it exists. Entry<K,V> replacement = (p.left != null ? p.left : p.right); if (replacement != null) { // Link replacement to parent replacement.parent = p.parent; if (p.parent == null) root = replacement; else if (p == p.parent.left) p.parent.left = replacement; else p.parent.right = replacement; // Null out links so they are OK to use by fixAfterDeletion. p.left = p.right = p.parent = null; // Fix replacement if (p.color == BLACK) fixAfterDeletion(replacement);//修复红黑树 } else if (p.parent == null) { // return if we are the only node. root = null; } else { // No children. Use self as phantom replacement and unlink. if (p.color == BLACK) fixAfterDeletion(p); if (p.parent != null) { if (p == p.parent.left) p.parent.left = null; else if (p == p.parent.right) p.parent.right = null; p.parent = null; } } }
static <K,V> TreeMap.Entry<K,V> successor(Entry<K,V> t) {//查找中序后继 if (t == null) return null; else if (t.right != null) {//如果存在右子树 Entry<K,V> p = t.right; while (p.left != null)//顺着右子树,向左搜索 p = p.left; return p; } else {//如果不存在右子树 Entry<K,V> p = t.parent;//顺着父亲,向上搜索 Entry<K,V> ch = t; while (p != null && ch == p.right) {//如果当前结点是父结点的右孩子,那么继续向上 ch = p; p = p.parent; } return p; } }
static <K,V> Entry<K,V> predecessor(Entry<K,V> t) { if (t == null) return null; else if (t.left != null) {//若存在左子树 Entry<K,V> p = t.left; while (p.right != null)//顺着左子树,向右搜索 p = p.right; return p; } else { Entry<K,V> p = t.parent; Entry<K,V> ch = t; while (p != null && ch == p.left) { ch = p; p = p.parent; } return p; } }
标签:style blog http color java os io strong for
原文地址:http://blog.csdn.net/chdjj/article/details/38782221