HashMap红黑树是jdk1.8之后新加的内部类,主要用于处理链表容量超过8个时的hash碰撞情况,时间复杂度为O(logN);要想彻底理解HashMap原理,红黑树是必须要学习的基础。这里我们从jdk1.8源码一步步认识红黑树的结构及原理。
1.首先认识一下他的继承树。
TreeNode继承了LinkedHashMap.Entry,因此他除了自身的特性外,还拥有LinkedHashMap.Entry的特性。
2.TreeNode成员变量
TreeNode<K,V> parent; // red-black tree links 红黑树连表
TreeNode<K,V> left; //左边节点
TreeNode<K,V> right; //右边节点
TreeNode<K,V> prev; // needed to unlink next upon deletion
boolean red;
3.构造方法
/**
*hash 节点hash
*key 键
*value 值
*next 下个节点
*/
TreeNode(int hash, K key, V val, Node<K,V> next) {
//调用父类构造方法。这个方法实现是Node类
super(hash, key, val, next);
}
4.方法解析
4.1 final TreeNode<K,V> root() 获取该节点的树根节点
/**
* Returns root of tree containing this node.
*/
final TreeNode<K,V> root() {
//定义一个r节点,并将r节点指向本节点,再定义一个p节点,开始无限循环。对此有疑问的同学可以这样看
//TreeNode<K,V> r = this, p;
//相当于
//TreeNode<K,V> r = this;TreeNode<K,V> p;
for (TreeNode<K,V> r = this, p;;) {
//将p节点指向r的父节点,,如果父节点不存在,那么该节点为根节点,返回该节点。
if ((p = r.parent) == null)
return r;
//否则,将r指向该节点,继续向上递归。
r = p;
}
}
4.2 getTreeNode(int h, Object k) 查找给定的hash和key对应的节点,调用4.3方法
final TreeNode<K,V> getTreeNode(int h, Object k) {
//如果父节点存在,则从根节点开始找,否则从该节点开始找
return ((parent != null) ? root() : this).find(h, k, null);
}
4.3 final TreeNode<K,V> find(int h, Object k, Class<?> kc) 从根节点开啥查找给定的hash和key对应的节点
/**
* Finds the node starting at root p with the given hash and key.
* The kc argument caches comparableClassFor(key) upon first use
* comparing keys.
*/
final TreeNode<K,V> find(int h, Object k, Class<?> kc) {
//定义一个p节点指向该节点
TreeNode<K,V> p = this;
//遍历
do {
int ph, dir; K pk;
//获取该节点的左节点和右节点。
TreeNode<K,V> pl = p.left, pr = p.right, q;
//如果该节点的hash值大于给定的哈希值h,那么将该节点指向左节点
if ((ph = p.hash) > h)
p = pl;
//如果该节点的hash值小于给定的哈希值h,那么将该节点指向右节点
else if (ph < h)
p = pr;
//如果该节点的hash值等于给定的哈希值h,且该节点的key和给定的k相同,那么直接返回该节点
else if ((pk = p.key) == k || (k != null && k.equals(pk)))
return p;
//如果该节点的hash值等于给定的哈希值h,且该节点的key和给定的k不同,且左节点为空,那么将该节点指向右节点
else if (pl == null)
p = pr;
//如果该节点的hash值等于给定的哈希值h,且该节点的key和给定的k不同,且左节点不为空右节点为空,那么将该节点指向左节点
else if (pr == null)
p = pl;
//如果该节点的hash值等于给定的哈希值h,且该节点的key和给定的k不同,且左右节点都不为空,如果给定对比参数类型kc不为空或者kc为可比较类型,同时k和本结点key不同,如果给定的k小,则将该节点指向左节点,否则,将该节点指向右节点
else if ((kc != null ||
(kc = comparableClassFor(k)) != null) &&
(dir = compareComparables(kc, k, pk)) != 0)
p = (dir < 0) ? pl : pr;
//如果以上条件都不满足则递归一次获取下一个节点。
else if ((q = pr.find(h, k, kc)) != null)
return q;
//如果递归一次仍获取不到节点则将该节点指向左节点
else
p = pl;
} while (p != null);
return null;
}
流程图:
4.4 tieBreakOrder 用这个方法来比较两个对象,返回值要么大于0,要么小于0,不会为0
/**
* 用这个方法来比较两个对象,返回值要么大于0,要么小于0,不会为0
* 也就是说这一步一定能确定要插入的节点要么是树的左节点,要么是右节点,不然就无法继续满足二叉树结构了
*
* 先比较两个对象的类名,类名是字符串对象,就按字符串的比较规则
* 如果两个对象是同一个类型,那么调用本地方法为两个对象生成hashCode值,再进行比较,hashCode相等的话返回-1
*/
static int tieBreakOrder(Object a, Object b) {
int d;
if (a == null || b == null ||
(d = a.getClass().getName().
compareTo(b.getClass().getName())) == 0)
//如果a==null或b==null或a和b类型相同,那么通过系统方法生成hash值进行比较
d = (System.identityHashCode(a) <= System.identityHashCode(b) ?
-1 : 1);
return d;
}
4.5
static <K,V> TreeNode<K,V> rotateLeft(TreeNode<K,V> root,
TreeNode<K,V> p) {
TreeNode<K,V> r, pp, rl;
//如果p结点及其右节点都不为null
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;
}
//如果p节点或者其右节点有一个为null,返回根节点root。
return root;
}
4.5 balanceInsertion
static <K,V> TreeNode<K,V> balanceInsertion(TreeNode<K,V> root,
TreeNode<K,V> x) {
//将该节点red置为true
x.red = true;
for (TreeNode<K,V> xp, xpp, xppl, xppr;;) {
//如果x的父节点为空,则将x节点red改为false,返回x节点
if ((xp = x.parent) == null) {
x.red = false;
return x;
}
//否则,如果父节点red为false且无父节点,则返回根节点root。
else if (!xp.red || (xpp = xp.parent) == null)
return root;
//至此x上级超过一层
//如果父节点正好是其父节点的左节点
if (xp == (xppl = xpp.left)) {
//且父节点的父节点的右节点不为空且是红节点,那么将这个右节点置为黑节点,父节点置为黑节点,父节点的父节点置为红节点,x指向父节点的父节点
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);
}
}
}
}
}
}
4.6
/**
* Forms tree of the nodes linked from this node.
*/
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;
//将该节点的左右节点均置为null
x.left = x.right = null;
//如果根节点root是null,则将该节点父节点置空,red标志置为false,根节点指向该节点
if (root == null) {
x.parent = null;
x.red = false;
root = x;
}
else {
//如果根节点不为null
K k = x.key;
int h = x.hash;
Class<?> kc = null;
//从根节点开始遍历,
for (TreeNode<K,V> p = root;;) {
int dir, ph;
K pk = p.key;
//如果根节点hash值大于h,则dir=-1
if ((ph = p.hash) > h)
dir = -1;
//如果根节点hash值小于h,则dir=1
else if (ph < h)
dir = 1;
//如果hash值相同,比较key
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);
}