Title: Orthogonal range searching
1Orthogonal range searching
2The problem (1-D)
Given a set of points S on the line, preprocess
them to build structure that allows efficient
queries of the from Given an interval Ix1,x2
find all points in S that are in the interval.
2
4
5
7
8
12
15
19
31-D solution
- Build a balanced tree using the points
co-ordinates as the keys.
2
4
5
7
8
12
15
19
7
query O(log nk)
space O(n)
4
12
2
5
8
15
7
19
15
12
8
2
4
5
4The problem (2-D)
Given a set of points S on the plane, preprocess
them to build structure that allows efficient
queries of the from Given an rectangle
Rx1,x2y1,y2 find all points in S that are
in the rectangle.
P3
P8
P5
P2
P6
P4
P7
P1
5Range Trees (2-D)
Maintain the points in a balanced search tree
ordered by x-coor. In each internal node maintain
the points in its subtree in a balanced search
tree ordered by y
P3
P3
P2
P2
P4
P4
P1
P1
P4
P3
P2
P1
6Range Trees (Contd.)
P3
P8
P5
P2
P6
P4
P7
P1
P3
P2
P4
P1
P4
P3
P2
P1
P8
P7
P6
P5
7Range Trees (Contd.)
P3
P8
P5
P2
P6
P4
P7
P1
P3
P2
P4
P1
P4
P3
P2
P1
P8
P7
P6
P5
8Query processing
- Search by the first dimension gives us O(logn)
trees which together contain the output. - We search each of these trees to get the answer
9Analysis (2-D)
- Space O(nlog n)
- Query O(log2 nk)
- Preprocessing O(nlog n)
10Further facts
- Generalizes to d-dimensions
- Query time can be reduced using a technique
called fractional cascading (we may talk about it
later on)
11The dynamic case
- Suppose the set S is not fixed
- We want to be able to insert and delete points,
and make queries intermixed with insertions and
deletions.
12- Easy in 1-D, just use a red-black tree or some
other balance search tree
7
4
12
2
5
8
15
7
19
15
12
8
2
4
5
13P3
P8
P5
P2
P6
P4
P7
P1
P3
P2
P4
P1
P4
P3
P2
P1
P8
P7
P6
P5
14P3
P8
P5
P2
P6
P4
P7
P1
P3
P2
P4
P1
P4
P2
P1
P8
P7
P6
P5
15- May need to rebalance the primary tree by doing a
rotation
y
x
ltgt
A
x
C
y
B
B
C
A
? We have to rebuild the secondary data
structures at x and y
16Dynamic range trees (analysis)
- So we use BB(a) trees
- Then the amortized cost of rebalancing the
primary tree is O(log n) - and we get
Query O(log2 nk)
Insert, delete O(log2 n) (amortized)
17Priority search trees
- Suppose the query is unbouded from below, can we
take advantage of this ?
P10
P11
P3
P12
P9
P8
P5
P2
P6
P4
P7
P1
18P10
P11
P3
P12
P9
P8
P5
P2
P6
P4
P7
P1
A
P1
A
P3
P12
We put the lowest point at the root and the
x-median (A) of the points other than the root
P4
P6
P7
P2
P5
P8
P9
P10
P11
19P10
P11
P3
P12
P9
P8
P5
P2
P6
P4
P7
P1
P1
A
B
A
P4
P7
B
How do we answer a query ?
P8
P6
P5
P2
P11
P10
P9
P3
P12
20P10
P11
P12
P3
P9
P8
P5
P2
P6
P4
P7
P1
P1
A
P4
P7
B
P8
P6
P5
P2
P11
P10
P9
P3
P12
21P10
You stop the search in the when you hit a point
that is not in the answer
P11
P12
P3
P9
P8
P5
P2
P6
P4
P7
P1
P1
A
P4
P4
P7
B
P8
P6
P6
P5
P2
P2
P11
P10
P9
P3
P12
22Priority search trees (analysis)
- query O(log(n) k)
- space O(n)
- preprocessing O(nlogn)
- How do we make them dynamic ?
23Dynamic priority search trees
P10
P11
P3
P12
First put the points at the leaves of a red-black
tree
P9
P8
P5
P2
P6
P4
P7
P1
P2
P3
P12
P1
P4
P10
P11
P5
P8
P6
P7
P9
24Top-down At each internal node put the smallest
point in its subtree not already assigned to an
internal node
P1
P7
P2
How does the insertion go ?
P3
P2
P3
P12
P1
P4
P10
P11
P5
P6
P7
P8
P9
25P10
P11
P3
P12
P9
P8
P5
P2
P6
P4
P7
P1
P1
N
P7
P2
P4
P6
P3
P11
P6
P5
P10
P2
P3
P12
P1
P4
P10
P11
P5
P6
P7
P8
P9
26P10
P11
P3
P12
P9
P8
P5
P2
P6
P4
P7
P1
P1
N
P7
P2
P4
P6
P3
P11
P6
P5
P10
P12
P1
P2
P4
P10
P11
P5
P6
P7
P8
P9
P3
N
27P10
P11
P3
P12
P9
P8
P5
P2
P6
P4
P7
P1
P1
N
P7
P2
P4
P6
P3
P11
P6
P5
P10
N
P12
P1
P2
P4
P10
P11
P5
P6
P7
P8
P9
P3
N
28P10
P11
P3
P12
P9
P8
P5
P2
P6
P4
P7
P1
P1
N
P7
P2
P4
P6
P3
N
P11
P6
P5
P10
P12
P1
P2
P4
P10
P11
P5
P6
P7
P8
P9
P3
N
29P10
P11
P3
P12
P9
P8
P5
P2
P6
P4
P7
P1
P1
N
P7
P2
N
P4
P6
P3
P11
P6
P5
P10
P12
P1
P2
P4
P10
P11
P5
P6
P7
P8
P9
P3
N
30P10
P11
P3
P12
P9
P8
P5
P2
P6
P4
P7
P1
P1
N
N
P7
P2
P4
P6
P3
P11
P6
P5
P10
P12
P1
P2
P4
P10
P11
P5
P6
P7
P8
P9
P3
N
31P10
P11
P3
P12
P9
P8
P5
P2
P6
P4
P7
P1
P1
N
N
P7
P2
P4
P6
P3
P11
P6
P5
P10
P12
P1
P2
P4
P10
P11
P5
P6
P7
P8
P9
P3
N
32P10
P11
P3
P12
P9
P8
P5
P2
P6
P4
P7
P1
N
N
P7
P1
P2
P4
P6
P3
P11
P6
P5
P10
P12
P1
P2
P4
P10
P11
P5
P6
P7
P8
P9
P3
N
33P10
P11
P3
P12
P9
P8
P5
P2
P6
P4
P7
P1
N
N
P7
P1
P2
P4
P6
P3
P11
P6
P5
P10
P12
P1
P2
P4
P10
P11
P5
P6
P7
P8
P9
P3
N
34P10
P11
P3
P12
P9
P8
P5
P2
P6
P4
P7
P1
N
N
P7
P1
P4
P6
P2
P3
P11
P6
P5
P10
P12
P1
P2
P4
P10
P11
P5
P6
P7
P8
P9
P3
N
35P10
P11
P3
P12
P9
P8
P5
P2
P6
P4
P7
P1
N
N
P7
P1
We may also need to rebalance the tree Is this a
problem ?
P4
P6
P2
P3
P11
P6
P5
P10
P12
P1
P2
P4
P10
P11
P5
P6
P7
P8
P9
P3
N
36M
P10
P11
P3
P12
P9
P8
P5
P2
P6
P4
P7
P1
N
N
P7
P1
P4
P6
P2
P3
P11
P6
P5
P10
P12
P1
P2
P4
P10
P11
P5
P6
P7
P8
P9
P3
N
37M
P10
P11
P3
P12
P9
P8
P5
P2
P6
P4
P7
P1
N
N
P7
P1
P4
P6
P2
P3
P11
P6
P5
P10
P12
P1
P2
P4
P10
P11
P6
P7
P8
P9
P3
N
M
P5
38M
P10
P11
P3
P12
P9
P8
P5
P2
P6
P4
P7
P1
N
N
P7
P1
P4
P6
P2
P3
P11
P6
P5
P10
P12
P1
P2
M
P4
P10
P11
P6
P7
P8
P9
P3
N
M
P5
39M
P10
P11
P3
P12
P9
P8
P5
P2
P6
P4
P7
P1
N
N
P7
P1
P4
P6
P2
P3
P11
P6
P5
P10
P12
P1
P2
M
P4
P10
P11
P6
P7
P8
P9
P3
N
M
P5
40M
P10
P11
P3
P12
P9
P8
P5
P2
P6
P4
P7
P1
N
N
P7
P1
P4
P6
P2
P3
P11
P6
P5
P10
P12
P1
P2
M
P4
P10
P11
P6
P7
P8
P9
P3
N
M
P5
41M
P10
P11
P3
P12
P9
P8
P5
P2
P6
P4
P7
N
P1
N
P7
P1
P4
P6
P2
P3
P11
P6
P5
P10
P12
P1
P2
M
P4
P10
P11
P6
P7
P8
P9
P3
N
M
P5
42P
M
P10
P11
P3
P12
P9
P8
P5
P2
P6
P4
P7
P1
N
43P
M
P10
P11
P3
P12
P9
P8
P5
P2
P6
P4
P7
N
P1
N
P7
P1
P4
P2
P6
P3
P11
P6
P5
P10
P12
P2
P1
P
P10
P11
P8
P6
P7
P9
P3
N
M
P
P4
P5
44Rotation, rotations
X
X
ltgt
Y
Z
C
A
B
C
B
A
Z is bubbled down into B or C Y is bubbled up
from A or B
45P
M
P10
P11
P3
P12
P9
P8
P5
P2
P6
P4
P7
N
P1
N
P7
P1
P4
P2
P6
P3
P11
P6
P5
P10
P12
P2
P1
P4
M
P10
P11
P8
P6
P7
P9
P3
N
M
P
P4
P5