2.1. Interval Trees

2.1. Interval Trees

The interval tree is used for answering the following one-dimensional (interval/point) stabbing query efficiently :

  • Input: a set S of closed intervals on the line;

  • Query: a single value x q ˆˆ image from book ;

  • Output: all intervals I ˆˆ S with x q ˆˆ I .

For example, in Figure 2.1, there are seven intervals s 1 , s 2 , , s 7 and a query value x q . For convenience, the intervals are illustrated by horizontal line segments s i in 2D and the query value is illustrated as a horizontal line x q . The (interval/point) stabbing query should report the segments s 2 , s 5 , and s 6 . We can assume that a segment s i is represented by the x -coordinate of its left and right endpoints l i and r i , respectively.

image from book
Figure 2.1: An example of the node information in an interval tree. For convenience, the intervals are represented by 2D line segments. The left and right endpoints l i and r i of the segments s i hit by the median x med are represented in sorted lists M L and M R , respectively.

We construct a data structure that answers the above query efficiently. The information of the intervals are stored in a binary tree. Let S denote a set of n intervals [ l i , r i ] for i = 1 , , n . The binary interval tree is constructed recursively, as shown in the construction sketch. A node of the tree is dedicated to a median value of the endpoint of all segments. For all segments that contain this value, there are two lists for checking wether a query value is also covered by those intervals; see Figure 2.1 for an example of the node information. The query operation will be explained in detail later in this chapter.

The following is the interval tree construction algorithm sketch (see also Algorithm 2.1):

  • Input: given a set of intervals S represented by [ l i , r i ] for i = 1 , , n ;

  • If S is empty, then the interval tree is an empty leaf. Otherwise, allocate a node v with two children;

  • For the node v , compute the median x med of { l 1 , , l n , r 1 , , r n }. This means that half of the interval endpoints lie to the left and half of the interval endpoints lie to the right of x med . Note that the median normally will not be equal to a value l i or r i ;

  • Let L med denote the set of intervals to the left of x med , let S med denote the set of intervals that contain x med , and let R med denote the set of intervals to the right of x med ;

    Algorithm 2.1: Computing an interval tree for a set of intervals, recursively.

    image from book

    IntervalTree( S ) ( S is a set of intervals {[ l 1 ,r 1 ] , , [ l n ,r n ]} together with sorted lists for { l i }, { r i }, and { l i } ˆ { r i })

      if   S  =  image from book   then   return  Nil  else   v  := new node  v.S   med  := HitSegments(  v.x   med   ,S  )  v.x   med  := Median(  S  )  R   med  := RightSegments(  v.x   med   ,S  )  L   med  := LeftSegments(  v.x   med   ,S  )  v.M    L   := SortLeftEndPoints(  v.S   med  )  v.M    R   := SortRightEndPoints(  v.S   med  )  v.  LeftChild := IntervalTree(  L   med  )  v.  RightChild := IntervalTree(  R   med  )  return   v   end if  
    image from book
     
  • At the root v , store x med and build a sorted list M L for all left endpoints of S med and a sorted list M R for all right endpoints of S med ;

  • Build the interval trees for L med and R med recursively for the children of v .

Algorithm 2.1 computes the interval tree efficiently, as shown in Lemma 2.1.

Lemma 2.1. The interval tree for a set S of n intervals has size O ( n ) and can be constructed in O ( n log n ) time.

Proof: In a preliminary step, we sort the interval endpoints by l i order, by r i order, and by total ( l i and r i ) order. Obviously, the depth of the constructed tree is in O (log n ). Let n v denote the number of intervals at node v . Computing the median takes O ( n v ) time using the total order of the interval endpoints. Let m v = S med for the set of intervals S med at v . Computing M L and M R from the l i and r i order takes at most O ( m v ) time. Since all occurring sets S med are distinct, this gives O ( n ) altogether. Maintaining the l i , r i , and total order for the recursive steps takes O ( n v ) time. Altogether, with the recursive steps, we have the recursive cost function image from book . Together with the depth O (log n ) of the tree, we conclude the given result.

Next, we consider the recursive stabbing query operation for a value x q ˆˆ image from book and the root v of the interval tree. The median x med of node v is used for binary branching. The node information is used for answering the stabbing query for the intervals that contain the median.

Algorithm 2.2: Answering a stabbing query for an interval tree v and a value x q , recursively.
image from book

IntervalStabbing( v, x q ) ( v is the root of an interval tree, x q ˆˆ image from book )

  D  := new list  if   v.x   med   < x    q    then   L  :=  v.M    L    f  :=  L.  First  while   f    Nil and  f < x    q    do   D.  ListInsert(Seg(  f  ))  f  :=  L.  Next  end while   D   1  := IntervalStabbing(  v.  LeftChild  , x    q   )  else if   v.x   med     x    q    then   R  :=  M    R   (  v  )  l  :=  R.  Last  while   l    Nil and  l > x    q    do   D.  ListInsert(Seg(  l  ))  l  :=  R.  Prev  end while   D   1  := IntervalStabbing(  v.  RightChild  , x    q   )  end if   D  :=  D.  ListAdd(  D   1  )  return   D  
image from book
 

The following is the stabbing query operation sketch:

  • Input: given the root v of an interval tree and the query point x q ˆˆ image from book ;

  • If x q < x med then

    • Scan the sorted list M L of the left endpoints in increasing order and report all stabbed segments. Stop if x q is smaller than the current left endpoint;

    • Recursively, continue with the interval tree of L med ;

  • If x q > x med then

    • Scan the sorted list M R of the right endpoints in decreasing order and report all stabbed segments. Stop if x q is bigger than the current right endpoint;

    • Recursively, continue with the interval tree of R med .

For example, assume that there is a stabbing query at a node v as pointed out in Figure 2.1. Starting from the left with M L , the segments s 5 and s 6 are reported since x q lies to the right of l 5 and l 6 . Then the stabbing query recursively goes on with L med and will find s 2 .

Lemma 2.2. An (interval/point) stabbing query with an interval tree for a set S of n intervals and a value x q ˆˆ image from book report all k intervals I of S with x q ˆˆ I in O ( k + log n ) time.

Proof: Scanning through M R or M L at node v takes time proportional to the number of reported stabbed intervals because we stop as soon as we find an interval that does not contain the point x q . The sets S med of all v are distinct, which gives O ( k ) for all scannings. The size of the considered subtree is divided by at least two at every level, which gives O (log n ) for the path length. Altogether, the given bound holds.

The interval tree was considered in [Edelsbrunner 80] and [McCreight 80]. Note that the interval tree has no direct generalization to higher dimensions, but can support combined queries; see Section 2.6.

A WeakDelete operation for a segment s (see Chapter 10) can be done in O (log n ) time. We find the corresponding node with s ˆˆ S med and mark the endpoints as deleted in the sorted lists M L and M R .

Lemma 2.3. A WeakDelete operation for a segment s in an interval tree of n intervals is performed in O (log n ) time.



Geometric Data Structures for Computer Graphics2006
Geometric Data Structures for Computer Graphics2006
ISBN: N/A
EAN: N/A
Year: 2005
Pages: 69

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net