Example #1
0
data_t bst_search(bst_t bst, index_t index) {
    data_t data = NULL;
    if (bst != NULL) {
        if (index_is_equal(index, pair_fst(bst->pair))) {
            data = pair_snd(bst->pair);
        } else {
            if (index_is_less_than(index, pair_fst(bst->pair))) {
                data = bst_search(bst->left, index);
            } else {
                data = bst_search(bst->right, index);
            }
        }
    }
    return (data);
}
static unsigned int index_compare(index_t index, bst_t bst) {


    if (index_is_equal(index, pair_fst(bst->pair))) {

        return (EQ);            /*Es igual... */

    } else if (index_is_less_than(index, pair_fst(bst->pair))) {

        return (LT);            /*Es menor... */

    } else {

        return (GT);            /*Es mayor... */

    }

}
Example #3
0
bst_t bst_add(bst_t bst, index_t index, data_t data) {
    unsigned int prev_length = bst_length(bst);
    if (bst != NULL) {
        if (index_is_less_than(index, pair_fst(bst->pair))) {
            bst->left = bst_add(bst->left, index, data);
        } else if (!index_is_equal(index, pair_fst(bst->pair))) {
            bst->right = bst_add(bst->right, index, data);
        }
    } else {
        bst_t add = calloc(1, sizeof(struct _tree_node_t));
        add->pair = pair_from_index_data(index, data);
        add->left = NULL;
        add->right = NULL;
        bst = add;
    }

    assert(prev_length + 1 == bst_length(bst));
    return bst;


}
Example #4
0
bst_t bst_remove(bst_t bst, index_t index) {
    bst_t current;

    if (bst != NULL) {

        if (index_is_less_than(index, pair_fst(bst->pair))) {
            bst->left = bst_remove(bst->left, index);
        } else if (index_is_equal(pair_fst(bst->pair), index) &&
                   bst->left == NULL) {
            current = bst->right;
            bst->pair = pair_destroy(bst->pair);
            free(bst);
            bst = current;
        } else if (index_is_equal(pair_fst(bst->pair), index) &&
                   bst->left != NULL) {
            bst->pair = pair_destroy(bst->pair);
            bst->pair = bst_max(bst->left);
            bst->left = delete_max(bst->left);
        } else {
            bst->right = bst_remove(bst->right, index);
        }
    }
    return (bst);
}