Broken KdNode equals and compareTo causes the KdTree not to return all valid results #159
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
By submitting this pull request I confirm I've read and complied with the below requirements.
Added {Algorithm/DS name} [{Language}]
, notUpdate README.md
orAdded new code
.I haven't done the above, so won't mark the checklist as the CONTRIBUTING.md file DOES NOT EXIST.
TL;DR; Calling
kdTree.nearestNeighbourSearch(K, point)
where K >= the size of the tree should return all the tree elements - but sometimes it doesn't (whether it works or not depends on the points coordinates). This PR fixes it. For more details, read below.So first of all, thank you very much for this repository and all the implementations. I have been looking closely at the KdTree implementation to build my own and after extensive testing I noticed that this implementation is broken. Here is why:
examined
HashSet so we don't visit an already visited branch.Set.contains
method from java relies heavily on equals and hashcode implementations - objects equality must be checked carefully.Which is already incorrect as those could be two completely different points
I have added a test case demonstrating this behavior + this small PR fixing it. I'm only fixing the compareTo method of the KdNode, but frankly also the hashCode implementations should be changed.