I work with segment trees now. It is known that segment trees has 2^n leaves. Let us imagine an array data
that consists of 5 elements [1, 2, 3, 4, 5] and is basic for a segment tree. We also have an array tree_data
containing the tree itself (the right half of tree_data
contains elements of data
). It would be logical to allocate 5 elements in tree_data
to contain elements from data
, but it is stated that we have to fill the other three elements with zeroes to make the right half of tree_data
consist of 8 elements and contain [1, 2, 3, 4, 5, 0, 0, 0].
Why should we do this instead of saving memory by filling the array with only five elements? Why do we need nodes 7, 14, 15 (see image) to create a segment tree?
Asked
Active
Viewed 57 times
1

coder-coder
- 323
- 4
- 13