I recently discovered and implemented in Java a kdtree with nearest neighbor search, query range search and point search functionality.
My KdTree implementation is non-dynamic (elements cannot be added after construction).
Description of what functionalities I needed for theses kdtree operations
KDTree construction :
- Selection algorithm O(N) in order not to rely on costly O(n*log(n)) sort.
- http://en.wikipedia.org/wiki/Selection_algorithm
Nearest Neighbors :
- When comparing distance, use squared distance instead of the real distance.
Query range search :
- Hyper rect intersection & containment method.
- A rect associated with each Kdtreenode representing the area it can contains.
- You start with an infinite rect and split it on each node cut.
- From a point cut and an active axis, you must be able to compute the “right” and “left” part of the cutted rectangle.
Usage :
KdTrees can be really efficient to reduce algorithms complexity.
For example, an naive implementation of a nearest segment search between 2 non-convex polygons would have a compexity of O(n^2) without a KdTree.
This complexity drops to O(n*log(n)) just by using a KdTree data structure for one of the polygon.
Remarks :
However we must always remember that building a KdTree uses some memory and is time consumming too.
KdTrees should only be used if the number of points is huge. For example, if you do want to use a KdTree to improve queries for 100 points you should better use a bruteforce naive algorithm which will reduce your developement time.