KD-tree learning

Nice features:

  • Search for points in a rectangular window in O( sqrt( n ) + k )
  • Still O( n ) storage and O( n log n ) construction time

Construction:

struct kdtree{
    Node-data - 数据矢量   数据集中某个数据点,是n维矢量(这里也就是k维)
    Range     - 空间矢量   该节点所代表的空间范围
    split     - 整数       垂直于分割超平面的方向轴序号
    Left      - kd树       由位于该节点分割超平面左子空间内所有数据点所构成的k-d树
    Right     - kd树       由位于该节点分割超平面右子空间内所有数据点所构成的k-d树
    parent    - kd树       父节点  
}

P a set of points, depth the current depth in the tree
BuildKdTree(P,depth)
    if contains only one point
    then return a leaf storing the point
    else if depth is even
    then
        Split P into two subsets with a vertical line l through the median x-coordinate of the points in P. Let A be the set of points to the left of l or on l, and B be the set of points to the right of l.
    else
         Split P into two subsets with a horizontal line l through the median y-cooridnate of the points in P. Let A be the set of points below l  or on l, and B be the set of points above l.
    Create a node v storing l with children left and right
    left  <- BuildKdTree( Adepth + 1 )
    right  <- BuildKdTree( Bdepth + 1 )
    return v

Input:  无序化的点云,维度k
Output:点云对应的kd-tree
Algorithm:
1、初始化分割轴:对每个维度的数据进行方差的计算,取最大方差的维度作为分割轴,标记为r;
2、确定节点:对当前数据按分割轴维度进行检索,找到中位数数据,并将其放入到当前节点上;
3、划分双支:
    划分左支:在当前分割轴维度,所有小于中位数的值划分到左支中;
    划分右支:在当前分割轴维度,所有大于等于中位数的值划分到右支中。
4、更新分割轴:r = (r + 1) % k;
5、确定子节点:
    确定左节点:在左支的数据中进行步骤2;
    确定右节点:在右支的数据中进行步骤2;

Use:

  • v a node, R a range
    SearchKdTree( vR )
    • if v is a leafelse
      • then Report the point stored at v if it lies in R
      • if region( v->left ) is fully contained in Relseif region( v->right ) is fully contained in Relse
        • then ReportSubtree( v->left )
        • if region( v->left ) intersects R
          • then SearchKdTree( v->leftR )
        • then ReportSubtree( v->right )
        • if region( v->right ) intersects R
          • then SearchKdTree( v->rightR )

参考资料:
kD Trees (mit.edu)
KD-Tree原理详解 - 知乎 (zhihu.com)

  • 25
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值