赞
踩
rotateLeft(TreeNode<K,V> root,TreeNode<K,V> p)
static <K,V> TreeNode<K,V> rotateLeft(TreeNode<K,V> root, TreeNode<K,V> p) { TreeNode<K,V> r, pp, rl; // 确保 p 的右孩子 r 不为空 if (p != null && (r = p.right) != null) { // 将 r 上面的 左孩子 覆盖原来 p 上面的右孩子 if ((rl = p.right = r.left) != null) rl.parent = p; // 将 r 作为 头节点 与 p 原来父节点相连,若为null说明作为了根,黑色 if ((pp = r.parent = p.parent) == null) (root = r).red = false; else if (pp.left == p) pp.left = r; else pp.right = r; // p 作为了 r 的左孩子 r.left = p; p.parent = r; } return root; }
rotateRight(TreeNode<K,V> root,TreeNode<K,V> p)
static <K,V> TreeNode<K,V> rotateRight(TreeNode<K,V> root, TreeNode<K,V> p) { TreeNode<K,V> l, pp, lr; // 确保 p 的 左孩子 l 不为null if (p != null && (l = p.left) != null) { // 将 l 上的 右孩子 覆盖 p 的 左孩子 if ((lr = p.left = l.right) != null) lr.parent = p; // 将 l 作为头节点 连接 p 原来的父亲,若为null说明作为了根,黑色 if ((pp = l.parent = p.parent) == null) (root = l).red = false; else if (pp.right == p) pp.right = l; else pp.left = l; // p 作为了 l 的右孩子 l.right = p; p.parent = l; } return root; }
规则:
balanceInsertion(TreeNode<K,V> root,TreeNode<K,V> x)
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的父节点 为空 // 说明 x节点 为根节点-黑色 x.red = false; return x; } else if (!xp.red || (xpp = xp.parent) == null) // 如果 x的父节点 为黑色 || x的祖父节点 为空 // 说明 不需要再调整,直接返回根节点 return root; // x的父节点 为红色 && x的祖父节点 不为空 if (xp == (xppl = xpp.left)) { // 如果 x的父节点 为 x的祖父节点 的 左孩子 if ((xppr = xpp.right) != null && xppr.red) { // 如果 x的祖父 的 右孩子 不为空 并且为红色 // x的祖父 的 左右孩子 都为黑色 // x的祖父 为 红色 xppr.red = false; xp.red = false; xpp.red = true; // x 变为 x 的祖父-红色 x = xpp; } else { // 如果 x的祖父 的 右孩子 为空 或者为黑色 if (x == xp.right) { // 如果 x 为父节点的 右孩子 // 左旋转 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 { // 如果 x的父节点 为 x的祖父节点 的 右孩子 if (xppl != null && xppl.red) { // 如果 x的祖父 的 左孩子 不为空 并且为红色 xppl.red = false; xp.red = false; xpp.red = true; x = xpp; } else { // 如果 x的祖父 的 左孩子 为空 或者为黑色 if (x == xp.left) { // 如果 x 为父节点的 左孩子 // 右旋转 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); } } } } } }
x的父节点 为 x的祖父节点 的 左孩子:
如果 x的祖父 的 右孩子 不为空 并且为红色
if ((xppr = xpp.right) != null && xppr.red) {
// x的祖父 的 左右孩子 都为黑色
// x的祖父 为 红色
xppr.red = false;
xp.red = false;
xpp.red = true;
// x 变为 x 的祖父-红色
x = xpp;
}
变色:
如果 x的祖父 的 右孩子 为空 或者为黑色
如果 x 为父节点的 右孩子
if (x == xp.right) {
// 左旋转
root = rotateLeft(root, x = xp);
xpp = (xp = x.parent) == null ? null : xp.parent;
}
左旋转(父节点 p):
如果 x 为父节点的 左孩子
if (xp != null) {
// 祖父节点为红色,父、叔节点为黑色
xp.red = false;
if (xpp != null) {
xpp.red = true;
// 右旋转
root = rotateRight(root, xpp);
}
}
变色:
右旋转(祖父节点 p):
x的父节点 为 x的祖父节点 的 右孩子:
如果 x的祖父 的 左孩子 不为空 并且为红色
if (xppl != null && xppl.red) {
xppl.red = false;
xp.red = false;
xpp.red = true;
x = xpp;
}
变色:
如果 x的祖父 的 左孩子 为空 或者为黑色
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);
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。