implemented kdTree building logic