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

[10815 byte] By [Anzena] at [2007-11-27 8:38:03]
# 1

> ...

> But it should be:

> de veelterm is +3*X^2 - 2*X^5

> de waarde voor 2 is: -40 (answer when x = 2)

>

> ...

When x = 2, the answer should be -52 for (3*(x^2)) - (2*(x^5)).

As for your problem: try to debug your code. You can do that by putting some System.out.println()'s in the code where you think the problem occurs. You should print the values as they're being manipulated so you can follow the flow of your program. You should be able to see why things are not going the way you want them to.

Good luck.

prometheuzza at 2007-7-12 20:35:34 > top of Java-index,Java Essentials,New To Java...
# 2

Thanks for your reply. I added some print.ln's in the eval method in the TermNode class and came to the conclusion that...it doesn't print anything. At all.

I was wondering if the eval method in the Veelterm class is correct. As it being just a pointer, I figured I messed up and it doesn't point anywhere...So I adjusted that bit, and came up with

return root.eval(int x)

instead of

return root.eval(x)

Couldn't omit the identifier...or at least, that's what I thought (think).

But after compiling it gave the error: .class expected and ')' expected.

Hmmm...I'm a bit, lost.

Thanks in advance.

Anzena at 2007-7-12 20:35:34 > top of Java-index,Java Essentials,New To Java...
# 3
> ...>> Hmmm...I'm a bit, lost. > > Thanks in advance.I'll have a closer look at your code tomorrow: it's bedtime for me now.
prometheuzza at 2007-7-12 20:35:34 > top of Java-index,Java Essentials,New To Java...
# 4

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 s = (left == null) ? "" : left.toString() + " + ";

s += content.toString();

s += (right == null) ? "" : " + " + right.toString();

return s;

}

public void 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);

} else if (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);

}

}

public int eval(int x) {

int total = content.eval(x);

if(left != null)

total += left.eval(x);

if(right != null)

total += right.eval(x);

return total;

}

}

class Term {

private int coefficient;

private int exponent;

public Term(int coeff, int exp) {

coefficient = coeff;

exponent = exp;

}

public int getCoefficient() {

return coefficient;

}

public int getExponent() {

return exponent;

}

public String toString() {

boolean skip = coefficient == 1;

String s = skip ? "" : String.valueOf(coefficient) + "*";

return s + "x^" + exponent;

}

public int eval(int x) {

if(x == 0)

return x;

int i = 0;

int product = 1;

while(i++ < exponent)

product *= x;

return product * coefficient;

}

}

crwooda at 2007-7-12 20:35:34 > top of Java-index,Java Essentials,New To Java...
# 5
Thanks a bunch~! Woah, you're a lifesaver, crwood. *whew* :DAlso thanks for your replies, prometheuzz. Quick and good replies, this forum is teh lulz! Everything is solved now, thanks again.
Anzena at 2007-7-12 20:35:34 > top of Java-index,Java Essentials,New To Java...