0

I am trying to understand segment trees. This is a great tutorial that shows how to find a minimum in range. However, it is said there that "All levels of the constructed segment tree will be completely filled except the last level. Also, the tree will be a Full Binary Tree because we always divide segments in two halves at every level.". I do not understand how adding is performed? For example, if we add two more elements 6 and 10 - where should they go? Into the right subtree? If yes, there will be 5 not very balanced and halves are not equal. Should I somehow reorder the tree and do computations again?

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
Bob
  • 10,427
  • 24
  • 63
  • 71

1 Answers1

1

This implementation of a segment tree does not support add operation, so it is not possible to add a new element.

kraskevich
  • 18,368
  • 4
  • 33
  • 45
  • Can you give an example what implementation support add operation? – Bob Oct 01 '14 at 18:36
  • You can use balanced binary search tree to store segment tree's nodes to make adding new elements possible. – kraskevich Oct 01 '14 at 18:40
  • But when balancing you will have to recompute distances, which is not very efficient. – Bob Oct 01 '14 at 18:44
  • Recomputation is required only for those nodes that are rotated or are on the path from the root to a new leaf. There are only `O(log N)` such nodes. – kraskevich Oct 01 '14 at 18:47