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

Implemented KD Tree Data Structure #11532

Merged
Merged
Changes from 2 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
0d6985c
Implemented KD-Tree Data Structure
Ramy-Badr-Ahmed Aug 28, 2024
6665d23
Implemented KD-Tree Data Structure. updated DIRECTORY.md.
Ramy-Badr-Ahmed Aug 28, 2024
6b3d47e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 28, 2024
4203cda
Create __init__.py
Ramy-Badr-Ahmed Aug 28, 2024
3222bd3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 28, 2024
a41ae5b
Replaced legacy `np.random.rand` call with `np.random.Generator` in k…
Ramy-Badr-Ahmed Aug 28, 2024
1668d73
Replaced legacy `np.random.rand` call with `np.random.Generator` in k…
Ramy-Badr-Ahmed Aug 28, 2024
81d6917
added typehints and docstrings
Ramy-Badr-Ahmed Aug 28, 2024
6cddcbd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 28, 2024
8b238d1
docstring for search()
Ramy-Badr-Ahmed Aug 28, 2024
cd1dd9f
Merge remote-tracking branch 'origin/feature/kd-tree-implementation' …
Ramy-Badr-Ahmed Aug 28, 2024
ead2838
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 28, 2024
543584c
Added tests. Updated docstrings/typehints
Ramy-Badr-Ahmed Aug 28, 2024
ad31f83
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 28, 2024
1322921
updated tests and used | for type annotations
Ramy-Badr-Ahmed Aug 28, 2024
4608a9f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 28, 2024
7c1aa7e
E501 for build_kdtree.py, hypercube_points.py, nearest_neighbour_sear…
Ramy-Badr-Ahmed Aug 29, 2024
ba24e75
I001 for example_usage.py and test_kdtree.py
Ramy-Badr-Ahmed Aug 29, 2024
05975a3
I001 for example_usage.py and test_kdtree.py
Ramy-Badr-Ahmed Aug 29, 2024
31782d1
Update data_structures/kd_tree/build_kdtree.py
Ramy-Badr-Ahmed Sep 3, 2024
6a9b3e1
Update data_structures/kd_tree/example/hypercube_points.py
Ramy-Badr-Ahmed Sep 3, 2024
2fd24d4
Update data_structures/kd_tree/example/hypercube_points.py
Ramy-Badr-Ahmed Sep 3, 2024
2cf9d92
Added new test cases requested in Review. Refactored the test_build_k…
Ramy-Badr-Ahmed Sep 3, 2024
a3803ee
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 3, 2024
f1f5862
Considered ruff errors
Ramy-Badr-Ahmed Sep 3, 2024
ec6559d
Merge remote-tracking branch 'origin/feature/kd-tree-implementation' …
Ramy-Badr-Ahmed Sep 3, 2024
5c07a1a
Considered ruff errors
Ramy-Badr-Ahmed Sep 3, 2024
3c09ac1
Apply suggestions from code review
cclauss Sep 3, 2024
bab43e7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 3, 2024
a10ff15
Update kd_node.py
cclauss Sep 3, 2024
d77a285
imported annotations from __future__
Ramy-Badr-Ahmed Sep 3, 2024
0426806
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions data_structures/kd_tree/tests/test_kdtree.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@


@pytest.mark.parametrize(
("num_points", "cube_size", "num_dimensions", "depth", "expected_result"),
"num_points, cube_size, num_dimensions, depth, expected_result",
[
(0, 10.0, 2, 0, None), # Empty points list
(10, 10.0, 2, 2, KDNode), # Depth = 2, 2D points
(0, 10.0, 2, 0, None), # Empty points list
(10, 10.0, 2, 2, KDNode), # Depth = 2, 2D points
(10, 10.0, 3, -2, KDNode), # Depth = -2, 3D points
],
)
Expand All @@ -24,11 +24,13 @@ def test_build_kdtree(num_points, cube_size, num_dimensions, depth, expected_res
- Positive depth value.
- Negative depth value.
"""
points = hypercube_points(num_points, cube_size, num_dimensions).tolist() \
if num_points > 0 \
points = (
hypercube_points(num_points, cube_size, num_dimensions).tolist()
if num_points > 0
else []
)

kdtree = build_kdtree(points, depth = depth)
kdtree = build_kdtree(points, depth=depth)

if expected_result is None:
# Empty points list case
Expand All @@ -38,12 +40,14 @@ def test_build_kdtree(num_points, cube_size, num_dimensions, depth, expected_res
assert kdtree is not None, "Expected a KDNode, got None"

# Check if root has correct dimensions
assert len(kdtree.point) == num_dimensions, \
f"Expected point dimension {num_dimensions}, got {len(kdtree.point)}"
assert (
Ramy-Badr-Ahmed marked this conversation as resolved.
Show resolved Hide resolved
len(kdtree.point) == num_dimensions
), f"Expected point dimension {num_dimensions}, got {len(kdtree.point)}"

# Check that the tree is balanced to some extent (simplistic check)
assert isinstance(kdtree, KDNode), \
f"Expected KDNode instance, got {type(kdtree)}"
assert isinstance(
Ramy-Badr-Ahmed marked this conversation as resolved.
Show resolved Hide resolved
kdtree, KDNode
), f"Expected KDNode instance, got {type(kdtree)}"


def test_nearest_neighbour_search():
Expand Down