如何实现TreeMap
发表于:2025-11-10 作者:千家信息网编辑
千家信息网最后更新 2025年11月10日,这篇文章给大家分享的是有关如何实现TreeMap的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。/**The comparator used to maintain orde
千家信息网最后更新 2025年11月10日如何实现TreeMap
这篇文章给大家分享的是有关如何实现TreeMap的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
/**
The comparator used to maintain order in this tree map, or
null if it uses the natural ordering of its keys.
@serial
*/
//自然排序
private final Comparator super K> comparator;
//根节点
private transient Entryroot; /**
The number of entries in the tree
*/
//大小
private transient int size = 0;
public TreeMap() {
comparator = null;
}
public V put(K key, V value) {
Entry
if (t == null) {
compare(key, key); // type (and possibly null) check
root = new Entry<>(key, value, null);//构造根节点,root没有父节点,传入null size = 1; modCount++; return null; } int cmp; Entryparent; //定义节点 // split comparator and comparable paths Comparator super K> cpr = comparator; //获得比较器 if (cpr != null) {//定义了比较器,采用自定义比较器进行比较 do { parent = t;//将红黑树根节点赋值给parent 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 e = new Entry<>(key, value, parent); if (cmp < 0) parent.left = e; else parent.right = e; //新插入节点后重新调整红黑树 fixAfterInsertion(e); size++; modCount++; return null;}/** From CLR */private void fixAfterInsertion(Entry x) { //加入的节点默认的颜色是红色 x.color = RED; //情形1:新节点x是树的根节点,没有父节点不需要任何操作 //情形2:新节点x的父节点颜色是黑色的,不需要操作 while (x != null && x != root && x.parent.color == RED) { //情形3:新节点的父节点颜色是红色的 //判断x的节点的父节点位置,是否属于左子节点 if (parentOf(x) == leftOf(parentOf(parentOf(x)))) { //获取x节点的父节点的兄弟节点,上面语句已经判断出x节点的父节点为左子节点,所以直接取右子节点 Entry y = rightOf(parentOf(parentOf(x))); //判断是否x节点的父节点的兄弟节点为红色 if (colorOf(y) == RED) { setColor(parentOf(x), BLACK);//x节点的父节点设置为黑色 setColor(y, BLACK);//y节点的颜色设置为黑色 setColor(parentOf(parentOf(x)), RED);//x的父节点的父节点设置为红色 x = parentOf(parentOf(x)); } else { if (x == rightOf(parentOf(x))) { x = parentOf(x); rotateLeft(x); } setColor(parentOf(x), BLACK); setColor(parentOf(parentOf(x)), RED); rotateRight(parentOf(parentOf(x))); } } else { Entry y = leftOf(parentOf(parentOf(x))); if (colorOf(y) == RED) { setColor(parentOf(x), BLACK); setColor(y, BLACK); setColor(parentOf(parentOf(x)), RED); x = parentOf(parentOf(x)); } else { if (x == leftOf(parentOf(x))) { x = parentOf(x); rotateRight(x); } setColor(parentOf(x), BLACK); setColor(parentOf(parentOf(x)), RED); rotateLeft(parentOf(parentOf(x))); } } } root.color = BLACK;}
感谢各位的阅读!关于"如何实现TreeMap"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
节点
红色
颜色
比较器
情形
黑色
兄弟
内容
更多
篇文章
自然
排序
不错
实用
位置
大小
指向
文章
树根
看吧
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
尚脑互联网科技上班怎么样
稳定性好的基因数据库
服务器历史的回顾和将来的展望
诺必行词典数据库
我的世界服务器后台查看密码
郑州管理软件开发流程
网络安全师证书
应用技术和网络技术
数据结构和软件开发的关系
绝地求生2怎么连接不了服务器
华为服务器内存条插法
gmail数据库
大型棋牌高服务器
英国网络安全技术学院
民睿互联网科技
高一信息网络技术教案
武汉网络安全周2020
gp数据库查字符串
中国汉代图像信息数据库
网络安全公益大讲堂在贵阳开课
儿童网络安全文章
服务器53端口
前端定义一个按钮删除数据库
网络技术就业率
网络技术专业哪个大学好
英超 数据库
花饭圈乱象绘网络安全
佛山定制软件开发
如何调用类中的数据库
保密技术和网络安全