Skip to content

Commit cd1dd9f

Browse files
Merge remote-tracking branch 'origin/feature/kd-tree-implementation' into feature/kd-tree-implementation
# Conflicts: # data_structures/kd_tree/nearest_neighbour_search.py
2 parents 8b238d1 + 6cddcbd commit cd1dd9f

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
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

0 commit comments

Comments
 (0)