Segment Trees: (, P), (P, P), (P, P), (P, P), - . - , (P, P), (P, P), (P, )
Segment Trees: (, P), (P, P), (P, P), (P, P), - . - , (P, P), (P, P), (P, )
[Bentley]
and can be used for solving problems e.g. concerning line segments.
1
A segment tree is a balanced tree where each node corresponds to an
interval. The leaves correspond to the atomic intervals according to
left to right order. An internal node u corresponds to the union of the
intervals corresponding to the leaves of the subtree rooted at u.
2
Let int(v) denote the interval corresponding to node v. With each
node v we store a set I(v) ⊆ I: Interval [x, x0 ] is stored in I(v) if and
only if
3
I NSERT S EGMENT T REE(v, [x, x0 ])
1 if int(v) ⊆ [x, x0 ]
2 then add [x, x0 ] to I(v)
3 else if int(lc(v)) ∩ [x, x0 ] 6= 0/
4 then I NSERT S EGMENT T REE(lc(v), [x, x0 ])
5 if int(rc(v)) ∩ [x, x0 ] 6= 0/
6 then I NSERT S EGMENT T REE(rc(v), [x, x0 ])
4
5
Q UERY S EGMENT T REE(v, qx )
1 Report all the intervals in I(v)
2 if v is not a leaf
3 then if qx ∈ int(lc(v))
4 then Q UERY S EGMENT T REE(lc(v), qx )
5 else Q UERY S EGMENT T REE(rc(v), qx )
6
Vertical stabbing queries in a set of disjoint line segments
7
For a node v, let S(v) be the set of segments corresponding to the
intervals in I(v).
8
Lemma: Let S be a set of n disjoint segments in the plane. S can
be stored in a data structure such that the segments in S intersected
by a vertical query segment can be reported in time O(k + (log n)2 ),
where k is the number of reported segments. The data structure uses
O(n log n) storage space and can be built in O(n(log n)2 ) time.
9
Semi-static Segment Trees
10
Klee’s Measure Problem
Given a collection of intervals, what is the length of their union?
http://granmapa.cs.uiuc.edu/~jeffe/open/klee.html
11
Higher-dimensional Segment Trees
12
A d-dimensional segment tree for axis-aligned rectangular boxes in
Rd is an augmented one-dimensional segment tree for the atomic
intervals according to the first dimension.
The secondary data structure associated with a node v is a (d − 1)-
dimensional segment tree according to the remaining coordinates for
the boxes corresponding to the intervals stored at v. More precisely,
it is a (d − 1)-dimensional segment tree for the boxes formed by the
remaining (d − 1) coordinates.
13
Theorem: A d-dimensional segment tree for a set of n axis-aligned
rectangular boxes in Rd can be built in O(n(log n)d ) time and takes
O(n(log n)d ) space.
14