前言
上一篇我们研究了treeifyBin方法,发现在链表长度超过8并且节点数组长度超过64时,HashMap会将链表转化成一条双向链表,再将这条双向链表转化成一棵红黑树。这一篇文章我们就一起来看下HashMap是如何将双向链表转化成红黑树的。
正文
1、开始
我们按照上一篇的测试案例,进入treeify方法:
/**
* Forms tree of the nodes linked from this node.
* @return root of tree
*/
final void treeify(Node<K,V>[] tab) {
TreeNode<K,V> root = null;
for (TreeNode<K,V> x = this, next; x != null; x = next) {
next = (TreeNode<K,V>)x.next;
x.left = x.right = null;
if (root == null) {
x.parent = null;
x.red = false;
root = x;
}
else {
K k = x.key;
int h = x.hash;
Class<?> kc = null;
for (TreeNode<K,V> p = root;;) {
int dir, ph;
K pk = p.key;
if ((ph = p.hash) > h)
dir = -1;
else if (ph < h)
dir = 1;
else if ((kc == null &&
(kc = comparableClassFor(k)) == null) ||
(dir = compareComparables(kc, k, pk)) == 0)
dir = tieBreakOrder(k, pk);
TreeNode<K,V> xp = p;
if ((p = (dir <= 0) ? p.left : p.right) == null) {
x.parent = xp;
if (dir <= 0)
xp.left = x;
else
xp.right = x;
root = balanceInsertion(root, x);
break;
}
}
}
}
moveRootToFront(tab, root);
}
说明:“双向链表”的树节点依次有:(“ssm”,4)、(“hej”,4)、(“eck”,4)、(“qso”,4)、(“hderm”,4)、(“hderni”,4)、(“eow”,4)、(“xdy”,4)、(“lyz”,4)。
2、插入根节点
首先,定义一个初始值为null的树节点root 。
TreeNode<K,V> root = null;
接着进入了一个for循环,沿着链表依次获取树节点,先拿到调用当前方法的树节点,赋值给root,使root成为一个黑色根节点(“ssm”,4):
if (root == null) {
x.parent = null;
x.red = false;
root = x;
}
3、插入其他节点
再依次找到链表上的其他树节点,执行以下代码:
K k = x.key;
int h = x.hash;
Class<?> kc = null;
for (TreeNode<K,V> p = root;;) {
int dir, ph;
K pk = p.key;
if ((ph = p.hash) > h)
dir = -1;
else if (ph < h)
dir = 1;
else if ((kc == null &&
(kc = comparableClassFor(k)) == null) ||
(dir = compareComparables(kc, k, pk)) == 0)
dir = tieBreakOrder(k, pk);
TreeNode<K,V> xp = p;
if ((p = (dir <= 0) ? p.left : p.right) == null) {
x.parent = xp;
if (dir <= 0)
xp.left = x;
else
xp.right = x;
root = balanceInsertion(root, x);
break;
}
}
我们看下这段代码做了什么:
1、定义类型为K的k变量,初始值为当前树节点的key
2、定义类型为int的h变量,初始值为当前树节点的hash成员属性
3、定义类型为Class<?>的kc变量,初始值为null
4、进入for循环:
for (TreeNode<K,V> p = root;;) {
int dir, ph;
K pk = p.key;
if ((ph = p.hash) > h)
dir = -1;
else if (ph < h)
dir = 1;
else if ((kc == null &&
(kc = comparableClassFor(k)) == null) ||
(dir = compareComparables(kc, k, pk)) == 0)
dir = tieBreakOrder(k, pk);
TreeNode<K,V> xp = p;
if ((p = (dir <= 0) ? p.left : p.right) == null) {
x.parent = xp;
if (dir <= 0)
xp.left = x;
else
xp.right = x;
root = balanceInsertion(root, x);
break;
}
}
对这段for循环代码进行分析:
4.1、定义了两个类型为int的变量dir,ph,未初始化
4.2、定义了一个类型为K的变量pk,设置为树节点p的key
4.3、比较当前树节点的hash值与根节点的hash值,如果根节点的hash值大于当前树节点的hash值,则dir值为-1,如果根节点的hash值小于当前树节点的hash值,则dir值为1。如果二者相等,进行判断:
(kc == null &&
(kc = comparableClassFor(k)) == null) ||
(dir = compareComparables(kc, k, pk)) == 0
这里的判断调用了comparableClassFor方法和compareComparables方法,我们将判断分解后得知,
(kc == null && (kc = comparableClassFor(k)) == null)与(dir = compareComparables(kc, k, pk)) == 0这两个判断中只要其中任意一个判断成立,则该判断成立。
我们先看(kc == null && (kc = comparableClassFor(k)) == null),这里调用了comparableClassFor方法:
/**
* Returns x's Class if it is of the form "class C implements
* Comparable<C>", else null.
*/
static Class<?> comparableClassFor(Object x) {
if (x instanceof Comparable) {
Class<?> c; Type[] ts, as; Type t; ParameterizedType p;
if ((c = x.getClass()) == String.class) // bypass checks
return c;
if ((ts = c.getGenericInterfaces()) != null) {
for (int i = 0; i < ts.length; ++i) {
if (((t = ts[i]) instanceof ParameterizedType) &&
((p = (ParameterizedType)t).getRawType() ==
Comparable.class) &&
(as = p.getActualTypeArguments()) != null &&
as.length == 1 && as[0] == c) // type arg is c
return c;
}
}
}
return null;
}
如果对象是实现了Comparable接口的类,则返回这个类的Class,否则返回null。
再看(dir = compareComparables(kc, k, pk)) == 0,这里调用了compareComparables方法:
/**
* Returns k.compareTo(x) if x matches kc (k's screened comparable
* class), else 0.
*/
@SuppressWarnings({"rawtypes","unchecked"}) // for cast to Comparable
static int compareComparables(Class<?> kc, Object k, Object x) {
return (x == null || x.getClass() != kc ? 0 :
((Comparable)k).compareTo(x));
}
判断当前树节点的key不为空并且根节点的key的类型与当前树节点key的类型相同时,返回根节点的key与当前树节点的key的差值,否则返回0。
这两个判断表达式都分析结束后,回到原判断,当判断成立时,调用tieBreakOrder方法:
dir = tieBreakOrder(k, pk);
我们看下tieBreakOrder方法:
/**
* Tie-breaking utility for ordering insertions when equal
* hashCodes and non-comparable. We don't require a total
* order, just a consistent insertion rule to maintain
* equivalence across rebalancings. Tie-breaking further than
* necessary simplifies testing a bit.
*/
static int tieBreakOrder(Object a, Object b) {
int d;
if (a == null || b == null ||
(d = a.getClass().getName().
compareTo(b.getClass().getName())) == 0)
d = (System.identityHashCode(a) <= System.identityHashCode(b) ?
-1 : 1);
return d;
}
打破实用程序,用于在相等hashCodes和不可比较的情况下对插入进行排序。我们不需要总订单,只需要一致的插入规则来维持重新平衡的等价性。超出必要条件的打破简化了测试。
- 小贴士:不管有没有重写hashCode方法,System.identityHashCode永远返回根据对象物理内存地址产生的hash值。
如果对象a与对象b中间任意一个为空,或者对象a的类名与对象b的类名差值为0时,返回(System.identityHashCode(a) <= System.identityHashCode(b) ?-1 : 1)的值。
最后该值会被赋给dir。
回到标记为4的for循环,这时dir值已确定。
4.4、定义一个树节点xp,设值为p树节点。
4.5、进入if-else代码块:
if ((p = (dir <= 0) ? p.left : p.right) == null) {
x.parent = xp;
if (dir <= 0)
xp.left = x;
else
xp.right = x;
root = balanceInsertion(root, x);
break;
}
4.5.1、如果dir≤0,将p树节点的左节点赋给p,否则将p树节点的右节点赋给p,最后判断p是否为空
4.5.2、若不为空,直接开始下一次循环。
若为空,则将xp树节点赋给当前树节点的父节点,并根据dir是否大于0来确定当前树节点将成为xp树节点的左节点还是右节点。
4.5.3、最后,调用了balanceInsertion方法:
static <K,V> TreeNode<K,V> balanceInsertion(TreeNode<K,V> root,
TreeNode<K,V> x) {
x.red = true;
for (TreeNode<K,V> xp, xpp, xppl, xppr;;) {
if ((xp = x.parent) == null) {
x.red = false;
return x;
}
else if (!xp.red || (xpp = xp.parent) == null)
return root;
if (xp == (xppl = xpp.left)) {
if ((xppr = xpp.right) != null && xppr.red) {
xppr.red = false;
xp.red = false;
xpp.red = true;
x = xpp;
}
else {
if (x == xp.right) {
root = rotateLeft(root, x = xp);
xpp = (xp = x.parent) == null ? null : xp.parent;
}
if (xp != null) {
xp.red = false;
if (xpp != null) {
xpp.red = true;
root = rotateRight(root, xpp);
}
}
}
}
else {
if (xppl != null && xppl.red) {
xppl.red = false;
xp.red = false;
xpp.red = true;
x = xpp;
}
else {
if (x == xp.left) {
root = rotateRight(root, x = xp);
xpp = (xp = x.parent) == null ? null : xp.parent;
}
if (xp != null) {
xp.red = false;
if (xpp != null) {
xpp.red = true;
root = rotateLeft(root, xpp);
}
}
}
}
}
}
我们看下balanceInsertion方法做了什么:
4.5.3.1、将当前树节点设为红色节点
4.5.3.2、定义类型为TreeNode的树节点变量xp, xpp, xppl, xppr,并进入for循环
里边由许多if-else代码块组成。
4.5.3.2.1、第一个if-else代码块为:
if ((xp = x.parent) == null) {
x.red = false;
return x;
}
else if (!xp.red || (xpp = xp.parent) == null)
return root;
这里将当前树节点的父节点赋值给xp,并判断xp是否为空,若为空,就将当前树节点设为黑色节点。
若不为空,则继续判断,xp节点是否为黑色节点,或者xp的父节点赋值给xpp并判断xpp是否为空,两个判断其中任何一个成立则返回root节点。
- 这里体现了红黑树在插入树节点时的情形一与情形二:见《红黑树(Red-Black Tree)》一文中插入操作的情形一与情形二。
4.5.3.2.2、第二个if-else代码块为:
if (xp == (xppl = xpp.left)) {
if ((xppr = xpp.right) != null && xppr.red) {①
xppr.red = false;
xp.red = false;
xpp.red = true;
x = xpp;
}
else {
if (x == xp.right) {③
root = rotateLeft(root, x = xp);
xpp = (xp = x.parent) == null ? null : xp.parent;
}
if (xp != null) {④
xp.red = false;
if (xpp != null) {⑤
xpp.red = true;
root = rotateRight(root, xpp);
}
}
}
}
else {
if (xppl != null && xppl.red) {②
xppl.red = false;
xp.red = false;
xpp.red = true;
x = xpp;
}
else {
if (x == xp.left) {⑥
root = rotateRight(root, x = xp);
xpp = (xp = x.parent) == null ? null : xp.parent;
}
if (xp != null) {⑦
xp.red = false;
if (xpp != null) {⑧
xpp.red = true;
root = rotateLeft(root, xpp);
}
}
}
}
这里将xpp的左节点赋值给xppl并判断xppl是否与xp节点相等,若相等,则进入一个子if–else代码块①,若不相等,则进入另一个子if-else代码块②。
子if–else代码块①判断条件:将xpp的右节点赋值给xppr并判断xppr不为空,而且xppr是红色节点。
若满足条件,将xppr、xp设为黑色节点,xpp设为红色节点,并将xpp赋值给x。
- ①体现了红黑树在插入树节点时的情形三:见《红黑树(Red-Black Tree)》一文中插入操作的情形三。
若不满足条件,进入③,判断x是否是xp的右节点,如果判断成立,调用rotateLeft方法:
static <K,V> TreeNode<K,V> rotateLeft(TreeNode<K,V> root,
TreeNode<K,V> p) {
TreeNode<K,V> r, pp, rl;
if (p != null && (r = p.right) != null) {
if ((rl = p.right = r.left) != null)
rl.parent = p;
if ((pp = r.parent = p.parent) == null)
(root = r).red = false;
else if (pp.left == p)
pp.left = r;
else
pp.right = r;
r.left = p;
p.parent = r;
}
return root;
}
这里是红黑树的左旋操作。
-
③体现了红黑树在插入树节点时的情形四:见《红黑树(Red-Black Tree)》一文中插入操作的情形四。
-
④与⑤体现了红黑树在插入树节点时的情形五:见《红黑树(Red-Black Tree)》一文中插入操作的情形五。
子if–else代码块②判断条件:xppl不为空并且xppl是红色节点。
-
②体现了红黑树在插入树节点时的情形三:见《红黑树(Red-Black Tree)》一文中插入操作的情形三。(此时P节点在祖父节点的右节点上)
-
⑥体现了红黑树在插入树节点时的情形五:见《红黑树(Red-Black Tree)》一文中插入操作的情形五。(此时P节点在祖父节点的右节点上)
-
⑦体现了红黑树在插入树节点时的情形四:见《红黑树(Red-Black Tree)》一文中插入操作的情形四。(此时P节点在祖父节点的右节点上)
测试案例中的双向链表转化成红黑树的变化如下:
1、插入(ssm,4)节点:
2、插入(hej,4)节点:
3、插入(eck,4)节点:
此时,满足红黑树插入操作的情形五,对(ssm,4)节点进行一个右旋转:
4、插入(qso,4)节点:
此时,满足红黑树插入操作的清醒三,变为:
为了满足红黑树的性质一,即插入操作的情形一,又变为:
5、插入(hderm,4)节点:
6、插入(hderni,4)节点:
7、插入(eow,4)节点:
8、插入(xdy,4)节点:
此时,满足红黑树插入操作的情形三,于是红黑树变为:
9、插入(lyz,4)节点:
以上就是插入操作完成后,我们得到的红黑树。
4、moveRootToFront
所以,在进行插入后的平衡后,我们回到treeify方法,跳出for循环后,执行下面代码:
moveRootToFront(tab, root);
我们来看下moveRootToFront方法:
/**
* Ensures that the given root is the first node of its bin.
*/
static <K,V> void moveRootToFront(Node<K,V>[] tab, TreeNode<K,V> root) {
int n;
if (root != null && tab != null && (n = tab.length) > 0) {
int index = (n - 1) & root.hash;
TreeNode<K,V> first = (TreeNode<K,V>)tab[index];
if (root != first) {
Node<K,V> rn;
tab[index] = root;
TreeNode<K,V> rp = root.prev;
if ((rn = root.next) != null)
((TreeNode<K,V>)rn).prev = rp;
if (rp != null)
rp.next = rn;
if (first != null)
first.prev = root;
root.next = first;
root.prev = null;
}
assert checkInvariants(root);
}
}
确保当前传入的root节点是容器的第一个节点。
如果发现当前索引位置上的第一个节点first不是传入的root节点,那么首先将root节点的前置节点rp设为root节点的后置节点rn的前置节点,将rn设为rp的后置节点,再将root节点设为first节点的前置节点,将first节点设为root节点的后置节点。
原有双向链表从头到尾的顺序变为:(“hej”,4)、(“ssm”,4)、(“eck”,4)、(“qso”,4)、(“hderm”,4)、(“hderni”,4)、(“eow”,4)、(“xdy”,4)、(“lyz”,4)。
最后调用checkInvariants方法进行递归检查。
5、结论
综上所述,我们对HashMap将双向链表转化成红黑树的过程有了基本的了解:
首先,HashMap按照双向链表从头到尾的顺序来进行红黑树节点的插入操作,在进行插入操作前,利用当前节点与父节点的hash值比较来判断,当前节点应该放在左子节点还是右子节点。
这里hash值的比较,有三种方式,根据优先级从高到低依次是:
1、使用HashMap内置的hashcode()方法计算的hash值比较;
2、使用Comparable的compareTo方法进行比较;
3、使用System.identityHashCode来比较对象物理内存地址的hash值。
其次,将树节点插入红黑树后,进行性质平衡操作,避免插入节点后的红黑树违反了红黑树本身的特点,这里的部分可参照《红黑树(Red-Black Tree)》一文中插入操作的部分。
最后,将红黑树的根节点作为当前节点数组中索引位置的第一个节点。
以上即HashMap将双向链表转化成红黑树的全部过程。