lorebringer
Member
Someone want to take a look at this and figure out why this tree implementation won't work?
Decaying tree.
What does not work mean here? Does it segfault, does it produce the incorrect answer? What's it supposed to do and what does it actually do?
Depending on those answers the issue might be that you never initialise product in your Expression struct, so when you push them into the array at the end you have undefined values for expr.product.
There's no reason not to initialise product when you create an Expression I believe since it looks like you never change the values of the dependent variables. So something like:
Code:
struct Expression
{
int twoPower;
int threePower;
int product;
public:
Expression(int two, int three) {
twoPower = two;
threePower = three;
product = pow(3.0, threePower) * pow(2.0, twoPower);
}
int get_product() const {
return product;
}
int get_two() const {
return twoPower;
}
int get_three() const {
return threePower;
}
};
In this case the stored expressions for me look like this for n = 5:
myExpressions =
{twoPower=0 threePower=0 product=1 }
{twoPower=1 threePower=0 product=2 }
{twoPower=2 threePower=0 product=4 }
{twoPower=0 threePower=1 product=3 }
{twoPower=1 threePower=1 product=6 }
{twoPower=2 threePower=1 product=12 })
Which might be what you are trying to do since it looks like the correct product for 2^twoPower * 3^threePower?