bst_t bst_remove(bst_t bst, index_t index) { if (bst_type(bst) == isNotEmpty) { switch (index_compare(index, bst)) { case EQ: switch (has_child(bst)) { case none: bst = bst_destroy(bst); break; case both: case justRigth: pair_destroy(bst->pair); bst->pair = pair_copy((bst->der)->pair); /*copia el par q le sigue (der) */ bst->der = bst_remove(bst->der, pair_fst((bst->der)->pair)); break; case justLeft: pair_destroy(bst->pair); bst->pair = pair_copy((bst->izq)->pair); /*copia el par q le sigue (der) */ bst->izq = bst_remove(bst->izq, pair_fst((bst->izq)->pair)); break; } break; case LT: bst->izq = bst_remove(bst->izq, index); break; case GT: bst->der = bst_remove(bst->der, index); break; } } return (bst); }
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); }
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; }
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; }
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_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; }
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; }
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; }
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); }
list_t bst_to_list(bst_t bst, list_t list) { if (bst != NULL) { index_t index = index_copy(pair_fst(bst->pair)); data_t data = data_copy(pair_snd(bst->pair)); if (bst->left != NULL) { list = bst_to_list(bst->left, list); } list = list_append(list, index, data); if (bst->right != NULL) { list = bst_to_list(bst->right, list); } index = NULL; data = NULL; } return (list); }
list_t bst_to_list(bst_t bst, list_t list) { index_t indexc; data_t datac; if (bst_type(bst) == isNotEmpty) { indexc = index_copy(pair_fst(bst->pair)); datac = data_copy(pair_snd(bst->pair)); list = list_append(bst_to_list(bst->izq, list), indexc, datac); list = bst_to_list(bst->der, list); } return (list); }
void list_dump(list_t list, FILE * fd) { list_t current = list; char* index; char* data; while(current!=NULL) { index = index_to_string(pair_fst(current->elem)); data = data_to_string(pair_snd(current->elem)); fprintf(fd,"%s: %s",index,data); current = current->next; if(current!=NULL) { fprintf(fd,"\n"); } free(index); free(data); index = NULL; data = NULL; } }
list_t list_copy(list_t list) { list_t result = list_empty(),current=list; index_t index_cop = NULL; data_t data_cop = NULL; while(current!=NULL) { index_cop = index_copy(pair_fst(current->elem)); data_cop = data_copy(pair_snd(current->elem)); result = list_append(result,index_cop,data_cop); current = current->next; } current = NULL; index_cop = NULL; data_cop = NULL; assert(list_is_equal(list,result)); return result; }