Skip to content

Commit 6cddcbd

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 81d6917 commit 6cddcbd

File tree

5 files changed

+23
-5
lines changed

5 files changed

+23
-5
lines changed

data_structures/kd_tree/build_kdtree.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import List, 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 set of k-dimensional 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+
right=build_kdtree(points[median_idx + 1 :], depth + 1),
3031
)

data_structures/kd_tree/example/example_usage.py

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from data_structures.kd_tree.build_kdtree import build_kdtree
55
from data_structures.kd_tree.nearest_neighbour_search import nearest_neighbour_search
66

7+
78
def main() -> None:
89
"""
910
Demonstrates the use of KD-Tree by building it from random points
@@ -32,5 +33,6 @@ def main() -> None:
3233
print(f"Distance: {nearest_dist:.4f}")
3334
print(f"Nodes visited: {nodes_visited}")
3435

36+
3537
if __name__ == "__main__":
3638
main()

data_structures/kd_tree/example/hypercube_points.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import numpy as np
22
from typing import Union
33

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

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 List, 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 subtree of this 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 a point and optional left and right children.
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, List, Tuple
22
from .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
@@ -28,7 +31,10 @@ def search(node: Optional[KDNode], depth: int = 0) -> None:
2831

2932
# Calculate the current distance (squared distance)
3033
current_point = node.point
31-
current_dist = sum((query_coord - point_coord) ** 2 for query_coord, point_coord in zip(query_point, current_point))
34+
current_dist = sum(
35+
(query_coord - point_coord) ** 2
36+
for query_coord, point_coord in zip(query_point, current_point)
37+
)
3238

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

0 commit comments

Comments
 (0)