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:
if (condition)
x = value
else
y = value
and
xyArray[index] = value
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.
I believe that if storing as two fields, you're not going to have just one if
, your code is going to crawl with them. The if
is in all probability more efficient and it is also more efficient memory-wise to have two fields instead of yet another heap-allocated object—a full object at that, with its usual overhead. If your tree is going to be really, really huge (millions of nodes), this is a concern. Also, if each node's payload is tiny compared to the overhead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With