Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

k-d tree (A k-dimensional tree) #805

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

bomenderick
Copy link

Description

A K-D Tree(also known as a K-Dimensional Tree) is a binary search tree where data in each node is a K-Dimensional point in space. In short, it is a space partitioning data structure for organizing points in a K-Dimensional space in other to facilitate nearest neighbor search of points.
In addition to insert, search, and delete methods, the implementation also supports nearest neighbors search, median finding for insertion in other to keep the k-d tree balanced, and a merge method to combine two k-d trees by collecting their points and building a balanced k-d tree from it.

Read more:

@codecov-commenter
Copy link

codecov-commenter commented Oct 4, 2024

Codecov Report

Attention: Patch coverage is 83.87097% with 55 lines in your changes missing coverage. Please review.

Project coverage is 95.14%. Comparing base (be27f2c) to head (8f06c40).
Report is 6 commits behind head on master.

Files with missing lines Patch % Lines
src/data_structures/kd_tree.rs 83.87% 55 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #805      +/-   ##
==========================================
- Coverage   95.32%   95.14%   -0.18%     
==========================================
  Files         310      311       +1     
  Lines       22488    22771     +283     
==========================================
+ Hits        21437    21666     +229     
- Misses       1051     1105      +54     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@vil02 vil02 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bomenderick: thanks for interesting contribution.

Before this will be merged some work needs to be done. First of all: please add missing tests. Especially the functionality finding the closest point has to be exercised quite brutally.

Could you also describe/document in the code, which operations does your implementation support?


impl<T: PartialOrd + Copy, const K: usize> KDTree<T, K> {
// Create and empty kd-tree
// #[must_use]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// #[must_use]

Comment on lines +47 to +50
// Returns true if point found, false otherwise
pub fn contains(&self, point: &[T; K]) -> bool {
search_rec(&self.root, point, 0)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really needed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Contains is useful to search for the presence of a point in a k-d tree.
Or do you mean the additional call of search_rec?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does contains need to be public?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes for interaction with the outside world. However, contains is just to complete CRUD operations on a k-d tree.
Indeed, the current k-d tree implementation doesn't make use of contains. However, its implementation could help as it is just a synonym of the search method of a k-d tree.
Would you recommend I make it private for now or remove it?

search_rec(&self.root, point, 0)
}

// Returns true if successfully delete a point, false otherwise
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete?

}

// Returns the number of points in a kd-tree
// #[must_use]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// #[must_use]

}

// Returns the depth a kd-tree
// #[must_use]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// #[must_use]

Comment on lines 88 to 90
pub fn depth(&self) -> usize {
depth_rec(&self.root, 0, 0)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed?

}

// Determine whether there exist points in a kd-tree or not
// #[must_use]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// #[must_use]

}

// Returns a kd-tree built from a vector points
// #[must_use]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// #[must_use]


/// Returns a `KDTree` containing both trees
/// Merging two KDTrees by collecting points and rebuilding
// #[must_use]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// #[must_use]

Comment on lines 115 to 120
pub fn merge(&mut self, other: &mut Self) -> Self {
let mut points: Vec<[T; K]> = Vec::new();
collect_points(&self.root, &mut points);
collect_points(&other.root, &mut points);
KDTree::build(points)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really needed?

Copy link

This pull request has been automatically marked as abandoned because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the stale label Nov 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants