The pseudo-code for nearest neighbor (NN) search in Wikipedia is not tractable enough for me. Few more posts available with implementations, but they seem to be language specific. So I'm finding it hard to understand how NN search works. This diagram is taken from https://www.cs.cmu.edu/~ckingsf/bioinfo-lectures/kdtrees.pdf. And I'm trying to understand it with a specific case say query point Q = (52,52). Assume two dim are (x,y) and root level splits by x-dim.
Searching NN:
First, I go down from root to a leaf as if I'm trying to insert the Q; and doing so, the leaf is (55,1). Update a (global) var current_best from INFINITY to (55-52)2 + (1-52)2 = 2610.
Next, I go up to (70,70) and updates current_best from 2610 to 182+182=648. Since this gives better distance, we must probe it's sub-tree: is this correct understanding?
Further, we see node (60,80) doesn't give better result (i.e. no update for current_best).
In the process of going up further, we find root (51,75) gives even better result (current_best gets set to 530). So, applying my understanding, we must check it's other sub-tree.
The (25,40) doesn't yield any better result. What I understand is that we still need to verify the sub-tree of (25,40). However, in this case, since this node uses y-dim, and since Q.y > 40, we need to check only right sub-tree (rooted at (35,90)): is this correct understanding?
In short, what I see is if a node provides better result for current_distance, we must probe both children nodes; and if a node does NOT provide better result, all we can do it ignore one of the sub-tree but must probe the other sub-tree based on the condition (splitting hyperplane by specific dimension). Is this correct understanding?
Finally, I'll really appreciate anyone providing a tractable pseudo-code for NN search for Kd-tree