共同学习Java源代码-数据结构-HashMap(二十)
来源:互联网 发布:全景视频拼接软件 编辑:程序博客网 时间:2024/05/23 21:33
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);
}
}
}
}
}
}
static <K,V> TreeNode<K,V> balanceDeletion(TreeNode<K,V> root,
TreeNode<K,V> x) {
for (TreeNode<K,V> xp, xpl, xpr;;) {
if (x == null || x == root)
return root;
else if ((xp = x.parent) == null) {
x.red = false;
return x;
}
else if (x.red) {
x.red = false;
return root;
}
else if ((xpl = xp.left) == x) {
if ((xpr = xp.right) != null && xpr.red) {
xpr.red = false;
xp.red = true;
root = rotateLeft(root, xp);
xpr = (xp = x.parent) == null ? null : xp.right;
}
if (xpr == null)
x = xp;
else {
TreeNode<K,V> sl = xpr.left, sr = xpr.right;
if ((sr == null || !sr.red) &&
(sl == null || !sl.red)) {
xpr.red = true;
x = xp;
}
else {
if (sr == null || !sr.red) {
if (sl != null)
sl.red = false;
xpr.red = true;
root = rotateRight(root, xpr);
xpr = (xp = x.parent) == null ?
null : xp.right;
}
if (xpr != null) {
xpr.red = (xp == null) ? false : xp.red;
if ((sr = xpr.right) != null)
sr.red = false;
}
if (xp != null) {
xp.red = false;
root = rotateLeft(root, xp);
}
x = root;
}
}
}
else { // symmetric
if (xpl != null && xpl.red) {
xpl.red = false;
xp.red = true;
root = rotateRight(root, xp);
xpl = (xp = x.parent) == null ? null : xp.left;
}
if (xpl == null)
x = xp;
else {
TreeNode<K,V> sl = xpl.left, sr = xpl.right;
if ((sl == null || !sl.red) &&
(sr == null || !sr.red)) {
xpl.red = true;
x = xp;
}
else {
if (sl == null || !sl.red) {
if (sr != null)
sr.red = false;
xpl.red = true;
root = rotateLeft(root, xpl);
xpl = (xp = x.parent) == null ?
null : xp.left;
}
if (xpl != null) {
xpl.red = (xp == null) ? false : xp.red;
if ((sl = xpl.left) != null)
sl.red = false;
}
if (xp != null) {
xp.red = false;
root = rotateRight(root, xp);
}
x = root;
}
}
}
}
}
这两个方法是在插入和删除后调整红黑树各个节点颜色的方法 但可读性较差 暂时不再深入分析了
至此HashMap分析完毕
- 共同学习Java源代码-数据结构-HashMap(二十)
- 共同学习Java源代码-数据结构-HashMap(十)
- 共同学习Java源代码-数据结构-HashMap(二)
- 共同学习Java源代码-数据结构-HashMap(一)
- 共同学习Java源代码-数据结构-HashMap(三)
- 共同学习Java源代码-数据结构-HashMap(四)
- 共同学习Java源代码-数据结构-HashMap(五)
- 共同学习Java源代码-数据结构-HashMap(六)
- 共同学习Java源代码-数据结构-HashMap(七)
- 共同学习Java源代码-数据结构-HashMap(八)
- 共同学习Java源代码-数据结构-HashMap(九)
- 共同学习Java源代码-数据结构-HashMap(十一)
- 共同学习Java源代码-数据结构-HashMap(十二)
- 共同学习Java源代码-数据结构-HashMap(十三)
- 共同学习Java源代码-数据结构-HashMap(十四)
- 共同学习Java源代码-数据结构-HashMap(十五)
- 共同学习Java源代码-数据结构-HashMap(十六)
- 共同学习Java源代码-数据结构-HashMap(十七)
- 是AI就躲个飞机-纯Python实现人工智能
- 阿里巴巴与星巴克合作 AR场景识别首次大规模商用
- html--标签
- c++ primer 13.3节练习(定义自己版本的swap函数并排序)
- Centos不能解析域名
- 共同学习Java源代码-数据结构-HashMap(二十)
- springmvc+mybatis+maven项目集成的时候遇到一个问题,项目搭起来以后,http请求怎么都进不到controller中,页面直接返回404错误,控制台没有报任何错误,请大神帮我看看
- PHP与h5 ajax 跨域上传图片解决方案
- [洪流学堂]Hololens开发高级篇1:凝视(Gaze)
- (记录)万象优图图片分析
- Today is a perfect day to start living your dream.
- python2和python3中的range区别
- 关于中文情感分类的知识
- js基本概念(中)之操作符