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 P 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( A, depth + 1 )
right <- BuildKdTree( B, depth + 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( v, R )- 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->left, R )
- then ReportSubtree( v->right )
- if region( v->right ) intersects R
- then SearchKdTree( v->right, R )
- if v is a leafelse