2d trees




I have uploaded a new snippet to Proving Ground: 2d tree. I just wanted to implement a k-d tree in JavaScript. There are some things which are not necessary (storing the rects in the nodes for instance) but that make some of the operations easier to implement (range and nearest neighbours queries for instance).

This data structure has a lot of applications in fields ranging from games to pattern recognition, anything that may imply or be transformed to a spatial distribution problem.

I have not implemented removal operations to the 2d tree, and the points at the nodes are supposed to be immutable (though they are not in the snippet). That can be easily accomplished using defensive copying. There are good things about immutable objects, which make for another article.