Inserting into a binary search tree in C I am in the process of refactoring this simple binary search tree code responsible for adding new values:

void add_internal(Node *root, int value) {
    // Decide whether to insert left or right
    if (root-&gt;value &gt;= value) {
        // Decide whether to insert here or down the subtree
        if (root-&gt;left) {
            add_internal(root-&gt;left, value);
        } else {
            root-&gt;left = create_node(value);
        }
    } else {
        // Decide whether to insert here or down the subtree
        if (root-&gt;right) {
            add_internal(root-&gt;right, value);
        } else {
            root-&gt;right = create_node(value);
        }
    }
}


to this:

Node **relevant_node = (root-&gt;value &gt;= value) ? &amp;root-&gt;left : &amp;root-&gt;right;

if (*relevant_node) {
    add_internal(*relevant_node, value);
} else {
    *relevant_node = create_node(value);
}


Both solutions work. My refactored one tho is pretty much the result of trial and error, will it be as robust as the first solution?
There is a third option:

Node *add_internal(Node *root, int value) {
    if(root == NULL) {
        return create_node(value);
    }

    // Decide whether to insert left or right
    if (root-&gt;value &gt;= value) {
        root-&gt;left = add_internal(root-&gt;left, value);
    } else {
        root-&gt;right = add_internal(root-&gt;right, value);
    }
    return root;
}


Basically moves the null test into the recursive call.

However this version requires it to be called as root = add_internal(root, value);. But it has the advantage that it can deal with a null root in a transparent way.