list_t list_remove(list_t list, index_t index) { list_t current = list,last=NULL; while(current != NULL && !index_is_equal(pair_fst(current->elem),index)) { last = current; current = current->next; } if(current!=NULL) { if(last==NULL) { list = current->next; } else { last->next = current->next; current->next = NULL; } current->elem = pair_destroy(current->elem); free(current); current = NULL; last = NULL; } return list; }
data_t list_search(list_t list, index_t index) { list_t current = list; while(current != NULL && !index_is_equal(pair_fst(current->elem),index)) { current = current->next; } data_t result = NULL; if(current!=NULL) { result = pair_snd(current->elem); } current = NULL; return result; }
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); }
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); }
bool list_is_equal(list_t list, list_t other) { bool result = true; while(result && list!=NULL && other!=NULL) { if(!index_is_equal(pair_fst(list->elem),pair_fst(other->elem))) { result = false; } if(!data_is_equal(pair_snd(list->elem),pair_snd(other->elem))) { result = false; } list = list->next; other = other->next; } return result && list == NULL && other == NULL; }
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... */ } }
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; }