李航老师书上的的算法说明没怎么看懂,看了网上的博客,悟出一套循环(建立好KD树以后的K近邻搜索),我想应该是这样的(例子是李航《统计学习算法》第三章56页;例3.3):
步骤 | 结点查询标记 | 栈内元素(本次循环结束后) | 最近点 | 最近距离 | 说明 | |||||||
A | B | C | D | E | F | G | ||||||
初始化 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ABD | M=空 | Mdis = ∞ | 初始化:先将S所在的区域找到,将经过的各个结点依次加入栈中,将查询标记初始化为0 |
循环 | 1 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | AB | M=D | Mdis = dis(S,D) | 取出栈顶元素D,D被查询,更新D的标记为1,计算S与D的距离,比当前最小值小,更新M=D,Mdis = dis(S,D),计算S到D的超平面的距离是否小于Mdis(这里二维就是以S为圆心,Mdis为半径的圆是否与D那维的直线相交,在这里是相交的),小于,将D两侧的子结点加入栈,但是D两侧没有子结点,不加 |
2 | 0 | 1 | 0 | 1 | 0 | 0 | 0 | A | M=D | Mdis = dis(S,D) | 取出栈顶元素B,B被查询,更新B的标记为1,计算S与B的距离,不比当前最小值小,不更新,计算S到B的超平面的距离是否小于Mdis,大于,那就只加入B的S那一侧的子结点D,子结点D已经被标记为1,已被查询,不加入 | |
3 | 1 | 1 | 0 | 1 | 0 | 0 | 0 | C | M=D | Mdis = dis(S,D) | 取出栈顶元素A,A被查询,更新A的标记为1,计算S与A的距离,不比当前最小值小,不更新,计算S到A的超平面的距离是否小于Mdis,小于,将A两侧的子结点加入,子结点B已经被标记为1,被查询,不加入,子结点C标记为0,加入栈 | |
4 | 1 | 1 | 1 | 1 | 0 | 0 | 0 | E | M=D | Mdis = dis(S,D) | 取出栈顶元素C,C被查询,更新C的标记为1,计算S与C的距离,不比当前最小值小,不更新,计算S到C的超平面的距离是否小于Mdis,大于,只加入C的S那侧的子结点E,子结点E标记为0,没被查询,加入栈 | |
5 | 1 | 1 | 1 | 1 | 1 | 0 | 0 | M=E | Mdis = dis(S,E) | 取出栈顶元素E,E被查询,更新E的标记为1,计算S与E的距离,比当前最小值小,更新M=E,Mdis = dis(S,D),计算S到E的超平面的距离是否小于Mdis,小于,将E两侧的子结点加入栈,但是E两侧没有子结点,不加 | ||
6 | 1 | 1 | 1 | 1 | 1 | 0 | 0 | M=E | Mdis = dis(S,E) | 栈空,循环结束 |
如果有错,还望大佬们能够指正