0% found this document useful (0 votes)
34 views16 pages

AVL IntervalTree

This document describes an interval tree data structure that allows adding, deleting, and checking for overlap of intervals in logarithmic time. It builds a binary search tree (BST) using the low endpoint of each interval as the key. Each node stores its maximum endpoint value and updates as nodes are inserted or deleted. The Overlap method searches the tree in logarithmic time to find any intervals overlapping a given range by comparing endpoints at each node.

Uploaded by

farzi kaam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views16 pages

AVL IntervalTree

This document describes an interval tree data structure that allows adding, deleting, and checking for overlap of intervals in logarithmic time. It builds a binary search tree (BST) using the low endpoint of each interval as the key. Each node stores its maximum endpoint value and updates as nodes are inserted or deleted. The Overlap method searches the tree in logarithmic time to find any intervals overlapping a given range by comparing endpoints at each node.

Uploaded by

farzi kaam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Interval Tree

Muralidhara V N
IIIT Bangalore
Dynamic Collection of Intervals

• Add(Interval)
• Delete(Interval)
• Overlap(Interval) – does it overlap
with any one of the intervals in the
collection ?

26 39 50 58

1 9 12 17 20 32 40 60
Law of Trichotomy
Given any two intervals [l(a),h(a)] and
[l(b),h(b)], exactly one of the following
should hold.
• A is to the left of B: h(a) < l(b)
A B
Law of Trichotomy
Given any two intervals [l(a),h(a)] and
[l(b),h(b)], exactly one of the following
should hold.
• A is to the left of B: h(a) < l(b)
A B

• B is to the left of A: h(b) <l(a)


B A
Law of Trichotomy
Given any two intervals [l(a),h(a)] and
[l(b),h(b)], exactly one of the following
should hold.
• A is to the left of B: h(a) < l(b)
A B

• B is to the left of A: h(b) <l(a)


B A
• A and B overlap: l(a)<=h(b) and
l(b)<=h(a)
B A B
A
Build a BST

Build a BBST with low end point of the


intervals as the keys in the BST.

For simplicity, will assume that all the


left end points of the intervals are
unique.
Dynamic Collection of Intervals

60 20

17 12 40 60

9 1
39
26 50 58

26 39 50 58

1 9 12 17 20 32 40 60
BBST Structure

struct BST
{
long long int key;
int height;
long long int max, high;
struct BST *left, *right;
};
Max of a node

node->left->max

node->max=Max node->high

node->right-
>max
Updatemax
Void Updatemax (struct BST *node){
if (node)
{
node->max = node->high;

if (node->left && node->left->max > node->high)


node->max = node->left->max;

if (node->right && node->right->max > node->high)


node->max = node->right->max;
}
}
Dynamic Collection of Intervals

60 20

17 12 40 60

9 1
39
26 50 58

26 39 50 58

1 9 12 17 20 32 40 60
Overlap in O(log n)
struct BST * Overlap (struct BST *node, long long int l, long long int
r) {

while (node){
if (l <= node->high && node->key <= r) return node;

if (node->left && node->left->max >= l) node = node->left;

elsenode = node->right;
}
return NULL;
}
Correctness of Overlap
• If we go to the right and did not find any overlap,
then the interval can not overlap with any
interval on the left side.
• when to the right because max on left < l, implies for
any interval on the left side h(a)<l

• If we go to the left and did not find any overlap,


then the interval can not overlap with any
interval on the right side.
Correctness of Overlap
• If we go to the left and did not find any overlap,
then the interval can not overlap with any
interval on the right side.
• Let a be a interval on left with left->max value
• The interval can not overlap with a
• r<l(a) as l <=h(a)
Correctness of Overlap
• If we go to the left and did not find any overlap,
then the interval can not overlap with any
interval on the right side.
• Let a be a interval on left with left->max value
• The interval can not overlap with a
• r<l(a) as l <=h(a)
• Let b be any interval on the right side, then l(a)<l(b)
• Hence r<l(a)<l(b)

l r
l(a) h(a) l(b) h(b)
Interval Trees in O(log n)

• Add()
• Delete()
• Overlap()

All the overlaping intervals can be


found in O(R log n), where are is the
number of intervals that overlap with
the given intervals

You might also like