zoukankan      html  css  js  c++  java
  • TreeMap源码分析

    简介

    基于红黑树的实现,根据key的自然顺序排序,或者根据构造方法传入的排序方式。

    类继承关系

    在这里插入图片描述

    属性

    	//构造方法传入的比较器 借此维护键的顺序
        private final Comparator<? super K> comparator;
        private transient Entry<K,V> root;//根节点
        private transient int size = 0;//长度
        private transient int modCount = 0;//修改次数
    

    构造方法

    //构造一个空的map,使用键的自然排序
    public TreeMap() {
        comparator = null;
    }
    public TreeMap(Comparator<? super K> comparator) {
        this.comparator = comparator;
    }
    public TreeMap(Map<? extends K, ? extends V> m) {
        comparator = null;
        putAll(m);
    }
    public TreeMap(SortedMap<K, ? extends V> m) {
        comparator = m.comparator();
        try {
            buildFromSorted(m.size(), m.entrySet().iterator(), null, null);
        } catch (java.io.IOException cannotHappen) {
        } catch (ClassNotFoundException cannotHappen) {
        }
    }
    

    内部类

    //红黑树
    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;
    
        Entry(K key, V value, Entry<K,V> parent) {
            this.key = key;
            this.value = value;
            this.parent = parent;
        }
    }
    

    添加put

    public V put(K key, V value) {
        Entry<K,V>t = root;
        if (t == null) {//如果根节点为null 将当前节点设置根
            root = new Entry<>(key, value, null);
            size = 1;
            modCount++;
            return null;
        }
        int cmp;
        Entry<K,V> parent;
        // 根据比较器获得父节点和比较路径
        Comparator<? super K> cpr = comparator;
        //如果为空 使用key自然排序
        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")
                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;
    }
    

    获取get

    public V get(Object key) {
        Entry<K,V> p = getEntry(key);
        return (p==null ? null : p.value);
    }
    //使用比较器找到可以所在节点
    final Entry<K,V> getEntry(Object key) {
        if (comparator != null)
            return getEntryUsingComparator(key);
        if (key == null)
            throw new NullPointerException();
        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;
    }
    
    final Entry<K,V> getEntryUsingComparator(Object key) {
        K k = (K) key;
        Comparator<? super K> cpr = comparator;
        if (cpr != null) {
            Entry<K,V> p = root;
            while (p != null) {
                int cmp = cpr.compare(k, p.key);
                if (cmp < 0)
                    p = p.left;
                else if (cmp > 0)
                    p = p.right;
                else
                    return p;
            }
        }
        return null;
    }
    

    删除remove

    public V remove(Object key) {
        Entry<K,V> p = getEntry(key);//先查出数据
        if (p == null)
            return null;
    
        V oldValue = p.value;
        deleteEntry(p);
        return oldValue;
    }
    
    private void deleteEntry(Entry<K,V> p) {
        modCount++;
        size--;
    
        // 将下一个节点放到p位置
        if (p.left != null && p.right != null) {
            Entry<K,V> s = successor(p);
            p.key = s.key;
            p.value = s.value;
            p = s;
        } // p has 2 children
    
        // 修复节点
        Entry<K,V> replacement = (p.left != null ? p.left : p.right);
    
        if (replacement != null) {
            // 链接替换到父级
            replacement.parent = p.parent;
            if (p.parent == null)
                root = replacement;
            else if (p == p.parent.left)
                p.parent.left  = replacement;
            else
                p.parent.right = replacement;
    
            p.left = p.right = p.parent = null;
    
            // Fix replacement
            if (p.color == BLACK)
                fixAfterDeletion(replacement);
        } else if (p.parent == null) { // 如果只有一个节点,则返回
            root = null;
        } else { // 没有子节点。使用self作为幻像替换并取消链接。
            if (p.color == BLACK)
                fixAfterDeletion(p);
    
            if (p.parent != null) {
                if (p == p.parent.left)
                    p.parent.left = null;
                else if (p == p.parent.right)
                    p.parent.right = null;
                p.parent = null;
            }
        }
    }
    

    总结

    TreeMap内部是红黑树
    如果不自定义排序,那么就使用key的自然排序
    可以存储null键和值

  • 相关阅读:
    记录爱忘记的STL知识点
    小狼
    CVTE总结
    STL底层实现
    小狼,你家BOSS喊你面试啦!!!(四)
    npm属性笔记
    iview中关于table组件内放入Input会失去焦点
    js实现数组内数据的上移和下移
    google搜索使用技巧
    bat中实现代码拷贝到指定目录后启动命令行并更改默认路径
  • 原文地址:https://www.cnblogs.com/paper-man/p/13284621.html
Copyright © 2011-2022 走看看