简体   繁体   English

TreeMap操作的时间复杂度--subMap,headMap,tailMap

[英]Time complexity of TreeMap operations- subMap, headMap, tailMap

Does anyone know the time complexity of the operations of TreeMap like - subMap, headMap. 有没有人知道TreeMap操作的时间复杂度 - 例如 - subMap,headMap。 tailMap. 的tailMap。

The time complexity of operations like get, put is O(logn). 像get,put这样的操作的时间复杂度是O(logn)。 But the javadoc doesnt say much about the complexity for the above operations. 但javadoc并没有说明上述操作的复杂性。

The worst case complexity I can thinks of O(n) since it will go through the entire list if the set includes the last element. 最糟糕的情况复杂性我可以想到O(n),因为如果集合包含最后一个元素,它将遍历整个列表。 Can we confirm it? 我们可以证实吗?

For those questions having the source code on hand is very useful as with sufficient IDE support you can simply browse through the implementation. 对于那些拥有源代码的问题非常有用,因为有足够的IDE支持,您只需浏览实现即可。 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 : 在查看TreeMap的源代码时,可以看出,所有三种方法都是使用AscendingSubMap构造函数构造一个新的map:

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 : 然后,除了将超级构造函数的参数传递给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. 我只能在这里看到的调用来compare的类型和断言检查的原因。 Hence, it should be pretty much O(1) . 因此,它应该是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. 您可以随时在线浏览源代码 ,但我真的建议您获取源文件并将它们链接到您选择的IDE。

I was able to browse the source of TreeMap to get the detailed implementation. 我能够浏览TreeMap的源代码以获得详细的实现。

If you go in detail with the source code as to how they are actually getting the subMap its something like this... 如果你详细了解它们如何实际获取subMap的源代码,就像这样......

If you see the size method of NavigableSubMap 如果你看到NavigableSubMap的size方法

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

The entrySet() implementation in multiple calls final calls up getCeilingEntry() function 多个调用中的entrySet()实现最终调用getCeilingEntry()函数

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). 时间复杂度大于O(1)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM