K-D Trees
K-D Trees
Dr.K.Kartheeban
kd-Trees
x 30,40
(70,70) y 5,25
70,70
(35,45)
(30,40)
x
10,12 50,30
(5,25)
(50,30) y
(10,12) 35,45
Insert Code
FindMin(x-dimension):
x
(35,90) 51,75
(60,80)
(51,75)
y 25,40 70,70
(70,70)
(50,50)
(25,40)
x 55,1
10,30 35,90 60,80
(10,30)
y 1,10 50,50
(1,10)
(55,1)
FindMin
FindMin(y-dimension):
x
(35,90)
(60,80) 51,75
(51,75) y
25,40 70,70
(70,70)
(50,50)
(25,40) x 55,1
10,30 35,90 55,1 60,80
(10,30)
(1,10)
y 1,10 50,50
(55,1) 1,10
FindMin
x
(35,90) 51,75
(60,80)
(51,75)
y 25,40 70,70
(70,70)
(50,50)
(25,40)
x 55,1
10,30 35,90 60,80
(10,30)
y 1,10 50,50
(1,10)
(55,1)
FindMin Code
In BST, we’d
findmin(A.right). cd A
Here, we have to
findmin(A.right, cd)
Everything in Q has Q P
cd-coord < B, and cd B
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.
- Reason: the nearest point to P in space may be far from P in
the tree:
- E.g. NN(52,52):
(35,90) 51,75
(60,80)
(51,75)
25,40 70,70
(70,70)
(50,50)
10,30 35,90 55,1 60,80
(25,40)
Query d
Point Q
If d > dist(C, Q), then no
point in BB(T) can be
Bounding box closer to Q than C. Hence,
T
of subtree no reason to search
rooted at T subtree rooted at T.