Docsity

Interval Trees - Advanced Data Structures - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Interval Trees - Advanced Data Structures - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

1

Interval Trees

  • Store intervals of the form [ l i,ri], l i <= ri.
  • An interval is stored in exactly 1 node.
  • So, O(n) nodes.
  • 3 versions.
  • Differing capability.

Version 1

  • Store intervals of the form [ l i,ri], l i <= ri.
  • At least 1 interval per node.
  • Static interval set.
  • Report all intervals that intersect/overlap a

given interval [ l ,r].

2

Definition—Version 1

  • A binary tree.
  • Each node v has a point v.pt and two lists

v.left and v.right.

  • u.pt < v.pt for nodes u in left subtree of v.
  • u.pt > v.pt for nodes u in right subtree of v.
  • So, it is a binary search tree on pt.

Definition—Version 1

  • Intervals with ri < v.pt are stored in the left

subtree of v.

  • Intervals with l i > v.pt are stored in the right

subtree of v.

  • Intervals with l i <= v.pt <= ri are stored in v.

 v.left has these intervals sorted by l i.  v.right has these intervals sorted by ri.

4

Selection of v.pt

  • v is the median of the end points of the

intervals stored in the subtree rooted at v.

1 e

1 a 3

2 c 4

4 b 6

3 f 6

5 d 7

  • End points = {1, 2, 3, 4, 5, 6, 7}
  • Use 4 as v.pt.

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.

5

Find All Overlapping Intervals

  • Query interval is [ l ,r].
  • v.pt ε [ l ,r]

 All intervals in v overlap.  Search L and R for additional overlapping intervals.

4

L R

v

l r

Find All Overlapping Intervals

  • v.pt < l

 Intervals in v with ri >= l overlap.  No interval in L overlaps.  Search R for additional overlapping intervals.

4

L R

v

l r

7

Version 2

  • Store intervals of the form [ l i,ri], l i <= ri.
  • Empty nodes permitted.
  • Inserts and deletes.
  • Answer queries of the form: which intervals

include the point d.

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).

8

Need For Empty Nodes

  • Deletion from a degree 2 node.

20

10

6

2 8

15

40

30

25 35

7

18

Why Upto 2n Nodes Permissible

  • When number of nodes > 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.

10

All intervals that contain d

4

L R

v

  • d = v.pt

 All intervals in v.  Done!

All intervals that contain d

4

L R

v

  • d < v.pt

 Intervals in v with large enough left end point.  No interval in R overlaps.  Search L for additional overlapping intervals.

  • d > v.pt

 Similar

  • O(log n + |output|)

11

Version 3

  • Store intervals of the form [ l i,ri], l i <= ri.
  • Exactly 1 interval per node.
  • Inserts and deletes.
  • Report just 1 overlapping interval.

Version 3—Structure

  • 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.

13

Version 3—Search

  • Search for an interval that has an overlap with Q = [ l ,r]

 If v.interval and Q overlap, done.  Otherwise, if v.leftChild.max >= l search v.leftChild.  Otherwise search v.rightChild.

f,

e,

a,3 c,

d,

b,

Version 3—Search

l r

max

v.leftChild.max >= l

14

Version 3—LL Rotation

  • Max values changes only for A and B.  A.max = max{A.interval.right, BR.max, AR.max}.  B.max = max{B.interval.right, B’L.max, A.max}.

A

B

B’L BR

AR

After insertion.

B

A

After rotation.

BR AR

B’L

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).