Computational Geometry: Range Searching and Kd-Trees
Computational Geometry: Range Searching and Kd-Trees
Kd-trees
Computational Geometry
Databases
Database queries
G. Ometer
born: Aug 16, 1954
salary salary: $3,500
19,500,000 19,559,999
date of birth
Computational Geometry Lecture 7: Range searching and kd-trees
Introduction Database queries
Kd-trees 1D range trees
Database queries
Database queries
19,500,000 19,559,999
Data structures
49
23 80
10 37 62 89
3 19 30 49 59 70 89 93
3 10 19 23 30 37 59 62 70 80 93 97
49
23 80
10 37 62 89
3 19 30 49 59 70 89 93
3 10 19 23 30 37 59 62 70 80 93 97
49
23 80
10 37 62 89
3 19 30 49 59 70 89 93
3 10 19 23 30 37 59 62 70 80 93 97
49
23 80
10 37 62 89
3 19 30 49 59 70 89 93
3 10 19 23 30 37 59 62 70 80 93 97
49
split node
23 80
10 37 62 89
3 19 30 49 59 70 89 93
3 10 19 23 30 37 59 62 70 80 93 97
Algorithm 1DRangeQuery(T, [x : x0 ])
1. νsplit ←FindSplitNode(T, x, x0 )
2. if νsplit is a leaf
3. then Check if the point in νsplit must be reported.
4. else ν ← lc(νsplit )
5. while ν is not a leaf
6. do if x ≤ xν
7. then ReportSubtree(rc(ν))
8. ν ← lc(ν)
9. else ν ← rc(ν)
10. Check if the point stored in ν must be reported.
11. ν ← rc(νsplit )
12. Similarly, follow the path to x0 , and . . .
Grey nodes: they occur on only two paths in the tree, and
since the tree is balanced, its depth is O(log n)
Result
49 14
7 7
23 80
4 3 4 3
10 37 62 89
2 2 2 1 2 2 1 2
3 19 30 49 59 70 89 93
1 1 1 1 1 1 1 1 1 1 1 1
3 10 19 23 30 37 59 62 70 80 93 97
49 14
7 7
23 80
4 3 4 3
10 37 62 89
2 2 2 1 2 2 1 2
3 19 30 49 59 70 89 93
1 1 1 1 1 1 1 1 1 1 1 1
3 10 19 23 30 37 59 62 70 80 93 97
Result
Range queries in 2D
Range queries in 2D
Kd-trees
Kd-trees
Kd-trees
`1
`5 `7 `1
p4 p9
p5 `2 `3
p10
`2 p2
`4 `5 `6 `7
p1 p7 `3
`8
p8
p3 `8
p6 p3 p4 p5 `9 p8 p9 p10
`9
`4 `6 p1 p2 p6 p7
Kd-tree construction
Algorithm BuildKdTree(P, depth)
1. if P contains only one point
2. then return a leaf storing this point
3. else if depth is even
4. then Split P with a vertical line ` through the
median x-coordinate into P1 (left of or
on `) and P2 (right of `)
5. else Split P with a horizontal line ` through
the median y-coordinate into P1 (below
or on `) and P2 (above `)
6. νleft ← BuildKdTree(P1 , depth + 1)
7. νright ← BuildKdTree(P2 , depth + 1)
8. Create a node ν storing `, make νleft the left
child of ν, and make νright the right child of ν.
9. return ν
Computational Geometry Lecture 7: Range searching and kd-trees
Kd-trees
Introduction Querying in kd-trees
Kd-trees Kd-tree query time analysis
Higher-dimensional kd-trees
Kd-tree construction
T(1) = O(1)
T(n) = 2 · T(n/2) + O(n)
`1
`1
`2
`3
`2 ν
region(ν) `3
Kd-tree querying
p4 p12
p5
p13
p2
p8 p10 p3 p4 p5 p11 p12 p13
p1 p9
p7 p11
p3
p6 p6
p1 p2
p7 p8 p9 p10
Kd-tree querying
Algorithm SearchKdTree(ν, R)
Input. The root of (a subtree of) a kd-tree, and a range R
Output. All points at leaves below ν that lie in the range.
1. if ν is a leaf
2. then Report the point stored at ν if it lies in R
3. else if region(lc(ν)) is fully contained in R
4. then ReportSubtree(lc(ν))
5. else if region(lc(ν)) intersects R
6. then SearchKdTree(lc(ν), R)
7. if region(rc(ν)) is fully contained in R
8. then ReportSubtree(rc(ν))
9. else if region(rc(ν)) intersects R
10. then SearchKdTree(rc(ν), R)
Kd-tree querying
p4 p12
p5
p13
p2
p8 p10 p3 p4 p5 p11 p12 p13
p1 p9
p7 p11
p3
p6 p6
p1 p2
p7 p8 p9 p10
x y
y y x x
n leaves n leaves
G(1) = 1
1
The depth is log n, so the binary depth is 2 · log n
Important: The logarithm is base-2
Every unary grey node has a unique binary parent (except the
root), so there are at most twice as many unary nodes as
binary nodes, plus 1
Result
Efficiency
√
n log n n
4 2 2
16 4 4
64 6 8
256 8 16
1024 10 32
4096 12 64
1.000.000 20 1000
Higher dimensions
Higher dimensions
Higher dimensions
How does the query time analysis change?
Higher dimensions
x
y y
z z z z
m leaves
G3 (1) = 1
Result