用 C++ 实现二维 kd 树构造算法

发布于 2024-10-03 18:15:10 字数 197 浏览 5 评论 0原文

我正在开展一个个人项目,用 C++ 实现二维 kd 树构造算法。
我知道有些库已经做到了这一点,但我想获得 C++ 编程经验
(如果您有个人项目要显示,则有助于恢复)

输入:点数以及点本身(可以从命令行读取输入)

我希望它在 O(n log n) 时间内运行,可以这样做吗,如果是的话,有人可以提供一些伪代码来帮助我开始,提前致谢。

I'm working on a personal project to implement a 2-dimensional kd-tree construction algorithm in C++.
I know there are libraries that already do this, but I want to gain experience in C++ programming
(helps in resume if you have personal projects to show)

Input: number of points, and the points themselves (input can be read in from command line)

I want this to run in O(n log n) time, can this be done, if so can someone provide some pseudo-code to help get me started, thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月下凄凉 2024-10-10 18:15:10

我最近一直在玩 kd-trees。这是一些未经测试的代码。 kd树的构建分两个阶段完成;遍历点列表,O(n),并沿每个维度排序,O(nlogn)。所以,是的,你可以在 O(nlogn) 时间内构建 kd 树。

不过,在树中搜索(假设您正在寻找最近的邻居)会变得更加棘手。我还没有找到任何易于理解的文档。

struct Node
{
    int[2] point;
    Node* left;
    Node* right;
}

Node* createtreeRecursive (int** values /* int[2][len] */, int len, int dim = 2, int depth = 0)
{
    // If empty, return
    if (value <= 1) return null;

    // Get axis to split along
    int axis = depth % dim;

    int** sorted = sortAlongDim (values, axis);

    int mid = len / 2;
    Node* curr = new Node ();
    curr.point[0] = sorted [0][mid];
    curr.point[1] = sorted [1][mid];

    int** leftHalf = values;
    int** rightHalf = &(values [mid]);

    curr.left = createtreeRecursive (leftHalf, mid, dim, depth + 1);
    curr.right = createtreeRecursive (rightHalf, len - mid, dim, depth + 1);

    return curr;
}

int** sortAlongDim (int** values, int axis)

I've been playing around with kd-trees recently. Here's some untested code. kd-tree construction in done in 2 stages; traversing the list of points, O(n), and sorting along each dimension, O(nlogn). So, yes, you can construct kd-trees in O(nlogn) time.

Searching through the tree (say you are looking for nearest neighbors), gets trickier though. I haven't found any easy-to-follow documentation for that.

struct Node
{
    int[2] point;
    Node* left;
    Node* right;
}

Node* createtreeRecursive (int** values /* int[2][len] */, int len, int dim = 2, int depth = 0)
{
    // If empty, return
    if (value <= 1) return null;

    // Get axis to split along
    int axis = depth % dim;

    int** sorted = sortAlongDim (values, axis);

    int mid = len / 2;
    Node* curr = new Node ();
    curr.point[0] = sorted [0][mid];
    curr.point[1] = sorted [1][mid];

    int** leftHalf = values;
    int** rightHalf = &(values [mid]);

    curr.left = createtreeRecursive (leftHalf, mid, dim, depth + 1);
    curr.right = createtreeRecursive (rightHalf, len - mid, dim, depth + 1);

    return curr;
}

int** sortAlongDim (int** values, int axis)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文