Segment Tree and Interval Tree
Segment Tree and Interval Tree
Lekcija 11
Sergio Cabello
[email protected]
FMF
Univerza v Ljubljani
I segment trees
• stabbing queries
• windowing problem
• rectangle intersection
I interval trees
• improvement for some problems
I higher dimension
I in Rd
• data: a set of n isothetic (axis-parallel) boxes
Object
Bounding box
I windowing queries
• data: a set of n disjoint segments in R2
rectangle R.
I motivation: zoom in maps
b4
b1
b3
b5
b2
line l
• in R: report the segments that intersect a query point
reported
reported
reported
l
• query time: O(log n + k)
• space usage: O(n log n)
• preprocessing time: O(n log n)
Sergio Cabello RC – More trees
Notations
• [e
2n , ∞]
I these are the leaves of the segment tree
v v0
Iu
Iv Iv 0
E
s
root
root
root
E
s
Algorithm Insert(u, s)
Input: root u of T , segment s. Endpoints of s have x–coordinates
x− < x+
1. if Iu ⊂ [x − , x + ]
2. then insert s into the list storing S(u)
3. else
4. if [x − , x + ] ∩ Iu.left 6= ∅
5. then Insert(u.left, s)
6. if [x − , x + ] ∩ Iu.right 6= ∅
7. then Insert(u.right, s)
u v u0
I then Iv .parent ⊂ [x − , x + ]
I so s cannot be stored at v
Sergio Cabello RC – More trees
Analysis
I inclusion
of the boxes
• at a given time, only rectangles that intersect l are in the
segment tree
• we can perform insertion and deletions in a segment tree in
O(log n) time
I each time a vertex of a box is encountered, perform a stabbing
query in the segment tree
I in Rd , a set B of n boxes
I for a query point q find all the boxes that contain it
I we use a multi-level data structure, with a segment tree in each
level
I inductive definition, induction on d
I first, we store B in a segment tree T with respect to
x1 –coordinate
I for each node u of T , associate a (d − 1)–dimensional
multi–level segment tree for the segments S(u), with respect to
(x2 , x3 . . . xd )
intersecting `
• relevant values: distance between consecutive events and
the length
• we compute the area to the left of `, updating it at each
event
I use segment trees to maintain the length
I
http://www.cgl.uwaterloo.ca/~krmoule/courses/cs760m/klee
Sergio Cabello RC – More trees
Klee’s measure problem
• space: O(n)
Sl
Sr
xmed
s5 s6
s7
Ml = (s4 , s6 , s1 )
Mr = (s1 , s4 , s6 )
I query time
• size of the subtree divided by at least two at each level
I space usage: O(n) (each segment is stored in two lists, and the
tree is balanced)
I preprocessing time: easy to do it in O(n log n) time
I pseudocode