Quote:
I know the correct answer but cannot analyse my code.
That's a pity, since you wrote it and thus are the best person to fix it as you understand what it is doing!
Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.
So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.
And the process of "finding an error in code" is called
debugging and it's something that every developer has to do, pretty much all the time: it takes longer to ensure code it right and working than it does to write it!
So you need to learn how to do it, and me fixing your code won't do that - you need to do it for yourself in order to develop the skill.
Start with three things: the input (
root
in this case), the expected answer (you know this, you said so), and the actual answer (run your code and find out).
Check the input: is it exactly what you expected it to be? Use the debugger (a quick google for the name of your IDE and "debugger" will get you instructions on how to use it) and actually look at the tree structure your method has been passed: write it down on paper to make sure it really is what you think it should be. If it isn't, then there is no point in checking the method itself as the result won't be what you want.
When you have valid input, look at the expected value and the actual. How are they different? is it "off by one"? An order of magnitude out? Zero? Think about the tree and consider what about it might have generated that result - for example, is it counting all nodes instead of just the left ones?
Working out possible routes to that erroneous result can give you a lot of clues about what is happening in your code that shouldn't, or not ha-ppenign that should.
We you've done that continue to use the debugger to single step though your code while it is running and work out what you expect to happen for each line before you execute it. Did it happen as you wanted? If so, keep going. If not, why not?
For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input Expected output Actual output
1 2 1
2 4 4
3 6 9
4 8 16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
private int Double(int value)
{
return value * value;
}
And a quick bit of debugging will show you exactly what is wrong!
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!