Binary Tree; sum.
This is my first post here, forgive me if I've done something inappropiate :D
As the title says, it's about binary trees. To be exact, I have to sum up the values of all the nodes. So, using a recursive method seems apparent;
BIG FAT EDIT
Update: I got it working...I think. At least, it doesn't give any errors. But (!), the output isn't right. Here's the entire code (theeval methods in every class are the ones that might've been messed up, the rest should be fine):
publicclass TestVeelterm{
publicstaticvoid main(String [] args){
Veelterm v =new Veelterm();
System.out.println("de veelterm is: " + v);
System.out.println("de waarde voor 2 is: " + v.eval(2));
System.out.println();
Term t;
t =new Term (-2,5);
v.add(t);
System.out.println("de veelterm is: " + v);
System.out.println("de waarde voor 1 is: " + v.eval(1));
System.out.println();
t =new Term (3,3);
v.add(t);
System.out.println("de veelterm is: " + v);
System.out.println("de waarde voor 2 is: " + v.eval(2));
System.out.println();
t =new Term (1,3);
v.add(t);
System.out.println("de veelterm is: " + v);
System.out.println("de waarde voor -3 is: " + v.eval(-3));
System.out.println();
t =new Term (2,5);
v.add(t);
System.out.println("de veelterm is: " + v);
System.out.println("de waarde voor 4 is: " + v.eval(4));
System.out.println();
}
}
class Veelterm{
private TermNode root;
publicvoid add(Term term){
if (root ==null)
root =new TermNode(term);
else
root.add(term);
}
public String toString(){
if (root ==null)
return"0";
else
return root.toString();
}
publicint eval(int x){
if (root ==null)
{
return 0;
}
else
{
return root.eval(x);
}
}
}
class TermNode{
private TermNode left, right;
private Term content;
public TermNode(TermNode l, TermNode r, Term t){
left = l;
right = r;
content = t;
}
public TermNode (Term t){
this(null, null, t);
}
public String toString(){
String antwoord ="";
if (left !=null)
antwoord += left.toString() +" ";
if (content.getCoefficient() >= 0)
antwoord +="+";
antwoord += content.toString();
if (right !=null)
antwoord +=" " + right.toString();
return antwoord;
}
publicvoid add(Term t){
int myexp = content.getExponent();
int termexp = t.getExponent();
if (myexp == termexp){
int sum = t.getCoefficient() + content.getCoefficient();
content =new Term(sum, myexp);
}
elseif (termexp < myexp){
if (left ==null)
left =new TermNode(t);
else
left.add(t);
}
else{
if (right ==null)
right =new TermNode(t);
else
right.add(t);
}
}
publicint eval(int x){
//int sum = t.getCoefficient() + content.getCoefficient()
int z = 0;
int y = 0;
int p = 0;
if (left ==null && right ==null)
{
p = content.getCoefficient();
return p + z + y;
}
else
{
while (left !=null && right !=null){
z = z + content.getCoefficient();
y = y + content.getCoefficient();
}
if (left !=null && right ==null){
z = z + content.getCoefficient();
}
if (right !=null && left ==null){
y = content.getCoefficient();
}
return z + y;
}
}
}
class Term{
privateint coefficient;
privateint exponent;
public Term(int coeff,int exp){
coefficient = coeff;
exponent = exp;
}
publicint getCoefficient(){
return coefficient;
}
publicint getExponent(){
return exponent;
}
public String toString(){
return coefficient +"*X^" + exponent;
}
publicint eval(int x){
int i;
i = 1;
while(i < exponent){
x = x * x;
i++;
}
return x * coefficient;
}
}
Now, as you've noticed already, I'm quite a newbie at java. As with this program, I can write it in pseudocode, but java is a pain. Or at least, I think so. Anyway, to cut to the case; the problem is: how do I add up the value of the nodes when they're not null?
No compiling errors.
Output =
de veelterm is:0
de waarde voor 2 is:0 (answer when x =2)
de veelterm is:-2*X^5
de waarde voor 1 is:-2 (answer when x =1)
de veelterm is: +3*x^2 - 2*X^5
de waarde voor 2 is: -2 (answer when x = 2)
etc
But it should be:
de veelterm is +3*X^2 - 2*X^5
de waarde voor 2 is: -40 (answer when x = 2)
Just ignore the dutch parts *cough*
I've been trying to figure out where I went wrong, and it's probably in the eval method in TermNode, but I can't seem to get it right...
Thanks in advance.
Message was edited by: Anzen...Once a - freaking - gain, sorry :p
Anzen
null

