如何实现TreeMap
发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,这篇文章给大家分享的是有关如何实现TreeMap的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。/**The comparator used to maintain orde
千家信息网最后更新 2026年11月03日如何实现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安全错误
数据库的锁怎样保障安全
手磨咖啡
重生之互联网科技帝国
科技平台互联网
圆梦互联网科技
互联网科技的
杭州互联网科技公司
互联网科技实习生岗位职责
成都互联网科技公司100强
茂名市互联网科技产业园
荆州市昕露互联网络科技有限公司
佛山市拉法互联网科技有限公司
济南有互联网科技公司吗
上海科技产业互联网平台
广州燕处互联网科技有限公司
易租星互联网科技有限公司
玉溪新华互联网科技怎么选
山东蛋芒互联网科技有限公司
互联网科技大会大佬
互联网高科技绘画
广州全民伙伴互联网科技有限公司
上海挚品互联网科技有限公司
华服互联网科技靠谱吗
互联网 卓越科技
凡科互联网科技
科技与互联网的利与弊
昊天互联网科技有限公司
伍佰万互联网科技怎么赚钱
智帮互联网科技公司最近动向
保山互联网科技靠谱吗
北京品多多互联网科技有限公司