Ejemplo n.º 1
0
bst_t bst_copy(bst_t bst) {

    bst_t copy = bst_empty();

    switch (bst_type(bst)) {

    case isNull:
        bst_destroy(copy);
        return (NULL);
        break;

    case isEmpty:
        break;

    case isNotEmpty:
        copy->pair = pair_copy(bst->pair);
        copy->izq = bst_copy(bst->izq);
        copy->der = bst_copy(bst->der);
        break;
    }

    assert(bst_is_equal(bst, copy));
    /*POST*/ return (copy);

}
Ejemplo n.º 2
0
bst_t bst_copy(bst_t bst) {
    bst_t result = bst_empty();
    if (bst != NULL) {
        result = bst_add(result, index_copy(pair_fst(bst->pair)),
                         data_copy(pair_snd(bst->pair)));
        result->right = bst_copy(bst->right);
        result->left = bst_copy(bst->left);
    }
    return result;
}
Ejemplo n.º 3
0
dict_t dict_empty(void) {

    dict_t dict = calloc(1, sizeof(struct _dict_t));

    dict->data = bst_empty();
    dict->length = 0;

    /*Postcondition verification*/
    assert(dict != NULL);
    assert(dict_length(dict) == 0);

    return (dict);

}
Ejemplo n.º 4
0
bst_t bst_add(bst_t bst, index_t index, data_t data) {

/*	assert(bst_search(index, bst)==NULL);*/ /*PRE*/
    unsigned int length = bst_length(bst);

    switch (bst_type(bst)) {

    case isNull:

        bst = bst_empty();
        bst->pair = pair_from_index_data(index, data);
        break;

    case isEmpty:

        bst->pair = pair_from_index_data(index, data);
        break;

    case isNotEmpty:

        switch (index_compare(index, bst)){
        
        case EQ:
            break;
            
        case LT:
            bst->izq = bst_add(bst->izq, index, data);
            break;
            
        case GT:
            bst->der = bst_add(bst->der, index, data);
            break;
    
        }
        
        break;
    }

    assert(bst_length(bst) == length + 1);
     /*POST*/ return (bst);

}