Skip to content

Commit 4608a9f

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 1322921 commit 4608a9f

File tree

5 files changed

+14
-11
lines changed

5 files changed

+14
-11
lines changed

data_structures/kd_tree/build_kdtree.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
from data_structures.kd_tree.kd_node import KDNode
22

3-
def build_kdtree(
4-
points: list[list[float]], depth: int = 0
5-
) -> KDNode | None:
3+
4+
def build_kdtree(points: list[list[float]], depth: int = 0) -> KDNode | None:
65
"""
76
Builds a KD-Tree from a list of points.
87
@@ -25,7 +24,7 @@ def build_kdtree(
2524

2625
# Create node and construct subtrees
2726
left_points = points[:median_idx]
28-
right_points = points[median_idx + 1:]
27+
right_points = points[median_idx + 1 :]
2928

3029
return KDNode(
3130
point=points[median_idx],

data_structures/kd_tree/example/hypercube_points.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import numpy as np
22

3+
34
def hypercube_points(
4-
num_points: int, hypercube_size: float, num_dimensions: int
5+
num_points: int, hypercube_size: float, num_dimensions: int
56
) -> np.ndarray:
67
"""
78
Generates random points uniformly distributed within an n-dimensional hypercube.

data_structures/kd_tree/kd_node.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing import Optional
22

3+
34
class KDNode:
45
"""
56
Represents a node in a KD-Tree.

data_structures/kd_tree/nearest_neighbour_search.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from data_structures.kd_tree.kd_node import KDNode
22

3+
34
def nearest_neighbour_search(
4-
root: KDNode | None,
5-
query_point: list[float]
5+
root: KDNode | None, query_point: list[float]
66
) -> tuple[list[float] | None, float, int]:
77
"""
88
Performs a nearest neighbor search in a KD-Tree for a given query point.
@@ -21,10 +21,7 @@ def nearest_neighbour_search(
2121
nearest_dist: float = float("inf")
2222
nodes_visited: int = 0
2323

24-
def search(
25-
node: KDNode | None,
26-
depth: int = 0
27-
) -> None:
24+
def search(node: KDNode | None, depth: int = 0) -> None:
2825
"""
2926
Recursively searches for the nearest neighbor in the KD-Tree.
3027

data_structures/kd_tree/tests/test_kdtree.py

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from data_structures.kd_tree.kd_node import KDNode
55
from data_structures.kd_tree.example.hypercube_points import hypercube_points
66

7+
78
def test_build_kdtree():
89
"""
910
Test that KD-Tree is built correctly.
@@ -23,6 +24,7 @@ def test_build_kdtree():
2324
# Check that the tree is balanced to some extent (simplistic check)
2425
assert isinstance(kdtree, KDNode)
2526

27+
2628
def test_nearest_neighbour_search():
2729
"""
2830
Test the nearest neighbor search function.
@@ -49,6 +51,7 @@ def test_nearest_neighbour_search():
4951
# Check that nodes visited is a non-negative integer
5052
assert nodes_visited >= 0
5153

54+
5255
def test_edge_cases():
5356
"""
5457
Test edge cases such as an empty KD-Tree.
@@ -65,6 +68,8 @@ def test_edge_cases():
6568
assert nearest_dist == float("inf")
6669
assert nodes_visited == 0
6770

71+
6872
if __name__ == "__main__":
6973
import pytest
74+
7075
pytest.main()

0 commit comments

Comments
 (0)