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。
- kd树(k-dimensional tree)(1)
- KD-树(K-dimension tree)
- KD树 (k-Dimension tree)
- K相邻和Kd tree
- kd-tree和K近邻
- KD(kd tree)树介绍
- k近邻法中kd-tree树的创建
- k近邻算法(KNN)及kd树简介(KD-Tree)
- K近邻,kd树
- K近邻-kd树
- KD-Tree在K-NN的应用
- hdu4347 kd-tree询问k近点
- K-means KNN AND KD-tree
- KD-Tree k近点 HDU 4347
- [bzoj4520][kd-tree]K远点对
- 构造平衡kd树(kd tree)
- BZOJ 1941 Sdoi2010 Hide and Seek K-Dimensional-Tree
- BZOJ 2648 SJY摆棋子 K-Dimensional-Tree
- -写霸-Android 实现字体识别(二)
- 16-m-3Sum Closest
- Splay树(区间更新)—— POJ 3468 A Simple Problem with Integers
- 九度OJ-题目1508:把字符串转换成整数
- 从无头单链表中删除节点
- kd树(k-dimensional tree)(1)
- js页面跳转整理
- 浅析Java中的final关键字
- 线程池相关
- 卡巴斯基遭攻击 Duqu 2.0 翻译
- C++代理实现
- java使用poi读取ppt文件和poi读取excel、word示例
- jdk自带的jvm监测程序
- volley源码解析(一)--volley的使用和架构