230 likes | 383 Views
Interval Trees. Store intervals of the form [ l i ,r i ], l i <= r i . Insert and delete intervals. Version 1 Answer queries of the form: which intervals intersect/overlap a given interval [ l ,r] . Version 2—Variant Report just 1 overlapping interval. Definition—Version 1.
E N D
Interval Trees • Store intervals of the form [li,ri], li <= ri. • Insert and delete intervals. • Version 1 • Answer queries of the form: which intervals intersect/overlap a given interval [l,r]. • Version 2—Variant • Report just 1 overlapping interval.
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 li> v.pt are stored in the right subtree of v. • Intervals with li<= v.pt<=ri are stored in v. • v.left has these intervals sorted by li. • v.right has these intervals sorted by ri.
e 1 2 3 f 6 v c d 4 2 5 7 4 a b 1 3 4 6 L R Example • v.pt = 4 • L = {a, e} • R = {d} • v.left = {c, f, b} • v.right = {c,b,f}
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.
e 1 2 3 f 6 d c 2 5 7 4 a b 1 3 4 6 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.
Selection of v.pt • With median selection, tree height is O(log n). • Median selection is possible only for static interval set. • Also, requiring each node to have at least 1 interval, makes it difficult to do a delete.
Relaxation • Select v.pt arbitrarily. • Use a red-black tree. • Each node stores between 0 and n intervals. • At most 2n nodes permissible. • Tree height is O(log n).
20 10 40 6 15 30 18 25 35 2 8 7 Need For Empty Nodes • Deletion from a degree 2 node.
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.
A B B’L B AR A B’L BR BR AR After insertion. After rotation. LL Rotation • Intervals change only for A and B. • Those intervals of A that include B.pt need to be moved into B.
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.
v 4 L R r l 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.
v r l 4 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.
v r l 4 L R Find All Overlapping Intervals • v.pt > r • Intervals in v with li <= r overlap. • No interval in R overlaps. • Search L for additional overlapping intervals.
v r l 4 L R Find All Overlapping Intervals • Complexity • O(log n) nodes encountered • All intervals in v overlap. • Intervals in v with ri >= l overlap. • Intervals in v with li <= r overlap. • O(log n + |output|) when v.left and v.right are sorted arrays.
A Variant • 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.
i i i j j j Interval Ordering • Ordered by left end points. Tie breaker for equal left end points. • i and j are two intervals. • i < j iff li < lj or (li = lj and ri > rj)
e 1 2 3 f 6 d c 2 5 4 7 a b 1 3 4 6 f,7 e,4 d,7 a,3 c,4 b,6 Example
f,7 e,4 d,7 a,3 c,4 b,6 A Variant—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.
A Variant—Search v.leftChild.max >= l r l max
A B B’L B AR A B’L BR BR AR After insertion. After rotation. A Variant—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}.
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).