Sie sind auf Seite 1von 4

Examples

Segment Trees • Closest pair of points in 3D  track aircraft.


• Basic data structure in computational geometry. • 2D Objects on earth (longitude and latitude).
• Computational geometry. • Nearest neighbor  find nearest gas station to current
 Computations with geometric objects. location.
 Points in 1-, 2-, 3-, d-space. • Nearest ship (longitude and latitude).
• Closest pair of points.
• Nearest neighbor of given point.
 Lines in 1-, 2-, 3-, d-space.
• Machine busy intervals.

Segment Trees Segment Tree Application


 Rectangles or more general polygons in 2-space.
• Store intervals of the form [i,j], i < j, i and j are
• VLSI mask verification:Sufficient overlap between rectangles
that represent wires and contact points on components of a integers.
VLSI design.  [i,j] may, for example represent the fact that a
• Finding most-specific matching rule requires finding the machine is busy from time i to time j.
smallest rectangle that contains the point given by the packet’s
• Answer queries of the form: which intervals
(source,dest).
intersect/overlap with a given unit interval [a,a+1].
• Detect conflicting 2-d filters requires rectangle intersection
detection. Filter matches addresses in the rectangle ([8,11],  List all machines that are busy from 2 to 3.
[6,7])
7

8 11
Source address

Segment Tree – Definition Example – Root range = [1,13]


• Binary tree. 1,13
• Each node, v, represents a closed interval. 1,7 7,13
 s(v) = start of v’s range.
 e(v) = end of v’s range. 1,4 4,7 7,10 10,13
 s(v) < e(v).
 s(v) and e(v) are integers. 1,2 2,4 4,5 5,7 7,8 8,10 10,11 11,13
 Root range = [1,n].
• e(v) = s(v) + 1 => v is a leaf node (unit interval). 2,3 3,4 5,6 6,7 8,9 9,10 11,12 12,13
• e(v) > s(v) + 1 =>
 Left child range is [s(v), (s(v) + e(v))/2].
 Right child range is [(s(v) + e(v))/2, e(v)]. Cream colored boxes are leaves/unit intervals.
The Segment Tree T(1,17) Store Interval [3,11]
1-17
mid = 9 1,13
1,7 7,13
1-9 mid = 5 9-17

5-9 9-13 13-17


1,4 4,7 7,10 10,13
1-5

1,2 2,4 4,5 5,7 7,8 8,10 10,11 11,13


1-3 3-5 5-7 7-9 9-11 11-13 13-15 15-17

2,3 3,4 5,6 6,7 8,9 9,10 11,12 12,13


1-2 2-3 3-4 4-5 5-6 6-7 7-8 8-9 9-10 10-11 11-12 12-13 13-14 14-15 15-16 16-17 Unit intervals of [3,11] highlighted.
Each interval [i,j], i < j, is stored in one or more nodes of
the segment tree.

Store Interval [3,11] Store Interval [4,10]


1,13 1,13
1,7 7,13 1,7 7,13

1,4 4,7 7,10 10,13 1,4 4,7 7,10 10,13

1,2 2,4 4,5 5,7 7,8 8,10 10,11 11,13 1,2 2,4 4,5 5,7 7,8 8,10 10,11 11,13

2,3 3,4 5,6 6,7 8,9 9,10 11,12 12,13 2,3 3,4 5,6 6,7 8,9 9,10 11,12 12,13
[3,11] is stored in node p iff all leaves in the subtree rooted Each node of a segment tree contains 0 or more intervals.
at p are highlighted and no ancestor of p satisfies this
property.

Which Nodes Are Stored? Segment Tree Height


1,13 1,13
1,7 7,13 1,7 7,13

1,4 4,7 7,10 10,13 1,4 4,7 7,10 10,13

1,2 2,4 4,5 5,7 7,8 8,10 10,11 11,13 1,2 2,4 4,5 5,7 7,8 8,10 10,11 11,13

2,3 3,4 5,6 6,7 8,9 9,10 11,12 12,13 2,3 3,4 5,6 6,7 8,9 9,10 11,12 12,13

Need to store only those nodes that contain intervals plus the
ancestors of these nodes. Range = [1,n] => Height <= ceil(log2 (n-1)) + 1.
Properties Properties
1,13 1,13
1,7 7,13 1,7 7,13

1,4 4,7 7,10 10,13 1,4 4,7 7,10 10,13

1,2 2,4 4,5 5,7 7,8 8,10 10,11 11,13 1,2 2,4 4,5 5,7 7,8 8,10 10,11 11,13

2,3 3,4 5,6 6,7 8,9 9,10 11,12 12,13 2,3 3,4 5,6 6,7 8,9 9,10 11,12 12,13

[i,j] in node v => [i,j] not in any ancestor of v . [i,j] in node v => [i,j] not in sibling of v .

Top-Down Insert — [3,11]


1,13
1,7 7,13

1,4 4,7 7,10 10,13

1,2 2,4 4,5 5,7 7,8 8,10 10,11 11,13

2,3 3,4 5,6 6,7 8,9 9,10 11,12 12,13

Top-Down Insert Complexity Of Insert


insert(s, e, v) 1,13
{// insert [s,e] into subtree rooted at v 1,7 7,13
if (s <= s(v) && e(v) <= e)
add [s,e] to v; // interval spans node range 1,4 4,7 7,10 10,13
else {
1,2 2,4 4,5 5,7 7,8 8,10 10,11 11,13
if (s < (s(v) + e(v))/2)
insert(s,e,v.leftChild); 2,3 3,4 5,6 6,7 8,9 9,10 11,12 12,13
if (e > (s(v) + e(v))/2)
insert(s,e,v.rightChild); Let L and R, respectively, be the leaves for [s,s+1]
} and [e – 1,e].
}
Complexity Of Insert Complexity Of Insert

1,13 
1,13
   
1,7 7,13 1,7 7,13

1,4 
4,7 
7,10 
10,13 
1,4 
4,7 
7,10 
10,13
 4,5 5,7
1,2 2,4 7,8 8,10 
10,11 11,13  4,5 5,7
1,2 2,4 7,8 8,10 
10,11 11,13

2,3  5,6 6,7


3,4 8,9 9,10 11,12 12,13 2,3  5,6 6,7
3,4 8,9 9,10 11,12 12,13
In the worst-case, L, R, all ancestors of L and R, and
possibly the other child of each of these ancestors are
Complexity is O(log n).
reached.

Top-Down Delete Search [a,a+1]


delete(s, e, v)
{// delete [s,e] from subtree rooted at v • Follow the unique path from the root to the
if (s <= s(v) && e(v) <= e) leaf node for the interval [a,a+1].
delete [s,e] from v; // interval spans node range • Report all segments stored in the nodes on
else { this unique path.
if (s < (s(v) + e(v))/2) • No segment is reported twice, because no
delete(s,e,v.leftChild); segment is stored in both a node and the
if (e > (s(v) + e(v))/2) ancestor of this node.
delete(s,e,v.rightChild);
}
}

Search – [5,6]


1,13
1,7 7,13

1,4 
4,7 7,10 10,13


1,2 2,4 4,5 5,7 7,8 8,10 10,11 11,13

2,3  6,7
3,4 5,6 8,9 9,10 11,12 12,13

O(log n + s), where s is the #


of segments in the answer.

Das könnte Ihnen auch gefallen