I have found as explained in this article on HackerEarth that segment trees can be implemented by using arrays where child elements of a node positioned at array-index n are at indexes 2n and 2n+1.
Also it states that for storing n elements in my segment tree, I need 2n+1 nodes.
Yet recently when I solved few problems related to segment trees, sometimes my code gave runtime error which got resolved when I changed the array size for storing the segment tree to 4 x (size of array to be stored in segment tree). How can I be sure that a segment tree actually requires 4n-sized array for n elements.
left child(N) = 2N; right child(N) = 2N+1
system works for a simple binary tree but only when starting from N=1, not when starting with N=0. You can fix it up so it does work from N=0, but it is slightly different formulae (LC(N) = 2N+1; RC(N)=2N+2). I'm not clear why you'd need 2N+1 nodes to store a tree with N entries; N is enough if you can allocate so that the first element is indexed at 1, and N+1 is sufficient if you allocate from 0 but only use from 1. Neither is inflated by a factor of 2. […continued…] – Calvities