libc/src/search/tsearch.c
static posix_tnode* search(const void* key, posix_tnode** root,
        int (*compare)(const void*, const void*)) {
    posix_tnode* node = *root;
    if (!node) {
        node = malloc(sizeof(posix_tnode));
        if (!node) return NULL;
        node->key = key;
        node->left = NULL;
        node->right = NULL;
        node->color = RED;

        *root = node;
        return node;
    }

    int cmp = compare(key, node->key);
    posix_tnode* result;

    if (cmp < 0) {
        result = search(key, &node->left, compare);
    } else if (cmp > 0) {
        result = search(key, &node->right, compare);
    } else {
        return node;
    }
    if (!result) return NULL;

    // After a new node has been inserted into the tree we might no longer have
    // a left-leaning red-black tree. We are fixing this on the way back up.
    *root = __tdelete_ensureInvariants(node);
    return result;
}
/*
Our binary search tree is implemented as a left-leaning red-black tree [1].
In between operations that change the tree, the following invariants hold:
1. A red node has only black children.
2. Any path from the root to any leaf crosses the same number of baclk nodes.
3. A black node can have at most one red child and this child (if it exists) is
   always the left one.

During operations that change the tree these invariants may temporarily be
violated but these violations will always be fixed before returning.

[1] Robert Sedgewick, Left-leaning Red-Black Trees (2008)
    https://www.cs.princeton.edu/~rs/talks/LLRB/LLRB.pdf
*/
Our binary search tree is implemented as a left-leaning red-black tree [1].
In between operations that change the tree, the following invariants hold:
1. A red node has only black children.
2. Any path from the root to any leaf crosses the same number of baclk nodes.
3. A black node can have at most one red child and this child (if it exists) is
always the left one.

During operations that change the tree these invariants may temporarily be
violated but these violations will always be fixed before returning.


Our binary search tree is implemented as a left - leaning red - black tree [ 1 ] . In between operations that change the tree the following invariants hold : 1 . A red node has only black children . 2 . Any path from the root to any leaf crosses the same number of baclk nodes . 3 . A black node can have at most one red child and this child ( if it exists ) is always the left one . During operations that change the tree these invariants may temporarily be violated but these violations will always be fixed before returning .
static posix_tnode* search(const void* key, posix_tnode** root,
        int (*compare)(const void*, const void*)) {
    posix_tnode* node = *root;
    if (!node) {
        node = malloc(sizeof(posix_tnode));
        if (!node) return NULL;
        node->key = key;
        node->left = NULL;
        node->right = NULL;
        node->color = RED;
        *root = node;
        return node;
    }
    int cmp = compare(key, node->key);
    posix_tnode* result;
    if (cmp < 0) {
        result = search(key, &node->left, compare);
    } else if (cmp > 0) {
        result = search(key, &node->right, compare);
    } else {
        return node;
    }
    if (!result) return NULL;
    *root = __tdelete_ensureInvariants(node);
    return result;
}
static posix_tnode * search ( const void * key , posix_tnode * * root , int ( * compare ) ( const void * , const void * ) ) { posix_tnode * node = * root ; if ( ! node ) { node = malloc ( sizeof ( posix_tnode ) ) ; if ( ! node ) return NULL ; node -> key = key ; node -> left = NULL ; node -> right = NULL ; node -> color = RED ; * root = node ; return node ; } int cmp = compare ( key , node -> key ) ; posix_tnode * result ; if ( cmp < 0 ) { result = search ( key , & node -> left , compare ) ; } else if ( cmp > 0 ) { result = search ( key , & node -> right , compare ) ; } else { return node ; } if ( ! result ) return NULL ; * root = __tdelete_ensureInvariants ( node ) ; return result ; }
Our binary search tree is implemented as a left-leaning red-black tree [1].
Our binary search tree is implemented as a left - leaning red - black tree [ 1 ] .
// After a new node has been inserted into the tree we might no longer have // a left-leaning red-black tree. We are fixing this on the way back up.