KdTree节点移除

发布于 2024-12-13 07:01:12 字数 978 浏览 4 评论 0原文

我一直在尝试从头开始实现 KdTree。在成功实现添加、查找最近邻居和查找范围方法中的节点后,我现在陷入了删除节点的困境。

维基百科上描述的方法很模糊而且毫无用处。相反,我使用这些幻灯片作为起点。然而,幻灯片 13 上对删除方法的描述让我感到困惑:

KDNode remove ( KDNode t , Point p , int cd ) {
if ( t == null ) return null;
else if (p.cd < t.data) t.left = remove (t.left , p , cd +1);
else if (p.cd > t.data) t.right = remove (t.right , p , cd +1);
else {
  if (t.right == null && t.left == null ) return null ;
  if (t.right != null )
    t.data = findmin(t.right , cd , cd +1);
  else {
    t.data = findmin (t.left , cd , cd +1);
    t.left = null;
}

t.right = remove (t.right , t . data , cd +1);
return t ;
}}

都将 t.left 替换为 null,将 t.right 替换为 remove(t.right, . ..) 没有任何意义。

这是正确的吗?当我们这样做时,这种方法还有什么问题吗?应该注意的是,与这些幻灯片中描述的方法相反,我将相等的节点放在左侧,而不是右侧。方法还有效吗?

I've been trying to implement a KdTree from scratch. Having successfully implemented add-, find nearest neighbour- and find nodes in range methods I am now stuck on removal of nodes.

The method described on wikipedia is vague and rather useless. Instead I am using these slides as starting point. However, the description of the remove method on slide 13 confuses me:

KDNode remove ( KDNode t , Point p , int cd ) {
if ( t == null ) return null;
else if (p.cd < t.data) t.left = remove (t.left , p , cd +1);
else if (p.cd > t.data) t.right = remove (t.right , p , cd +1);
else {
  if (t.right == null && t.left == null ) return null ;
  if (t.right != null )
    t.data = findmin(t.right , cd , cd +1);
  else {
    t.data = findmin (t.left , cd , cd +1);
    t.left = null;
}

t.right = remove (t.right , t . data , cd +1);
return t ;
}}

Both replacing t.left with null and t.right with remove(t.right, ...) makes no sense.

Is this correct, and while we're at it is there anything else wrong with this method? It should be noted that opposite the method described in these slide, I put equal nodes to the left, instead of the right. Is the method still valid?

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

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

发布评论

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

评论(1

音盲 2024-12-20 07:01:12

当您删除不是叶子的节点时,必须将其替换为子树之一的叶子节点。这意味着叶节点父节点需要获取 NULL 指针,而叶节点本身需要将其指针设置为被替换节点中的那些值。

您需要替换该节点,因为两个子节点都没有使用正确的分割轴,因此如果子树更改级别,则子树无效。最小右值或最大左值仍将在同一轴上划分点,因此用于替换。

When you remove a node that is not a leaf, you must replace it with a leaf node from one of the sub-trees. That means the leaf nodes parent needs to get a NULL pointer and the leaf node itself needs to get its pointers set to those values in the node being replaced.

You need to replace the node because neither child node uses the correct splitting axis, so subtrees are not valid if they change level. A minimum right value or the max left value will still partition the points an the same axis and so get used for the replacement.

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