Professional Documents
Culture Documents
KD Trees
KD Trees
KD Trees
CMSC 420
kd-Trees
x 30,40
(35,45)
(5,25)
(50,30) y 35,45
(10,12)
Insert Code
FindMin(x-dimension):
x
(35,90)
(60,80)
51,75
(51,75)
y 25,40 70,70
(70,70)
(50,50)
x 10,30 55,1
(25,40) 35,90 60,80
(10,30)
(1,10)
y 1,10 50,50
(55,1)
FindMin
FindMin(y-dimension):
x
(35,90)
(60,80)
51,75
(51,75)
y 25,40 70,70
(70,70)
(50,50)
x 10,30 55,1
55,1
(25,40) 35,90 60,80
(10,30)
(1,10)
y 1,10
1,10 50,50
(55,1)
FindMin
x
(35,90)
(60,80)
51,75
(51,75)
y 25,40 70,70
(70,70)
(50,50)
x 10,30 55,1
(25,40) 35,90 60,80
(10,30)
(1,10)
y 1,10 50,50
(55,1)
FindMin Code
Point findmin(Node T, int dim, int cd):
// empty tree
if T == NULL: return NULL
In BST, we’d
findmin(A.right). cd A
Here, we have to
findmin(A.right, cd)
Everything in Q has Q P
cd B
cd-coord < B, and
everything in P has cd-
coord ≥ B
Delete in kd-trees --- No Right Subtree
x (x,y)
return t
Nearest Neighbor Searching in kd-trees
• Nearest Neighbor Queries are very common: given a point Q find the
point P in the data set that is closest to Q.
• Doesn’t work: find cell that would contain Q and return the point it
contains.
- E.g. NN(52,52):
(35,90) 51,75
(60,80)
(51,75)
25,40 70,70
(70,70)
(50,50)
10,30 55,1
35,90 60,80
(25,40)
Query
Point Q
d
If d > dist(C, Q), then no
point in BB(T) can be
Bounding box closer to Q than C.
of subtree
T Hence, no reason to search
rooted at T subtree rooted at T.