One efficient solution is to use Segment Tree that performs both operations in O(Logn) time. An alternative solution is Binary Indexed Tree, which also achieves O(Logn) time complexity for both operations. Compared with Segment Tree, Binary Indexed Tree requires less space and is easier to implement..
Both segment and interval trees store intervals. Segment tree is mainly optimized for queries for a given point, and interval trees are mainly optimized for overlapping queries for a given interval.
Fenwick trees are faster and extremely simple to implement. The asymptotic bounds are equivalent, but the most basic query and update code is almost branchless, non-recursive, and uses very few operations. The segment tree versions of this can be made almost as fast, but this does take extra effort.
Segment Tree is a basically a binary tree used for storing the intervals or segments. Each node in the Segment Tree represents an interval. Consider an array of size and a corresponding Segment Tree : The root of will represent the whole array A [ 0 : N − 1 ] .
All these data structures are used for solving different problems:
Performance / Space consumption for one dimension:
(k is the number of reported results).
All data structures can be dynamic, in the sense that the usage scenario includes both data changes and queries:
Higher dimensions (d>1):
Not that I can add anything to Lior's answer, but it seems like it could do with a good table.
k
is the number of reported results
Operation | Segment | Interval | Range | Indexed |
---|---|---|---|---|
Preprocessing | n logn | n logn | n logn | n logn |
Query | k+logn | k+logn | k+logn | logn |
Space | n logn | n | n | n |
Insert/Delete | logn | logn | logn | logn |
d > 1
Operation | Segment | Interval | Range | Indexed |
---|---|---|---|---|
Preprocessing | n(logn)^d | n logn | n(logn)^d | n(logn)^d |
Query | k+(logn)^d | k+(logn)^d | k+(logn)^d | (logn)^d |
Space | n(logn)^(d-1) | n logn | n(logn)^(d-1)) | n(logn)^d |
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With