hi all, this is some extra credit from class we sat around cleaning up code today this is what we came up with

public boolean equals2(IntTree t2){
    return equals2(this.overallRoot, t2.overallRoot);
}
private Boolean equals2(IntTreeNode r1, IntTreeNode r2){
    if(r1 == null || r2 == null){
        return r1 == null && r2 == null;
    }
    return ((r1.data==r2.data)&& equals2(r1.left, r2.left) && equals(r1.right, r2.right));
}

we get five points extra credit if we can shorten any further. So my question iscan someone explain to me the ^ character and is that the right path to shortening this code

The ^ character is for bitwise-exclusive-or (xor). This will not help you much. But the logical ^^ version might.

I would like to note that "making the code shorter" isn't always a good thing. True, it's fun for code golf, or for things that actually save you development time, but in this case you're not being productive. I would suggest ignoring the bonus because it's not usefull, unless you have extra time on your hands.

In fact, the very first thing my eye's saw when I looked at it was how to make it faster dispite lengthening the code. Making it faster would be a much more usefull bonus question in my opinion. Who knows, maybe you can explain him the same thing and show much much faster your version is and get the bonus anyways. (I initially saw 3 ways you can improve performance).

But ok, if you want to play code golf, then code golf it is. Consider the following: Say a function always returns a boolean. Say the return statements are nested within an if-statement. Then, my question to you is, are the if statements nessacary?

i should have also stated that this is a practice it question (awsome site btw) so there are guidelines the code has to follow the if statement helps handel three parts in one statement ie if both are null return true if one is null but other is not return false ad vice versa

public boolean equals2(IntTree t2){
    return equals2(this.overallRoot, t2.overallRoot);
}
private Boolean equals2(IntTreeNode r1, IntTreeNode r2){
    return (r1 == null&&r2 == null)||((r1.data==r2.data)&& equals2(r1.left, r2.left) && equals(r1.right, r2.right));
}

this is what ive come up with but i am not sure if it teststhe same logic as the previouse snippet of code when the program is passed a single null tree will this return false or will it send a null pointer value exception pactice it does not test for this case

Yes, if only one is null it will throw an NPE, so it's not the same.

public boolean equals2(IntTree t2){
    return equals2(this.overallRoot, t2.overallRoot);
}
private Boolean equals2(IntTreeNode r1, IntTreeNode r2){
    return (r1 != null&&r2 == null)||(r1 == null&&r2 != null)||(r1 == null&&r2 == null)||((r1.data==r2.data)&& equals2(r1.left, r2.left) && equals(r1.right, r2.right));
}

how about this

You're changing the base case.
(r1 != null&&r2 == null) will return true if r1 and r2 are not the same.

If resolving the logic is unclear, I would recommend you first write it out as a truth table.

public boolean equals2(IntTree t2){

 }
private Boolean equals2(IntTreeNode r1, IntTreeNode r2){
    return ((r1==null||r2==null)&&(r1 == null&&r2 == null))||((r1.data==r2.data)&& equals2(r1.left, r2.left) && equals(r1.right, r2.right));
 }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.