1
- from typing import Optional , List , Tuple
2
- from .kd_node import KDNode
1
+ from typing import Optional
2
+ from data_structures . kd_tree .kd_node import KDNode
3
3
4
-
5
- def nearest_neighbour_search (
6
- root : Optional [KDNode ], query_point : List [float ]
7
- ) -> Tuple [Optional [List [float ]], float , int ]:
4
+ def nearest_neighbour_search (root : Optional [KDNode ], query_point : list [float ]) -> tuple [Optional [list [float ]], float , int ]:
8
5
"""
9
6
Performs a nearest neighbor search in a KD-Tree for a given query point.
10
7
11
8
Args:
12
9
root (Optional[KDNode]): The root node of the KD-Tree.
13
- query_point (List [float]): The point for which the nearest neighbor is being searched.
10
+ query_point (list [float]): The point for which the nearest neighbor is being searched.
14
11
15
12
Returns:
16
- Tuple [Optional[List [float]], float, int]:
13
+ tuple [Optional[list [float]], float, int]:
17
14
- The nearest point found in the KD-Tree to the query point.
18
15
- The squared distance to the nearest point.
19
16
- The number of nodes visited during the search.
20
17
"""
21
- nearest_point : Optional [List [float ]] = None
18
+ nearest_point : Optional [list [float ]] = None
22
19
nearest_dist : float = float ("inf" )
23
20
nodes_visited : int = 0
24
21
25
22
def search (node : Optional [KDNode ], depth : int = 0 ) -> None :
26
23
"""
27
- Recursively searches the KD-Tree to find the nearest point to the query point .
24
+ Recursively searches the KD-Tree for the nearest neighbor .
28
25
29
26
Args:
30
- node (Optional[KDNode]): The current node being examined.
31
- depth (int): The current depth of the tree, which determines the axis to split on.
32
-
33
- Updates:
34
- nearest_point: The closest point found so far in the KD-Tree.
35
- nearest_dist: The squared distance from the query point to the nearest point found.
36
- nodes_visited: The number of nodes visited during the search.
27
+ node (Optional[KDNode]): The current node in the KD-Tree.
28
+ depth (int): The current depth in the tree.
37
29
"""
38
30
nonlocal nearest_point , nearest_dist , nodes_visited
39
31
if node is None :
@@ -43,18 +35,15 @@ def search(node: Optional[KDNode], depth: int = 0) -> None:
43
35
44
36
# Calculate the current distance (squared distance)
45
37
current_point = node .point
46
- current_dist = sum (
47
- (query_coord - point_coord ) ** 2
48
- for query_coord , point_coord in zip (query_point , current_point )
49
- )
38
+ current_dist = sum ((query_coord - point_coord ) ** 2 for query_coord , point_coord in zip (query_point , current_point ))
50
39
51
40
# Update nearest point if the current node is closer
52
41
if nearest_point is None or current_dist < nearest_dist :
53
42
nearest_point = current_point
54
43
nearest_dist = current_dist
55
44
56
45
# Determine which subtree to search first (based on axis and query point)
57
- k = len (query_point ) # dimensionality of points
46
+ k = len (query_point ) # Dimensionality of points
58
47
axis = depth % k
59
48
60
49
if query_point [axis ] <= current_point [axis ]:
@@ -67,7 +56,7 @@ def search(node: Optional[KDNode], depth: int = 0) -> None:
67
56
# Search the nearer subtree first
68
57
search (nearer_subtree , depth + 1 )
69
58
70
- # If the further subtree has a closer point, search it
59
+ # If the further subtree has a closer point
71
60
if (query_point [axis ] - current_point [axis ]) ** 2 < nearest_dist :
72
61
search (further_subtree , depth + 1 )
73
62
0 commit comments