R ^ 3(x,y,z)中

时间:2016-02-17 08:53:32

标签: c++ data-structures tree

我想找到一个内存有效的数据结构,用于搜索R ^ 3坐标的巨大网络,可能是100,000-500,000个坐标。我看过看起来很好的k-d树有没有用于快速随机和顺序访问的良好数据结构?编程语言将是C ++或C.我已经研究过k-d-tree和R ^ * - tree是否有更高效的数据结构?

id x y z

1 0.1 0.05 0.3

2 0.3 0.22 2.3

3 0.1 0.2 3.3

等等

3 个答案:

答案 0 :(得分:3)

假设您需要在任意矩形棱镜内搜索,您应该考虑使用八叉树。 Wiki上有一篇文章,https://en.wikipedia.org/wiki/Octree

答案 1 :(得分:0)

这是如何最快速和记忆有效地通过所有这些找到点的存在或用它组织地图的方法。 您需要为yor 3d坐标类实现特殊的比较对象http://ru.cppreference.com/w/cpp/utility/functional/less。将它用作std :: map或std :: vector依赖于访问要求的模板参数。

template< class T >
struct My3DLess;
bool operator()(const T &lhs, const T &rhs) const
{
    return lhs.x < rhs.x && lhs.y < rhs.y && lhs.z < rhs.z;
}

typedef std::map<My3DPoint,MyMappedObject,My3DLess> My3DMap;

如果你的插入不像fetch那么频繁,你需要使用vector。您可以使用此排序插入函数:

typedef< typename T, typename Pred >
typename std::vector<T>::iterator
    insert_sorted( std::vector<T> & vec, T const& item, Pred pred )
{
    return vec.insert ( std::upper_bound( vec.begin(), vec.end(), item, pred ), item );
}

typedef std::vector<My3DPoint> My3DVector;
My3DVector my3DVector;

// inserting
insert_sorted( my3DVector, my3DPoint, My3DLess() );

// check existance
if ( std::binary_search( my3DVector.begin(), my3DVector.end(), my3DPoint, My3DLess() ) )
    { ... }

答案 2 :(得分:0)

如果内存效率至关重要,我建议像PH-Tree(我自己的实现)这样的东西。根据数据量和维度,它需要比包含相同坐标的普通float[]float[][]更少的空间(例如10M点和> 7维)。对于3维,它需要更多空间,但仍然比大多数其他数据结构更少。

它是四叉树/八叉树与trie(前缀共享)和一些其他算法的混合,使其适合大数据和高维度。

增加的好处:支持非常快速的随机更新,它们只需要比查找更长的时间。查询速度与R-Trees相似。

主要缺点:目前没有C / C ++版本,只有Java版本。此外,代码非常复杂,需要一些翻译工作。另一个缺点是PH-Tree有时有点慢,小数据集的<1,000,000点;它可能会更快(!)更多点。

修改 顺序访问返回稳定的顺序。作为额外的奖励,顺序访问和窗口查询返回z顺序的结果。但是,无法按ID排序。