How is the memory of the array of segment tree 2 * 2 ^(ceil(log(n))) - 1?
What is happening here is, if you have an array of n elements, then the segment tree will have a leaf node for each of these n entries. Thus, we have (n) leaf nodes, and also (n-1) internal nodes.
Total number of nodes= n + (n-1) = 2n-1 Now, we know its a full binary tree and thus the height is: ceil(Log2(n)) +1
Total no. of nodes = 2^0 + 2^1 + 2^2 + … + 2^ceil(Log2(n)) // which is a geometric progression where 2^i denotes, the number of nodes at level i.
Formula of summation G.P. = a * (r^size - 1)/(r-1) where a=2^0
Total no. of nodes = 1*(2^(ceil(Log2(n))+1) -1)/(2-1)
= 2* [2^ceil(Log2(n))] -1 (you need space in the array for each of the internal as well as leaf nodes which are this count in number), thus it is the array of size.
= O(4 * n) approx..
You may also think of it this way, Let the below be the segment tree:
10
/ \
3 7
/\ /\
1 2 3 4
If the above is you segment tree, then you array of segment tree will be: 10,3,7,1,2,3,4 i.e. 0th element will store the sum of 1st and 2nd entries, 1st entry will store the sum of 3rd and 4th and 2nd will store the sum of 5th and 6th entry!!
Also, the better explanation is: if the array size n is a power of 2, then we have exactly n-1 internal nodes, summing up to 2n-1 total nodes. But not always, we have n as the power of 2, so we basically need the smallest power of 2 which is greater than n. That means this,
int s=1;
for(; s<n; s<<=1);
You may see my same answer here
Oddly enough, I was reading from the same source as the question when I came upon this. I'll try and answer my best.
Let's start with a basic difference in trees representations (in context only):
The almost "Worst Case" scenario. This one is not completely balanced and not really fun to traverse. Why? Because, with different inputs, different trees might be generated and hence time taken to traverse is not very predictable.
Our "Best Case" scenario. This one is totally balanced or complete and will take a predictable amount of time to traverse, always. Moreover, this tree is also better "hacked".
Now let's get back to our question. [Refer to the first image] We know that for every n-input array (The numbers in green), there will be n-1 internal nodes (The numbers in blue). So a maximum of 2n-1 node space must be allocated.
But the code here does something on the contrary. Why and how?
What you expect: You expect that the memory allocated for 2n-1 nodes should be sufficient. In other words, this should be done:
int *st = new int[2*n - 1];
Assuming the rest of the code works well, this is isn't a very good idea. That's because it creates our unbalanced tree, much like in our first case. Such a tree is not easy to traverse nor easy to apply to problem-solving.
What really happens: We add/pad extra memory with
null
or0
values. We do this:int x = (int)(ceil(log2(n))); //Height of segment tree int max_size = 2*(int)pow(2, x) - 1; //Maximum size of segment tree int *st = new int[max_size];
That is we allocate enough space to generate a balanced complete tree. Such a tree is easy to traverse (using some special modifications) and can be applied to problems directly.
How did we allocate enough memory for case 2? Here's how:
We know there are at least three components in our balanced Segment Tree:
- n numbers from our input array.
- n-1 internal nodes which are mandatorily required.
- The extra space we need to allocate for our padding.
We also know that a balanced tree with k leaves will have:
Combining the two we get the desired outcome:
int x = (int)(ceil(log2(n))); //Height of segment tree int max_size = 2*(int)pow(2, x) - 1; //Maximum size of segment tree int *st = new int[max_size];
Trivia! Raising 2 to the power of x
above, ensures that we get the nearest ceiling integer which is:
- Greater than or equal to
n
(Number of elements in our input array). - Is perfectly and repeatedly divisible by 2, to get a completely balanced 2-ary (binary) tree.
Let the size of input array is n.
All the input array elements will be leaf nodes in segment tree so the number of leaf nodes = n
Since the Segment tree is a complete tree so the Hight of Segment Tree h = ⌈ Log2n ⌉ + 1
Maximum number of nodes in a binary tree of height ‘h’ is 2h-1
So Number of nodes in a segment tree = 2⌈ Log2n ⌉ + 1 -1
Equals to 2*2⌈ Log2n ⌉ -1