Hi all,

I'm doing an exercise from a book to design and draw a binary tree, with the constructor simply taking the number of levels as a constructor.

I have it creating the correct number of nodes for the required level, however I am having trouble using the module operator within the constructor.

This code works:

Code:

Binary_tree::Binary_tree(int l)
:levels(l)
{
if(levels>0)
add(Point(100,100));
nodes=pow(2.0, levels)-1;
for(int i = 1; i< nodes; ++i) {
add(Point(point(i-1).x+50, point(i-1).y+50));
}
}

The problem is I obviously want the nodes to go left/right. So I used the modulo remainder to determine if the node is odd/even as such:

Code:

Binary_tree::Binary_tree(int l)
:levels(l)
{
if(levels>0)
add(Point(100,100));
nodes=pow(2.0, levels)-1;
for(int i = 1; i< nodes; ++i) {
if(i%2 == 0)
add(Point(point(i-1).x+50, point(i-1).y+50));
}
}

However, I get a windows error box appearing saying vector subscript out of range? Any ideas?

Thanks.