217

What are differences between segment trees, interval trees, binary indexed trees and range trees in terms of:

  • Key idea/definition
  • Applications
  • Performance/order in higher dimensions/space consumption

Please do not just give definitions.

Michael Petrotta
  • 56,954
  • 26
  • 136
  • 173
Aditya
  • 5,101
  • 4
  • 27
  • 50
  • 13
    It is not a duplicate, That question is if fenwick trees is generalization of interval tress, and my question is more specific and different. – Aditya Jul 04 '13 at 09:08
  • 7
    It has not been answered at http://stackoverflow.com/questions/2795989/are-interval-segment-fenwick-trees-the-same, the answer there just gives definition. – Aditya Jul 04 '13 at 09:10
  • 12
    How is it too broad? "What are some differences between x and y?" is as clear and focused as it gets. This is a very good question. – IVlad Jul 04 '13 at 09:29
  • 17
    And there is no good answer for this available anywhere. A good answer will be great for the community – Aditya Jul 04 '13 at 09:30
  • 23
    Most of these data structures (except Fenwick trees) are reviewed in this pdf: ["Interval, Segment, Range, and Priority Search Trees"](http://www.iis.sinica.edu.tw/~dtlee/dtlee/CRCbook_chapter18.pdf) (by D. T. Lee). Or you can read it as a chapter from this book: ["Handbook of Data Structures and Applications"](http://www.amazon.com/Handbook-Structures-Applications-Computer-Information/dp/1584884355). – Evgeny Kluev Jul 04 '13 at 10:18
  • Not a greatest answer but still worth reading http://www.quora.com/Data-Structures/What-are-the-major-differences-between-segment-trees-interval-trees-binary-indexed-trees-and-range-trees – banarun Jul 04 '13 at 10:21
  • 2
    I read that already before asking the question, its not really good – Aditya Jul 04 '13 at 10:22
  • 1
    If I didn't know about TopCoder et al., this list would look pretty random. Only segment trees and interval trees have the same applications, and there are several divide-and-conquer data structures missing. – David Eisenstat Jul 06 '13 at 13:00
  • 1
    The "Interval, Segment, Range, and Priority Search Trees" (by D. T. Lee) was very helpful. – Sangcheol Choi Aug 16 '13 at 09:33

3 Answers3

346

All these data structures are used for solving different problems:

  • Segment tree stores intervals, and optimized for "which of these intervals contains a given point" queries.
  • Interval tree stores intervals as well, but optimized for "which of these intervals overlap with a given interval" queries. It can also be used for point queries - similar to segment tree.
  • Range tree stores points, and optimized for "which points fall within a given interval" queries.
  • Binary indexed tree stores items-count per index, and optimized for "how many items are there between index m and n" queries.

Performance / Space consumption for one dimension:

  • Segment tree - O(n logn) preprocessing time, O(k+logn) query time, O(n logn) space
  • Interval tree - O(n logn) preprocessing time, O(k+logn) query time, O(n) space
  • Range tree - O(n logn) preprocessing time, O(k+logn) query time, O(n) space
  • Binary Indexed tree - O(n logn) preprocessing time, O(logn) query time, O(n) space

(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:

  • Segment tree - interval can be added/deleted in O(logn) time (see here)
  • Interval tree - interval can be added/deleted in O(logn) time
  • Range tree - new points can be added/deleted in O(logn) time (see here)
  • Binary Indexed tree - the items-count per index can be increased in O(logn) time

Higher dimensions (d>1):

  • Segment tree - O(n(logn)^d) preprocessing time, O(k+(logn)^d) query time, O(n(logn)^(d-1)) space
  • Interval tree - O(n logn) preprocessing time, O(k+(logn)^d) query time, O(n logn) space
  • Range tree - O(n(logn)^d) preprocessing time, O(k+(logn)^d) query time, O(n(logn)^(d-1))) space
  • Binary Indexed tree - O(n(logn)^d) preprocessing time, O((logn)^d) query time, O(n(logn)^d) space
Lior Kogan
  • 18,061
  • 4
  • 49
  • 79
  • 14
    I really get the impression that segment trees < interval trees from this. Is there any reason to prefer a segment tree? E.g. implementation simplicity? – j_random_hacker Jul 24 '13 at 21:36
  • 7
    @j_random_hacker: Segment trees based algorithms have advantages in certain more complex high-dimensional variants of the intervals query. For example, finding which non-axis-parallel line-segments intersect with a 2D window. – Lior Kogan Jul 25 '13 at 16:39
  • 5
    Thanks, I'd be interested in any elaboration you could give on that. – j_random_hacker Jul 25 '13 at 23:17
  • 3
    @j_random_hacker, segment trees have another interesting use: RMQs (range minimum queries) in O(log N) time where N is the overall interval size. – ars-longa-vita-brevis Feb 26 '14 at 06:48
  • @LiorKogan Segment trees should only take up O(n) in one dimension if implemented properly. Use an array of size 2*n. – Nicholas Pipitone Jun 21 '16 at 14:36
  • @NicholasPipitone: This is true when you're using a segment tree for finding sums of a given range. For interval queries, as discussed above, you'll first need to sort the intervals by their endpoints. – Lior Kogan Jun 26 '16 at 09:23
  • Maybe a 1D Binary Indexed Tree can be built in O(n)? https://stackoverflow.com/questions/31068521/is-it-possible-to-build-a-fenwick-tree-in-on?noredirect=1&lq=1 – Thilo Jun 13 '17 at 09:30
  • I think `B+ tree` can absolutely do what `Range` and `binary indexed` tree can do. Am I right? It can also do what interval/segment tree can do but may be not as efficient. – M.kazem Akhgary Feb 07 '18 at 12:33
  • What is the difference of 2d R-tree and 2d segment tree ? – shuva Jun 14 '18 at 12:42
  • I also heard that `interval-trees` are **balanced and more compressed** than `segment trees`. Is that segment tree better as off-line tree and interval-tree be more on-line (where more insertion is going on). – shuva Jun 14 '18 at 12:43
  • 2
    Why are segment trees O(n log n) space? They store N leaves + N /2 + N/4 + ... + N/2^(log N), and this sum is O(N) if I am not mistaken. Also @icc97 answer also reports O(N) space. – Ant Jun 19 '18 at 08:32
  • @LiorKogan Thank you - I had read the comment, but I am not sure how the need to keep things sorted would translate into an increase in space complexity - I would appreciate it if you could elaborate on it! :-) – Ant Jun 19 '18 at 09:26
  • @Ant: See https://en.wikipedia.org/wiki/Segment_tree#Storage_requirements – Lior Kogan Jun 19 '18 at 09:42
  • 1
    @j_random_hacker: See for example http://cs.yazd.ac.ir/farshi/Teaching/CG3921/Slides/chapter10%20-section3.pdf – Lior Kogan Jun 19 '18 at 09:51
  • I used to think that [RMQ (Range Minimum/Maximum Query)](https://www.geeksforgeeks.org/segment-tree-set-1-range-minimum-query/) and [Range Sum Query](https://www.geeksforgeeks.org/segment-tree-set-1-sum-of-given-range/) are main application for Segment Tree ._. – hashlash Oct 30 '19 at 13:23
27

Not that I can add anything to Lior's answer, but it seems like it could do with a good table.

One Dimension

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

Higher Dimensions

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
Cpp Forever
  • 602
  • 6
  • 13
icc97
  • 8,746
  • 6
  • 60
  • 75
  • 2
    What do you mean by reported results ? – Pratik Singhal Feb 01 '16 at 12:23
  • @ps06756 search algorithms often have a runtime of log(n) where n is the inputsize but can yield results that are linear in n which can't be done in logarithmic time (outputting n numbers in log(n) time is not possible). – oerpli Aug 23 '16 at 12:14
  • 1
    Shouldn't Segment Tree have `O(n logn) space` in the first table? – Danny_ds Nov 23 '18 at 15:48
0

The bounds for preprocessing and space for segment trees and binary indexed trees can be improved:

Asad-ullah Khan
  • 739
  • 8
  • 18