So I was implementing my own binary search tree and noticed that an ugly if statement appears all too often the way I’m doing it (which possibly isn’t the best way, but that’s not what we’re discussing), based on whether a child of a node is the left or right child, e.g:
if (leftChild)
parent.setLeft(child.getRight());
else
parent.setRight(child.getRight());
Then I thought of this:
parent.setChild(childIndex, child.getRight());
Where childIndex is a byte that was determined earlier where leftChild would have been determined.
As you can see this much more concise, but to have it this way I would either have to have an if statement in the setChild method or represent the children as an array of length 2. If we pretend here that this BST requires maximized performance/space efficiency, what kind of trade-off would it be to switch the storage of child node references to a 2-element array, rather than a pair of variables (or even just hide the if statement inside the setChild method).
I know in a real-world situation this might not matter that much, but I’m still interested in which would be the best approach.
As I understand it, you’re asking, which of the following two is more efficient:
and
First of all any answer is compiler and/or JVM implementation dependent since the JLS doesn’t mention any execution times for any types of statements.
That being said, I would suspect that the
if-statement would be slightly faster since the JVM doesn’t have to compute any array offset and check array bounds.In any case, this smells like premature optimization to me. Choose approach based on which one is easiest to read and debug.