0

I am trying to build a segment tree in c++. Following the recursive function for the same:

int buildTree(int node,int start,int end,int tree[])
{
        // printf("Node is: %d\n",node);
    printf("start: %d\tend:%d\tnode:%d\t\n",start,end,node);
    if ( start == end )
    {
        // printf("start: %d,node: %d,array[start] : %d\n",start,node,array[start] );
        tree[node] = array[start];  
        return array[start];

    }
    else
    {
        int mid = ( start + end ) / 2;

        buildTree(2 * node ,mid  + 1,end,tree);
        buildTree(2 * node + 1,start,mid,tree);

        tree[node] = tree[ 2 * node ] + tree[ 2 * node + 1 ];
        return tree[node];
    }
}

The array is globally defined:

int array[] = {1,2,3,4,5};

The tree after following call:

int main(int argc, char const *argv[])
{
    int tree[100];
    buildTree(0,0,4,tree);
    for (int i = 0; i < 9; ++i)
    {
        printf("%d : %d\n",i, tree[i]);
    }
    return 0;
}

gives the output:

start: 0    end:4   node:0  
start: 3    end:4   node:0  
start: 4    end:4   node:0  
start: 3    end:3   node:1  
start: 0    end:2   node:1  
start: 2    end:2   node:2  
start: 0    end:1   node:3  
start: 1    end:1   node:6  
start: 0    end:0   node:7  
0 : 15
1 : 6
2 : 3
3 : 3
4 : 474810352
5 : 32766
6 : 2
7 : 1
8 : 0

So, the nodes 4 and 5 are never processed. Where am I doing wrong? I think I am having hard time with recursion.

learner
  • 4,614
  • 7
  • 54
  • 98
  • The right tool to solve such problems is your debugger. You should step through your code line-by-line *before* asking on Stack Overflow. For more help, please read [How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). At a minimum, you should [edit] your question to include a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example that reproduces your problem, along with the observations you made in the debugger. – πάντα ῥεῖ Jun 18 '17 at 07:38
  • You are building `node[0]` over and over again b/c `2*0 == 0`. – n. m. could be an AI Jun 18 '17 at 07:45
  • How can I avoid that? – learner Jun 18 '17 at 07:46
  • You're not processing 4, 5 and 8. As it's not clear what you're expecting as result, it's difficult to help. I presume that you make assumptions on your tree layout in the array which are not correct. Here a variant of your code which better shows the recursivity: http://ideone.com/57Hi7S – Christophe Jun 18 '17 at 08:37
  • By not doing that ;) Either don't use index 0 or find a formula that works for 0-based arrays. – n. m. could be an AI Jun 18 '17 at 08:37
  • Everyone has had a hard time with recursion at some point, if that can make you feel any better. Your debugger is your friend. – Michaël Roy Jun 18 '17 at 09:17

1 Answers1

0

I build a segment tree using somehow the same implementation.

You should call the function buildTree with initial node = 1.

buildTree(1,0,4,tree);

This should fix the error.


Also most of the segment tree implementation codes use the node (2*node) for the range (start -> mid) and the node (2*node+1) for the range(mid+1 -> end).

I think it's a matter of convention. However sticking to the convention makes it easier for you to debug your code, and easier for other programmers to understand it.

So you can rewrite the recursive calls as:

buildTree(2 * node ,start,mid,tree);
buildTree(2 * node + 1,mid+1,end,tree);

Instead of:

buildTree(2 * node ,mid  + 1,end,tree);
buildTree(2 * node + 1,start,mid,tree);
Amr Keleg
  • 336
  • 4
  • 11