Efficient intra- and inter-night linking of asteroid detections using kd-trees