File tree 5 files changed +14
-11
lines changed
5 files changed +14
-11
lines changed Original file line number Diff line number Diff line change 1
1
from data_structures .kd_tree .kd_node import KDNode
2
2
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 :
6
5
"""
7
6
Builds a KD-Tree from a list of points.
8
7
@@ -25,7 +24,7 @@ def build_kdtree(
25
24
26
25
# Create node and construct subtrees
27
26
left_points = points [:median_idx ]
28
- right_points = points [median_idx + 1 :]
27
+ right_points = points [median_idx + 1 :]
29
28
30
29
return KDNode (
31
30
point = points [median_idx ],
Original file line number Diff line number Diff line change 1
1
import numpy as np
2
2
3
+
3
4
def hypercube_points (
4
- num_points : int , hypercube_size : float , num_dimensions : int
5
+ num_points : int , hypercube_size : float , num_dimensions : int
5
6
) -> np .ndarray :
6
7
"""
7
8
Generates random points uniformly distributed within an n-dimensional hypercube.
Original file line number Diff line number Diff line change 1
1
from typing import Optional
2
2
3
+
3
4
class KDNode :
4
5
"""
5
6
Represents a node in a KD-Tree.
Original file line number Diff line number Diff line change 1
1
from data_structures .kd_tree .kd_node import KDNode
2
2
3
+
3
4
def nearest_neighbour_search (
4
- root : KDNode | None ,
5
- query_point : list [float ]
5
+ root : KDNode | None , query_point : list [float ]
6
6
) -> tuple [list [float ] | None , float , int ]:
7
7
"""
8
8
Performs a nearest neighbor search in a KD-Tree for a given query point.
@@ -21,10 +21,7 @@ def nearest_neighbour_search(
21
21
nearest_dist : float = float ("inf" )
22
22
nodes_visited : int = 0
23
23
24
- def search (
25
- node : KDNode | None ,
26
- depth : int = 0
27
- ) -> None :
24
+ def search (node : KDNode | None , depth : int = 0 ) -> None :
28
25
"""
29
26
Recursively searches for the nearest neighbor in the KD-Tree.
30
27
Original file line number Diff line number Diff line change 4
4
from data_structures .kd_tree .kd_node import KDNode
5
5
from data_structures .kd_tree .example .hypercube_points import hypercube_points
6
6
7
+
7
8
def test_build_kdtree ():
8
9
"""
9
10
Test that KD-Tree is built correctly.
@@ -23,6 +24,7 @@ def test_build_kdtree():
23
24
# Check that the tree is balanced to some extent (simplistic check)
24
25
assert isinstance (kdtree , KDNode )
25
26
27
+
26
28
def test_nearest_neighbour_search ():
27
29
"""
28
30
Test the nearest neighbor search function.
@@ -49,6 +51,7 @@ def test_nearest_neighbour_search():
49
51
# Check that nodes visited is a non-negative integer
50
52
assert nodes_visited >= 0
51
53
54
+
52
55
def test_edge_cases ():
53
56
"""
54
57
Test edge cases such as an empty KD-Tree.
@@ -65,6 +68,8 @@ def test_edge_cases():
65
68
assert nearest_dist == float ("inf" )
66
69
assert nodes_visited == 0
67
70
71
+
68
72
if __name__ == "__main__" :
69
73
import pytest
74
+
70
75
pytest .main ()
You can’t perform that action at this time.
0 commit comments