TreeMap
- TreeMap结构最主要的功能是在存储的时候可以按照key进行顺序存储。
- TreeMap底层的数据结构是红黑树,和HashMap的红黑树结构一致,不同之处,TreeMap利用了,红黑树左节点小,右节点大的性质,根据key进行排序,使每个元素能够插入到红黑树大小适当的位置,维护了key的大小关系,适用于key需要排序的场景。
一、内部属性
- 代码
//比较器,如果外部有传进来 Comparator 比较器,首先用外部的
//如果外部比较器为空,则使用 key 自己实现的 Comparable#compareTo 方法
//比较手段和上面日常工作中的比较 demo 是一致的
private final Comparator<? super K> comparator;
//红黑树的根节点
private transient Entry<K,V> root;
//红黑树的根节点
private transient int size = 0;
//错误机制和hashmap一致,用于判断在迭代过程中,是否存在树结构改变,改变就直接失败
private transient int modCount = 0;
- 属性中注意点
- 错误机制
- 内部包含比较器,通过后面得知,树的排序是通过比较器进行判断的,同时,这个涉及也极大增强了代码的灵活性,没有将比较设置死,可以实现自己的比较器,作出符合自己的排序规则。
- transient:在序列化时,不会对其进行序列化,因为节点关系,可以推导出头结点以及size大小
二、增、删,查
1.增加节点
- 过程
1. 判断是否存在节点 |->没有节点 |->将当前节点置为头结点,返回null |->存在节点 |->存在外部比较器 |->获取到父节点parent,节点所处父节点的位置cmp |->不存在外部比较器 |->获取到父节点parent,节点所处父节点的位置cmp |->创建新节点 |->根据父节点parent和位置cmp,插入树中 |->红黑树调整 |->结构变化,modCount改变
- 代码
public V put(K key, V value) { //记录上一次节点 Entry<K,V> t = root; if (t == null) { //此处用来检验是否是null,说明空key是不允许的 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 { if (key == null) throw new NullPointerException(); @SuppressWarnings("unchecked") 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; }
2.删除节点
- 过程
//P:要删除的节点,S:P的后继节点 ,R:替代节点的后继节点或者删除节点的孩子节点
|-> 如果要删除的节点P,有左孩子和右孩子--(1)
|-> 1. 获取删除节点的后继节点
|-> 2. 将后继(替代)节点S,赋值给要删除的节点(TreeMap中存在两个相同key,value的“后继”节点)--- 后面肯定是删除后继节点
|-> 获取R节点
|-> 要删除的节点P,只有一个孩子节点,付给R
|-> 走了上面的(1),此时的P为替代节点
|-> 如果P是叶子节点,R为null
|-> 如果P不是叶子节点,则R为S的后继节点
|-> 针对不同的形式进行删除
- 代码
public V remove(Object key) { Entry<K,V> p = getEntry(key); if (p == null) return null; V oldValue = p.value; deleteEntry(p); return oldValue; }
/** * 删除节点p且平衡树 * 对于有两个子节点的删除节点p,使用右子树的最小节点替换, * 然后根据这个最小节点是叶子结点就先修复再删除,若有一个右子树就使用右子树中节点与父节点连接 * 对于有一个子节点的删除节点p,使用删除节点的子节点与父节点连接 * 对于没右子节点就修复后再删除 * @param p */ private void deleteEntry(Entry<K,V> p) { modCount++; size--; if (p.left != null && p.right != null) { //1. 获取当前节点的后继节点(有玩头) Entry<K,V> s = successor(p); //2. 将当前节点的key和value更改为后继节点的key和value p.key = s.key; p.value = s.value; //3. 将p节点指向要删除节点的后继节点 p = s; } // p has 2 children //----------------调整后继节点后面的节点结构-------------------- // 4. 如果删除节点只有一个孩子就返回这个孩子。注意如果进入上一个if,返回的p如果是叶节点,即replacement=null // p如果不是叶节点,只会存在右子节点 Entry<K,V> replacement = (p.left != null ? p.left : p.right); //5. 如果当前节点后面,存在节点,获取后面节点中,最小的节点 if (replacement != null) { //5.1 让后面的第一个节点,向上变化,更改为当前删除节点的位置 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; //5.2 删除要删除的节点,只需断开连接即可 p.left = p.right = p.parent = null; // Fix replacement if (p.color == BLACK) fixAfterDeletion(replacement); //6. 当前节点为根节点,则直接将根节点置为null } else if (p.parent == null) { // return if we are the only node. root = null; //7. 当前节点,不存在子节点 } else { // No children. Use self as phantom replacement and unlink. if (p.color == BLACK) fixAfterDeletion(p); //解除p的父节点对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对p父节点的引用 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; } }
- 后继节点
3.查询节点
- 过程
获取外部比较器
|-> 如果存在
|-> 使用外部比较器
|-> 不存在
|-> 获取key的比较器
|-> 从根节点开始,根据比较器进行查询
- 代码
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)
// 如果比较器为空,只是用key作为比较器查询
return getEntryUsingComparator(key);
if (key == null)
throw new NullPointerException();
Comparable<? super K> k = (Comparable<? super K>) key;
// 取得root节点
Entry<K,V> p = root;
// 从root节点开始查找,根据比较器判断是在左子树还是右子树
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;
}
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;
}
优秀的文章:https://blog.csdn.net/mbmispig/article/details/78750405