Skip to content

Commit ad31f83

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

File tree

6 files changed

+29
-9
lines changed

6 files changed

+29
-9
lines changed

data_structures/kd_tree/build_kdtree.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import Optional
22
from .kd_node import KDNode
33

4+
45
def build_kdtree(points: list[list[float]], depth: int = 0) -> Optional[KDNode]:
56
"""
67
Builds a KD-Tree from a list of points.
@@ -26,5 +27,5 @@ def build_kdtree(points: list[list[float]], depth: int = 0) -> Optional[KDNode]:
2627
return KDNode(
2728
point=points[median_idx],
2829
left=build_kdtree(points[:median_idx], depth + 1),
29-
right=build_kdtree(points[median_idx + 1:], depth + 1),
30-
)
30+
right=build_kdtree(points[median_idx + 1 :], depth + 1),
31+
)

data_structures/kd_tree/example/example_usage.py

+1
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@ def main() -> None:
3232
print(f"Distance: {nearest_dist:.4f}")
3333
print(f"Nodes visited: {nodes_visited}")
3434

35+
3536
if __name__ == "__main__":
3637
main()

data_structures/kd_tree/example/hypercube_points.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import numpy as np
22

3-
def hypercube_points(num_points: int, hypercube_size: float, num_dimensions: int) -> np.ndarray:
3+
4+
def hypercube_points(
5+
num_points: int, hypercube_size: float, num_dimensions: int
6+
) -> np.ndarray:
47
"""
58
Generates random points uniformly distributed within an n-dimensional hypercube.
69

data_structures/kd_tree/kd_node.py

+7-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.
@@ -10,7 +11,12 @@ class KDNode:
1011
right (Optional[KDNode]): The right child node.
1112
"""
1213

13-
def __init__(self, point: list[float], left: Optional['KDNode'] = None, right: Optional['KDNode'] = None) -> None:
14+
def __init__(
15+
self,
16+
point: list[float],
17+
left: Optional["KDNode"] = None,
18+
right: Optional["KDNode"] = None,
19+
) -> None:
1420
"""
1521
Initializes a KDNode with the given point and child nodes.
1622

data_structures/kd_tree/nearest_neighbour_search.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
from typing import Optional
22
from data_structures.kd_tree.kd_node import KDNode
33

4-
def nearest_neighbour_search(root: Optional[KDNode], query_point: list[float]) -> tuple[Optional[list[float]], float, int]:
4+
5+
def nearest_neighbour_search(
6+
root: Optional[KDNode], query_point: list[float]
7+
) -> tuple[Optional[list[float]], float, int]:
58
"""
69
Performs a nearest neighbor search in a KD-Tree for a given query point.
710
@@ -35,7 +38,10 @@ def search(node: Optional[KDNode], depth: int = 0) -> None:
3538

3639
# Calculate the current distance (squared distance)
3740
current_point = node.point
38-
current_dist = sum((query_coord - point_coord) ** 2 for query_coord, point_coord in zip(query_point, current_point))
41+
current_dist = sum(
42+
(query_coord - point_coord) ** 2
43+
for query_coord, point_coord in zip(query_point, current_point)
44+
)
3945

4046
# Update nearest point if the current node is closer
4147
if nearest_point is None or current_dist < nearest_dist:

data_structures/kd_tree/tests/test_kdtree.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@
55
from data_structures.kd_tree.kd_node import KDNode
66
from data_structures.kd_tree.example.hypercube_points import hypercube_points
77

8-
class TestKDTree(unittest.TestCase):
98

9+
class TestKDTree(unittest.TestCase):
1010
def setUp(self):
1111
"""
1212
Set up test data.
1313
"""
1414
self.num_points = 10
1515
self.cube_size = 10.0
1616
self.num_dimensions = 2
17-
self.points = hypercube_points(self.num_points, self.cube_size, self.num_dimensions)
17+
self.points = hypercube_points(
18+
self.num_points, self.cube_size, self.num_dimensions
19+
)
1820
self.kdtree = build_kdtree(self.points.tolist())
1921

2022
def test_build_kdtree(self):
@@ -66,5 +68,6 @@ def test_edge_cases(self):
6668
self.assertEqual(nearest_dist, float("inf"))
6769
self.assertEqual(nodes_visited, 0)
6870

69-
if __name__ == '__main__':
71+
72+
if __name__ == "__main__":
7073
unittest.main()

0 commit comments

Comments
 (0)