changed kdtree build to O(n log(n)) algorithm