对于类似地图的类,我当前在字典中保存了一些对象,例如:
{(3,5):<something>, (1,12):<whatever>, (17,2):<example>}
键用元组表示x/y坐标。不是每一个坐标都被占据了,还有“洞”。在
有些方法需要从字典中读出一个区域,例如“从(2,5)到(6,10)的所有对象”。我目前的方法是迭代所有键并返回请求区域中的所有键。由于这些类似地图的字典可以变得相当大,我正在寻找一种更直接的方式来获取这些区域。我考虑了一个OrdneredDict,这样如果迭代超过了请求的区域,我就可以停止迭代,但是也许还有更好的方法呢?在
所描述的问题是一个可以使用K-D tree有效解决的典型问题。在
k-d树(k-dimensional tree的缩写)是一种用于在k维空间中组织点的空间划分数据结构。在你的例子中k等于2。它基本上是一个二叉搜索树,在其中,您可以在每个级别上进行的比较之间进行交替。例如,如果在某个级别上,根据点的x坐标比较点,则在下一个级别上,将使用y坐标比较点。在
因此在树中插入一个点可以在
O(logN)
中完成。这个图像可以演示插入。因此,在数据结构中插入N个点将花费O(NlogN)
时间。在从http://coursera.cs.princeton.edu/algs4/assignments/kdtree.html拍摄的图像
若要查找给定查询矩形中包含的所有点,请从根开始,并使用以下修剪规则递归搜索两个子树中的点:如果查询矩形与节点对应的矩形不相交,则无需探索该节点(或其子树)。只有当子树可能包含查询矩形中包含的点时,才会搜索子树。在
因此,下面的running Java code以最佳的方式解决了这个问题。对于矩形中包含的点的每个查询通常可以在
O(R+logN)
时间内得到回答。其中R是范围内的点数,N是点数。然而,对于病理数据,最坏的运行时间是O(R + sqrt(N))
。在您可以生成所有潜在坐标并检查它们是否存在于}时间的解决方案。在
dict
中,而不是遍历整个dict
中。这将是O(1)
时间与当前{类似这样的方法是可行的(其中
grid
是您的dict
)相关问题 更多 >
编程相关推荐