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); }
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; }
list_t list_append(list_t list, index_t index, data_t data) { list_t result=NULL,current=NULL; current = list; unsigned int prev_length = 0; result = calloc(1,sizeof(struct _node_t)); result->elem = pair_from_index_data(index,data); result->next = NULL; if(list == NULL) { list = result; } else { prev_length = list_length(list); while(current->next != NULL) { current = current->next; } current->next = result; } assert(list_length(list) == prev_length +1); return list; }