I needed to compute sums within a range on an array, so I came across Segment Tree and Fenwick Tree and I noticed that both of these trees query and update with the same asymptotic running time. I did a bit more research, and these 2 data structures seem to do everything at the same speed. Both have linear memory usage (Segment Tree uses twice as much).
Aside from constant factors in running-time/memory and implementations, is there any reason why I would choose one over the other?
I am looking for an objective answer, like some operation that is faster with one than the other, or maybe some restriction one has that the other does not.
I saw 2 other StackOverflow questions about this, but the answers just described both data structures rather than explaining when one might be better than the other.
I read this on Quora. Hope you find it useful.
I found something on cp-Algorithm which might help you.
Segment tree -
Fenwick tree -
answers each query in O(logN)
preprocessing done in O(NlogN)
Pros: the shortest code, good time complexity
Cons: Fenwick tree can only be used for queries with L=1, so it is not applicable to many problems.
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