Time complexity of TreeMap operations- subMap, headMap, tailMap

前端 未结 2 511
抹茶落季
抹茶落季 2021-01-02 03:56

Does anyone know the time complexity of the operations of TreeMap like - subMap, headMap. tailMap.

The time complexity of operations like get, put is O(logn). But th

相关标签:
2条回答
  • 2021-01-02 04:13

    I was able to browse the source of TreeMap to get the detailed implementation.

    If you go in detail with the source code as to how they are actually getting the subMap its something like this...

    If you see the size method of NavigableSubMap

      public int size() {
            return (fromStart && toEnd) ? m.size() : entrySet().size();
        }
    

    The entrySet() implementation in multiple calls final calls up getCeilingEntry() function

    final Entry<K,V> getCeilingEntry(K key) {
        Entry<K,V> p = root;
        while (p != null) {
            int cmp = compare(key, p.key);
            if (cmp < 0) {
                if (p.left != null)
                    p = p.left;
                else
                    return p;
            } else if (cmp > 0) {
                if (p.right != null) {
                    p = p.right;
                } else {
                    Entry<K,V> parent = p.parent;
                    Entry<K,V> ch = p;
                    while (parent != null && ch == parent.right) {
                        ch = parent;
                        parent = parent.parent;
                    }
                    return parent;
                }
            } else
                return p;
        }
        return null;
    }
    

    SO i guess to get the actual map from the created submap; the time complexity is more than O(1).

    0 讨论(0)
  • 2021-01-02 04:26

    For those questions having the source code on hand is very useful as with sufficient IDE support you can simply browse through the implementation. When looking at the source code of TreeMap it can be seen, that all three methods construct a new map by using the constructor of AscendingSubMap:

    public NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive,
                                    K toKey,   boolean toInclusive) {
        return new AscendingSubMap(this,
                                   false, fromKey, fromInclusive,
                                   false, toKey,   toInclusive);
    }
    

    Which does nothing else then to pass the parameters up with the super constructor to the class NavigableSubMap:

    super(m, fromStart, lo, loInclusive, toEnd, hi, hiInclusive);
    

    So all three methods are based on the following constructor:

    NavigableSubMap(TreeMap<K,V> m,
                    boolean fromStart, K lo, boolean loInclusive,
                    boolean toEnd,     K hi, boolean hiInclusive) {
        if (!fromStart && !toEnd) {
            if (m.compare(lo, hi) > 0)
                throw new IllegalArgumentException("fromKey > toKey");
        } else {
            if (!fromStart) // type check
                m.compare(lo, lo);
            if (!toEnd)
                m.compare(hi, hi);
        }
    
        this.m = m;
        this.fromStart = fromStart;
        this.lo = lo;
        this.loInclusive = loInclusive;
        this.toEnd = toEnd;
        this.hi = hi;
        this.hiInclusive = hiInclusive;
    }
    

    All I can see here are invocations to compare for type and assertion checking reasons. Hence, it should be pretty much O(1).

    You can always browse the source code online, but I really recommend to get the source files and link them to your IDE of choice.

    0 讨论(0)
提交回复
热议问题