ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

TreeMap 的简单解释

2019-12-22 19:02:30  阅读:233  来源: 互联网

标签:解释 parent TreeMap else key 简单 null 节点 cmp


TreeMap的构造函数   可以传入 自定义的比较器、Map、SortedMap。   put方法:
public V put(K key, V value) {
    Entry<K,V> t = root; //得到根节点
    if (t == null) { //如果根节点为空
        compare(key, key); // type (and possibly null) check
        root = new Entry<>(key, value, null); //把当前的键值对插入作为根节点
        size = 1;
        modCount++;
        return null;
    }
    int cmp;
    Entry<K,V> parent;
    // split comparator and comparable paths
    Comparator<? super K> cpr = comparator; //取得比较器
    if (cpr != null) {
        do {
            parent = t;
            cmp = cpr.compare(key, t.key); //根据比较器 比较当前节点与插入节点的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; //尝试将key转为Comparable<? super K>类型,就是说 如果没有传入比较器
                                                    //,key所在的类需要实现Comparable接口
        do {
            parent = t;
            cmp = k.compareTo(t.key); //尝试用key自己实现的comparteTo方法比较父节点的key
            if (cmp < 0)
                t = t.left;
            else if (cmp > 0)
                t = t.right;
            else
                return t.setValue(value);
        } while (t != null);
    }
    Entry<K,V> e = new Entry<>(key, value, parent); //创建新节点,设置key value 父节点
    if (cmp < 0)
        parent.left = e;
    else
        parent.right = e;
    fixAfterInsertion(e); //插入新节点后 对 红黑树继续宁
    size++;
    modCount++;
    return null;
}
public V put(K key, V value) {
    Entry<K,V> t = root; //得到根节点
    if (t == null) { //如果根节点为空
        compare(key, key); // type (and possibly null) check
        root = new Entry<>(key, value, null); //把当前的键值对插入作为根节点
        size = 1;
        modCount++;
        return null;
    }
    int cmp;
    Entry<K,V> parent;
    // split comparator and comparable paths
    Comparator<? super K> cpr = comparator; //取得比较器
    if (cpr != null) {
        do {
            parent = t;
            cmp = cpr.compare(key, t.key); //根据比较器 比较当前节点与插入节点的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")
    //尝试将key转为Comparable ,如果没有实现此接口,会报错
        Comparable<? super K> k = (Comparable<? super K>) key;
    do {
        parent = t;
        cmp = k.compareTo(t.key);//尝试用key自身的compareTo方法比较
        if (cmp < 0)
            t = t.left;
        else if (cmp > 0)
            t = t.right;
        else
            return t.setValue(value);
    } while (t != null);
}
Entry<K,V> e = new Entry<>(key, value, parent); //此时,找到插入的位置,创建新的节点,传入参数,以及对父节点的引用
if (cmp < 0)
    parent.left = e;
else
    parent.right = e;
fixAfterInsertion(e); //插入节点后,对红黑树进行相应的变化
size++;
modCount++;
return null;

 

标签:解释,parent,TreeMap,else,key,简单,null,节点,cmp
来源: https://www.cnblogs.com/zwb1/p/12080633.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有