TreeMap 源码学习

 TreeMap是基于红黑树的实现

其排序标准是对key的自然排序(如果实例化时传入比较器,则对key按比较器排序)

containsKey,get,put,remove中提供了log(n)的时间复杂度

TreeMap并不是线成同步的

Map<String, Object> treemap = new TreeMap<>();

默认构造方法会创建一颗空树

默认使用key的自然排序来构建有序树,所谓自然排序就是key的类型是什么,就采用该类型的compareTo方法来比较大小。决定顺序。java自带的基本数据类型以及其装箱类型都实现了Comparable接口的compareTo方法。

key的类型,必须实现Compareable接口,如果不识闲,就没有办法完成元素大小的比较实现有序性。

 public TreeMap(Comparator<? super K> comparator) {
        this.comparator = comparator;
    }

还提供了支持外部比较器来初始化构造方法:即在外部定义一个类,实现Compartor接口compare方法

 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与新值的key
                //下面的if是用来判断,判断根节点是不是有子节点
                if (cmp < 0)
                    t = t.left; //这一步就是根节点移动到它的左孩子上
                else if (cmp > 0)
                    t = t.right;
                else
                    return t.setValue(value);//若值相同的话,覆盖旧值
            } while (t != null); //判断节点的子节点是不是为NULL,不为空的话说明有值,那么继续循环,这个循环的就是为了寻找子节点为Null的父节点
        }
        else {//使用默认的比较器
            if (key == null) //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<K,V> e = new Entry<>(key, value, parent);
        if (cmp < 0)
            parent.left = e; //将值添加到父节点的左子树上
        else
            parent.right = e;
        fixAfterInsertion(e);
        size++;
        modCount++;
        return null;
    }
final int compare(Object k1, Object k2) {
        return comparator==null ? ((Comparable<? super K>)k1).compareTo((K)k2)
            : comparator.compare((K)k1, (K)k2);
    }

//从这里就能知道,默认的比较器是compareTo,自定义的话是compare
Map<String, Object> treemap = new TreeMap<>();
treemap.put("1", 1);
treemap.put("1", 2);
int size = treemap.size();
System.out.println(treemap.get("1")+" "+size);  //2 1
final Entry<K,V> getEntry(Object key) {
        // Offload comparator-based version for sake of performance
        if (comparator != null)
            return getEntryUsingComparator(key);
        if (key == null)
            throw new NullPointerException();
        @SuppressWarnings("unchecked")
            Comparable<? super K> k = (Comparable<? super K>) key;
        Entry<K,V> p = root;
        while (p != null) {
            int cmp = k.compareTo(p.key);
            if (cmp < 0)
                p = p.left;
            else if (cmp > 0)
                p = p.right;
            else
                return p;
        }
        return null;
    }

get方法就是利用了二查查找树,从根节点开始,若大于当前节点,则比较当前节点的右子树,否则比较当前节点的左子树

这就与红黑树有关,准确的说与二叉排序树有关

二叉排序树:要么是空树,要么是以下特征的树

                     左节点的值必须小于等于父节点的值

                     右节点的值必须大于等于父节点的值

一般操作时间是O(lgn),最差是O(n)

平衡二叉树:要么是空树,要么是以下特征的树

                     左子树和右子树都是平衡二叉树

                    左子树和右子树的深度差的绝对值不为1

红黑树是一种特殊的二叉查找树,在每一个节点上增加了存储颜色。颜色要么是红色,要么是黑色

                   每个节点要么是红色,要么是黑色

                   根节点是黑色

                   所有叶子节点是黑色,即Null

                   如果一个节点是红色。则它的两个子节点必须是黑色的。

                   从一个节点到其所有叶子节点所有路径包含相同数目的黑节点

因此在最坏情况下,红黑树能保证时间复杂度为O( lgn )

猜你喜欢

转载自blog.csdn.net/qq_35815781/article/details/85063975