kd树(k-dimensional tree)(1)

来源:互联网 发布:外贸软件 知乎 编辑:程序博客网 时间:2024/06/05 00:21

  kd树(k-dimensional树的简称),是一种分割k维数据空间的数据结构。主要应用于多维空间关键数据的搜索(如:范围搜索和最近邻搜索)。
  索引结构中相似性查询有两种基本的方式:一种是范围查询(range searches),另一种是K近邻查询(K-neighbor searches)。范围查询就是给定查询点和查询距离的阈值,从数据集中找出所有与查询点距离小于阈值的数据;K近邻查询是给定查询点及正整数K,从数据集中找到距离查询点最近的K个数据,当K=1时,就是最近邻查询(nearest neighbor searches)。
  一般有两类方法:一类是线性扫描法,即将数据集中的点与查询点逐一进行距离比较,也就是穷举,缺点很明显,就是没有利用数据集本身蕴含的任何结构信息,搜索效率较低,第二类是建立数据索引,然后再进行快速匹配。因为实际数据一般都会呈现出簇状的聚类形态,通过设计有效的索引结构可以大大加快检索的速度。索引树属于第二类,其基本思想就是对搜索空间进行层次划分。根据划分的空间是否有混叠可以分为Clipping和Overlapping两种。前者划分空间没有重叠,其代表就是k-d树;后者划分空间相互有交叠,其代表为R树。
  在数据库系统中,我们可能要查询生日在某个范围中并且薪水在某个范围的人数,这可以看做一个二维的查询,kd树就是为多维空间关键数据的快速查询搜索设计的:

  尽管可以创建一个任何维数的kd树,但是一个kd树中存储的数据必须具有同样的维数,比如你不能存储二维空间的数据在三维空间的kd树中。
  Kd树是二叉树,对于n个点,O(n)的空间和O(lgn)的高度(如果平衡的话)
  
  创建一个二维的kd树(2d树):
  At each step, choose one of the coordinate as a basis of dividing the rest of the points
  • For example, at the root, choose x as the basis
   – Like binary search trees, all items to the left of root will have the x-coordinate less than that of the root
   – All items to the right of the root will have the x-coordinate greater than (or equal to) that of the root
  • Choose y as the basis for discrimination for the root’s children
  • And choose x again for the root’s grandchildren

  其实就是通过x,y对空间不断分割,其实二叉搜索树也是这样的,只不过它是一维的而已。

const int DIMEN = 2;kdNode insert (point p, kdNode t, int dimen) {  if(t == 0)     t = new kdNode(p) ;  else if (p == t.data ) ……  else if (p[dimen] < t.data[dimen])    t.left = insert (p, t.left, (dimen+1)%DIMEN );  else    t.right =insert (p, t.right, (dimen+1)%DIMEN);  return t ;}

• 查找一个指定维数上的最小值,如拥有最小x的值:

Point findmin (kdNodet, int whichAxis, int dimen) {  if(t == 0)     return 0;  else if ( whichAxis == dimen)  {    if (t.left == 0) return t.data;    else     return findmin (t.left, whichAxis,(dimen+1)%DIMEN );  }  else   return minimum(t.data, findmin(t.left, whichAxis,(dimen+1)%DIMEN),findmin(t.right,whichAxis,(dimen+1)%DIMEN)) ;}

• 如果树是平衡的, findmin (root): O(sqrt(n)) time in the worst case.

删除节点:

kdNode remove ( kdNode t, Point p, int dimen) {  if ( t == 0 )     return 0 ;  else if ( p[dimen] < t.data[dimen] )     t.left = remove (t.left, p, (dimen+1)%DIMEN);  else if( p[dimen] > t.data[dimen] )     t.right = remove(t.right, p, (dimen+1)%DIMEN);  else {    if( t.right == 0 && t.left == 0 )       return 0 ;    if( t.right ! = 0 )      t.data = findmin(t.right, dimen, (dimen+1)%DIMEN);    else {      t.data = findmin(t.left, dimen, (dimen+1)%DIMEN);      t.left = 0 ;    }    t.right = remove ( t.right , t.data , (dimen+1)%DIMEN);    return t ;  }}

  删除叶子上的点,如果树是平衡的, remove() takes O(log n) time。

0 0