TreeSet 中有序操作的时间复杂度是多少?
What is the time complexity of the following operations in java.util.TreeSet
?
first()
last()
lower()
higher()
I would assume that these are constant time but the API makes no guarantees.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
实际上,我原以为这些操作对于一般实现来说都是
O(logN)
。要使
first()
和last()
的时间复杂度为O(1)
,TreeSet 实现需要维护一个指向最左边的指针和树中最右边的叶节点。维护这些会为每次插入增加恒定成本,并且至少为每次删除增加恒定成本。实际上,该实现可能会动态找到最左/最右节点...这是一个O(logN)
操作。lower()
和higher()
方法必须执行与get
相同的工作,因此O(logN )
。当然,您可以自己检查源代码,看看实际发生了什么。 (正如其他人所做的那样:见下文。)
Actually, I'd have thought that those operations are all going to be
O(logN)
for a general implementation.For
first()
andlast()
to beO(1)
the TreeSet implementation would need to maintain a pointer to the leftmost and rightmost leaf nodes in the tree respectively. 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 anO(logN)
operation.The
lower()
andhigher()
methods have to do the same work asget
and are thereforeO(logN)
.Of course, you can check the source-code yourself to see what actually happens. (As other people have done: see below.)
根据 TreeSet 默认使用的 TreeMap 的实现(sun jdk 1.6.0_23),看起来first()和last()都是O(log n)而不是O(1):
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:
其实我查了一下源码
在 http://developer.classpath.org/doc/java/util/ TreeSet-source.html,first()调用maps.firstKey()
然后在
http://developer.classpath.org/doc/java/util/TreeMap -source.html
并在firstNode()中,它执行while循环一直到左边
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
and in firstNode(), it does the while loop to go all the way to the left
这将取决于实施情况。我对JAVA不是很熟悉,但似乎所有这些操作都是遍历操作(获取最低元素,获取最高元素,获取下一个更高或下一个更低)。
如果树被实现为自平衡二叉搜索树,就像AVL 树,或任何其他类型的平衡树结构,您将看到平均情况每个操作的最坏情况为 O(log n) 时间,最好情况为 O(1)。
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).
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).
API 不提供任何保证,因为这些保证基于 trie 的标准模型。最好的情况是 O(1),平均情况是 O(log n),最坏情况是 O(n)。
从文档中:
这些不是您要求的功能,但请考虑一下 Java 将如何遍历 TreeSet。
The API makes no guarantees because these are based on the standard model of a trie. The best case is in O(1), with the average case being O(log n) and worst case O(n).
From the documentation:
These aren't the functions you asked for, but think about how Java will traverse the TreeSet.