TreeMap 的简单解释

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;

猜你喜欢

转载自www.cnblogs.com/zwb1/p/12080633.html