美文网首页
TreeMap小抄

TreeMap小抄

作者: 上海马超23 | 来源:发表于2017-07-06 23:48 被阅读0次
     public class TreeMap {
        // 红黑树实现
        // 树的层数不会差的太远,使得所有操作的复杂度不超过 O(lgn)
        // 但也使得插入,修改时要复杂的左旋右旋来保持树的平衡。
        static final class Entry<K,V> implements Map.Entry<K,V> {
            K key;
            V value;
            Entry<K,V> left;
            Entry<K,V> right;
            Entry<K,V> parent;
            boolean color = BLACK;
            ...
        }
        
        // 修改值会做比较,代价比HashMap要大
        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);
                    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);
                    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;
        }
        
        public K firstKey() {
            return key(getFirstEntry());
        }
        
        // 第一个节点即最左边的节点
        final Entry<K,V> getFirstEntry() {
            Entry<K,V> p = root;
            if (p != null)
                while (p.left != null)
                    p = p.left;
            return p;
        }
        
        // 最后一个节点即最右边的节点
        final Entry<K,V> getLastEntry() {
            Entry<K,V> p = root;
            if (p != null)
                while (p.right != null)
                    p = p.right;
            return p;
        }
     }
    

    相关文章

      网友评论

          本文标题:TreeMap小抄

          本文链接:https://www.haomeiwen.com/subject/tdnkhxtx.html