简体   繁体   English

TreeSet中有序操作的时间复杂度是多少?

[英]What is the time complexity of ordered operations in TreeSet?

What is the time complexity of the following operations in java.util.TreeSet ? java.util.TreeSet以下操作的时间复杂度是多少?

  • first()
  • last()
  • lower()
  • higher()

I would assume that these are constant time but the API makes no guarantees. 我认为这些是恒定的时间,但API不保证。

Actually, I'd have thought that those operations are all going to be O(logN) for a general implementation. 实际上,我认为这些操作都是O(logN)用于一般实现。

  • For first() and last() to be O(1) the TreeSet implementation would need to maintain a pointer to the leftmost and rightmost leaf nodes in the tree respectively. 对于first()last()O(1) ,TreeSet实现将需要分别维护指向树中最左侧和最右侧叶节点的指针。 Maintaining these adds a constant cost to every insertion and at least a constant cost to every deletion. 维护这些会增加每次插入的固定成本,并且每次删除至少会产生不变的成本。 In reality, the implementation will probably find the left / rightmost nodes on the fly ... which is an O(logN) operation. 实际上,实现可能会在运行中找到左/右节点...这是一个O(logN)操作。

  • The lower() and higher() methods have to do the same work as get and are therefore O(logN) . lower()higher()方法必须执行与get相同的工作,因此是O(logN)

Of course, you can check the source-code yourself to see what actually happens. 当然,您可以自己检查源代码以查看实际发生的情况。 (As other people have done: see below.) (正如其他人所做的那样:见下文。)

Looks like both first() and last() will be O(log n) and not O(1)based on Implentation(sun jdk 1.6.0_23) of TreeMap which is used by TreeSet by default: 看起来first()和last()都是O(log n)而不是O(1)基于TreeMap的Implentation(sun jdk 1.6.0_23),TreeSet默认使用它:

 /**
 * Returns the first Entry in the TreeMap (according to the TreeMap's
 * key-sort function).  Returns null if the TreeMap is empty.
 */
final Entry<K,V> getFirstEntry() {
    Entry<K,V> p = root;
    if (p != null)
        while (p.left != null)
            p = p.left;
    return p;
}

/**
 * Returns the last Entry in the TreeMap (according to the TreeMap's
 * key-sort function).  Returns null if the TreeMap is empty.
 */
final Entry<K,V> getLastEntry() {
    Entry<K,V> p = root;
    if (p != null)
        while (p.right != null)
            p = p.right;
    return p;
}

I actually looked up the source code, in http://developer.classpath.org/doc/java/util/TreeSet-source.html , first() calls maps.firstKey() then in http://developer.classpath.org/doc/java/util/TreeMap-source.html 我实际查找了源代码,在http://developer.classpath.org/doc/java/util/TreeSet-source.html中 ,first()调用maps.firstKey()然后在http://developer.classpath中。组织/ DOC / JAVA / UTIL / TreeMap的-source.html

393: public K firstKey()
394: (
395: if (root == nil)
396: throw new NoSuchElementException();
397: return firstNode().key;
398: )

and in firstNode(), it does the while loop to go all the way to the left 在firstNode()中,它将while循环一直向左移动

952: final Node<K, V> firstNode()
953: (
954: // Exploit fact that nil.left == nil.
955: Node node = root;
956: while (node.left != nil)
957: node = node.left;
958: return node;
959: )

The API makes no guarantees because these are based on the standard model of a trie. API不做任何保证,因为它们基于特里的标准模型。 The best case is in O(1), with the average case being O(log n) and worst case O(n). 最好的情况是在O(1)中,平均情况是O(log n),最差情况是O(n)。

From the documentation: 从文档:

This implementation provides guaranteed log(n) time cost for the basic operations (add, remove and contains). 此实现为基本操作(添加,删除和包含)提供了有保证的log(n)时间成本。

These aren't the functions you asked for, but think about how Java will traverse the TreeSet. 这些不是您要求的功能,而是考虑Java将如何遍历TreeSet。

It's going to depend on the implementation. 这将取决于实施。 I'm not incredibly familiar with JAVA, but it seems that all of those operations are traversal operations (get lowest element, get highest element, get next higher or next lower). 我对JAVA并不是很熟悉,但似乎所有这些操作都是遍历操作(获取最低元素,获得最高元素,获得下一个更高或下一个更低)。

If the Tree is implemented as a Self-Balancing Binary Search Tree like an AVL Tree , or any other sort of a balanced-tree structure, you're going to be looking at Average-Case and Worst-Case O(log n) time for each of the operations, and a best case of O(1). 如果树被实现为自平衡二进制搜索树(AVL树 )或任何其他类型的平衡树结构,那么您将看到Average-Case和Worst-Case O(log n)时间对于每个操作,以及O(1)的最佳情况。

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

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