Java TreeMap time complexity - lowerKey

你。 提交于 2019-12-13 08:14:46

问题


What is the time complexity of the lowerKey() operation in Java implementation of TreeMap ?

I think it is log(n) but I can't find it anywhere in the documentation.

The complexity of more basic operation is well documented:

This implementation provides guaranteed log(n) time cost for the containsKey, get, put and remove operations.

BTW: I'm also interested in the complexity of subMap(). I guess that a log(n) complexity of lowerKey() will allow log(n) time for constant size subMap().


回答1:


lowerKey() is a search in a balanced binary search tree, so it's obviously O(log n). You might want to read the source code, e.g. from here, to see how the tree is traversed.

Similarly, each operation with a NavigableMap returned from subMap() also requires O(log n) because you will need to traverse the tree to find elements you want.



来源:https://stackoverflow.com/questions/30767130/java-treemap-time-complexity-lowerkey

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!