Interval Trees - PowerPoint PPT Presentation

About This Presentation
Title:

Interval Trees

Description:

Intervals with ri v.pt are stored in the left subtree of v. ... 2n suffices to avoid having to handle empty degree 2 nodes. LL Rotation ... – PowerPoint PPT presentation

Number of Views:369
Avg rating:3.0/5.0
Slides: 29
Provided by: sah19
Category:

less

Transcript and Presenter's Notes

Title: Interval Trees


1
Interval Trees
  • Store intervals of the form li,ri, li lt ri.
  • An interval is stored in exactly 1 node.
  • O(n) nodes.
  • 3 versions.
  • Differing capability.

2
Version 1
  • Store intervals of the form li,ri, li lt ri.
  • At least 1 interval per node.
  • Static interval set.
  • Report all intervals that intersect/overlap a
    given interval l,r.

3
DefinitionVersion 1
  • A binary tree.
  • Each node v has a point v.pt and two lists v.left
    and v.right.
  • u.pt lt v.pt for nodes u in left subtree of v.
  • u.pt gt v.pt for nodes u in right subtree of v.
  • So, it is a binary search tree on pt.

4
DefinitionVersion 1
  • Intervals with ri lt v.pt are stored in the left
    subtree of v.
  • Intervals with li gt v.pt are stored in the right
    subtree of v.
  • Intervals with li lt v.pt lt ri are stored in v.
  • v.left has these intervals sorted by li.
  • v.right has these intervals sorted by ri.

5
Example
  • v.pt 4
  • L a, e
  • R d
  • v.left c, f, b
  • v.right c,b,f

6
Properties
  • Each interval is stored in exactly one node.
  • Each node stores between 1 and n intervals.
  • Number of nodes is O(n).
  • Sum of sizes of left and right lists is O(n).
  • Tree height depends on how you choose the points
    v.pt.

7
Selection of v.pt
  • v is the median of the end points of the
    intervals stored in the subtree rooted at v.
  • End points 1, 2, 3, 4, 5, 6, 7
  • Use 4 as v.pt.

8
Selection of v.pt
  • With median selection, tree height is O(log n).
  • Median selection is possible only for static
    interval set. So, no inserts/deletes.
  • Could relax to support insert/delete.

9
Find All Overlapping Intervals
  • Query interval is l,r.
  • v.pt e l,r
  • All intervals in v overlap.
  • Search L and R for additional overlapping
    intervals.

10
Find All Overlapping Intervals
  • v.pt lt l
  • Intervals in v with ri gt l overlap.
  • No interval in L overlaps.
  • Search R for additional overlapping intervals.

11
Find All Overlapping Intervals
  • v.pt gt r
  • Intervals in v with li lt r overlap.
  • No interval in R overlaps.
  • Search L for additional overlapping intervals.

12
Find All Overlapping Intervals
  • Complexity
  • O(log n output) nodes encountered.
  • All intervals in v overlap.
  • Intervals in v with ri gt l overlap.
  • Intervals in v with li lt r overlap.
  • O(log n output) when v.left and v.right are
    sorted arrays.

13
Version 2
  • Store intervals of the form li,ri, li lt ri.
  • Empty nodes permitted.
  • Inserts and deletes.
  • Answer queries of the form which intervals
    include the point d.

14
Inserts Deletes
  • Difficult in version 1 because v.pt is median.
  • Select v.pt (almost) arbitrarily and use a
    red-black tree.
  • Each node stores between 0 and n intervals.
  • At most 2n nodes permissible.
  • Tree height is O(log n).
  • Interval placement rule is same as for Version 1.

15
Need For Empty Nodes
  • Deletion from a degree 2 node.

16
Why Upto 2n Nodes Permissible
  • When number of nodes gt 2n, at least 1 degree 0 or
    degree 1 node must be empty.
  • Empty degree 0 and 1 nodes are easily removed.
  • So, no need to keep them around.
  • 2n suffices to avoid having to handle empty
    degree 2 nodes.

17
LL Rotation
  • Intervals change only for A and B.
  • Those intervals of A that include B.pt need to be
    moved into B.

18
Remaining Rotations
  • All insert/delete rotations require relocating
    intervals from O(1) nodes.
  • O(1) rotations per insert/delete.
  • Complexity of insert/delete is O(f(n) log n),
    where f(n) is time needed to relocate O(n)
    intervals from one node to another.

19
All intervals that contain d
  • d v.pt
  • All intervals in v.
  • Done!

20
All intervals that contain d
  • d lt v.pt
  • Intervals in v with left end point lt d.
  • No interval in R contains d.
  • Search L for additional overlapping intervals.
  • d gt v.pt
  • Similar
  • O(log n output)

21
Version 3
  • Store intervals of the form li,ri, li lt ri.
  • Exactly 1 interval per node.
  • Inserts and deletes.
  • Report just 1 overlapping interval.

22
Version 3Structure
  • Red-black tree.
  • Each node has exactly one interval v.int and one
    point v.max.
  • v.max max (right) end point of intervals in
    subtree rooted at v.
  • Binary search tree on intervals. So, need an
    ordering relation for intervals.

23
Interval Ordering
  • Ordered by left end points. Tie breaker for equal
    left end points.
  • i and j are two intervals.
  • i lt j iff li lt lj or (li lj and ri gt rj)

24
Example
25
Version 3Search
  • Search for an interval that has an overlap with Q
    l,r
  • If v.interval and Q overlap, done.
  • Otherwise, if v.leftChild.max gt l search
    v.leftChild.
  • Otherwise search v.rightChild.

26
Version 3Search
v.leftChild.max gt l
l
  • r

max
27
Version 3LL Rotation
  • Max values changes only for A and B.
  • A.max maxA.interval.right, BR.max, AR.max.
  • B.max maxB.interval.right, BL.max, A.max.

28
Remaining Rotations
  • All insert/delete rotations require computing max
    for O(1) nodes.
  • O(1) rotations per insert/delete.
  • Complexity of insert/delete is O(log n).
Write a Comment
User Comments (0)
About PowerShow.com