jeudi 12 juillet 2018

Need a fancy if-statement to cut out code-doubles

So I'm working on implementing a BST and I would like to cut down on a double:

int result = x.compareTo(e.element);
    if (result > 0) {
        e.right = new BinaryNode<E>(x);
    } else {
        e.left = new BinaryNode<E>(x);
    }

I would like to use the ?-operand here, but am not sure how. My plan was to do something like this, but only the reference to the new node, n, is changed:

BinaryNode n = (result > 0) ? e.right : e.left;
n = new BinaryNode<E>(x);

I would love for it to work like this:

(result > 0) ? e.right : e.left = new BinaryNode<E>(x);

Anyone got any ideas? Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire