最有效地在TreeMap中查找第N个键

时间:2012-03-12 16:26:31

标签: performance scala treemap

我有一个Scala TreeMap可以自动对键进行排序。我想知道的是,如果在地图中找到第N个密钥的方式比以下示例更有效:

treeMap.take(N).lastKey

谢谢, 布鲁斯

修改
我使用以下代码创建了一个小测试:

class Test {
    var treeMap = new scala.collection.immutable.TreeMap[Double,String]()
    val numberOfEntries = 1000
    (0 until numberOfEntries) map { i => {treeMap += {i.toDouble -> i.toString}}}
    val iterations = 2000
    var N = 1

    while(N < numberOfEntries) {

        // my original version
        var i = 0
        val start1 = System.nanoTime()
        while(i < iterations) {
            i += 1
            val v = treeMap.take(N).lastKey
        }
        val end1 = System.nanoTime()
        val elapsed1 = end1 - start1

        // Daniel's suggestion
        i = 0
        val start2 = System.nanoTime()
        while(i < iterations) {
            i += 1
            val v = treeMap.keysIterator.drop(N - 1).next
        }
        val end2 = System.nanoTime()
        val elapsed2 = end2 - start2

        println("N = %d, elapsed1 = %d, elapsed2 = %d".format(N,elapsed1,elapsed2))
        N += 50
    }

}

object Test {
  def main(args:Array[String]) {
    val test = new Test
  }
}

似乎丹尼尔的建议确实更好

结果

N = 1, elapsed1 = 956492000, elapsed2 = 700300000
N = 51, elapsed1 = 1103271000, elapsed2 = 936045000
N = 101, elapsed1 = 1286896000, elapsed2 = 1041744000
N = 151, elapsed1 = 1368854000, elapsed2 = 1199766000
N = 201, elapsed1 = 1584878000, elapsed2 = 1333284000
N = 251, elapsed1 = 1790965000, elapsed2 = 1468806000
N = 301, elapsed1 = 2052298000, elapsed2 = 1649021000
N = 351, elapsed1 = 2294625000, elapsed2 = 1819525000
N = 401, elapsed1 = 2529855000, elapsed2 = 1961699000
N = 451, elapsed1 = 2762582000, elapsed2 = 2100127000
N = 501, elapsed1 = 2977613000, elapsed2 = 2232108000
N = 551, elapsed1 = 3211812000, elapsed2 = 2384940000
N = 601, elapsed1 = 3437116000, elapsed2 = 2539431000
N = 651, elapsed1 = 3652749000, elapsed2 = 2650910000
N = 701, elapsed1 = 3900431000, elapsed2 = 2807085000
N = 751, elapsed1 = 4123141000, elapsed2 = 2934904000
N = 801, elapsed1 = 4337909000, elapsed2 = 3060158000
N = 851, elapsed1 = 4554490000, elapsed2 = 3188378000
N = 901, elapsed1 = 4768488000, elapsed2 = 3306528000
N = 951, elapsed1 = 4978839000, elapsed2 = 3413813000

2 个答案:

答案 0 :(得分:3)

抛开Scala和TreeMaps,我不确定是否有比O(n)算法更好的方法来查找树中的第N个项目。对于任何节点M,为了确定M需要多少个孩子下降到每个孩子。因此,要计算前N个节点,您需要下降到前N个叶子。

答案 1 :(得分:3)

最近,不可变的SortedSetSortedMap进行了大修。版本2.10将进行一些改进,包括改进查找第一个和最后一个元素以及takedropslice

目前,您提出的解决方案take(N).lastKey并不是特别有效。相反,我会做iterator.drop(n - 1).next._1。它也不是特别有效,我在选择之前对两个解决方案进行基准测试。