Geometric Applications of BST
Last updated
Last updated
This is the extension for symbol table.
Insert key-value pair.
Search for key k
.
Delete key k
.
Range search: Find all keys between k1 & k2
.
Range count: Number of keys between k1 & k2
.
Implementation
Un Ordered List: Fast Insertion and slow search
Ordered List: Slow insert, binary search for k1 and k2 to do range search.
Example1:
Java code to find the number of keys between low
and high
Example 2:
Given N horizontal and vertical line segments, find all intersections.
Sweep vertical line between left to right and record your observations
Algorithm:
x-coordinates define events.
h-segment (left endpoint): insert y-coordinate into BST.
h-segment (left endpoint): insert y-coordinate into BST.
v-segment: Range search for interval of y-end points.
Explanation:
Put x-coordinate in PQ (or Sort ) . -------------> N * log(N)
Insert y-coordinate into the BST . -------------> N * log(N)
Remove y-coordinate from the BST -------------> N * log(N)
Range Searches in BST -------------> N * log(N) + R
Data structure to hold the set of overlapping intervals.
Create BST, where each node stores an interval (lo, hi)
.
Use left endpoint as BST key.
Store max endpoint in subtree rooted at node.
If interval in node intersects query interval, return it.
Else if left subtree is null, go right.
Else if max endpoint in left subtree is less than lo, go right.
Else go left.
Implementation: Use a red-black BST ( easy to maintain auxiliary information using log N extra work per op ) to guarantee performance.
Goal: Find all intersections among a set of N orthogonal rectangles.
Here we use the same principle sweep line algorithm. Sweep one vertical line from left to right. when you encounter left end point insert y interval. and repeat the interval search until you get the right end point.
x-coordinates of left and right endpoints define events.
Maintain set of rectangles that intersect the sweep line in an interval search tree (using y-intervals of rectangle).
Left endpoint: Interval search for y-interval of rectangle; insert y-interval.
Right endpoint: Remove y-interval.
Analysis
Put x-coordinate in PQ (or Sort ) . -------------> N * log(N)
Insert y-coordinate into the BST . -------------> N * log(N)
Reomve y-coordinate from the BST -------------> N * log(N)
Interval Search for y Coordinates -------------> N * log(N) + R * log(N)