data_t bst_search(bst_t bst, index_t index) { if (bst_type(bst) == isNotEmpty) { switch (index_compare(index, bst)) { case EQ: return (pair_snd(bst->pair)); break; case LT: return (bst_search(bst->izq, index)); break; case GT: return (bst_search(bst->der, index)); break; } } return (NULL); }
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); }
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); }
static void index_sort(mxml_index_t* ind, /* I - Index to sort */ int left, /* I - Left node in partition */ int right) { /* I - Right node in partition */ mxml_node_t* pivot, /* Pivot node */ *temp; /* Swap node */ int templ, /* Temporary left node */ tempr; /* Temporary right node */ /* * Loop until we have sorted all the way to the right... */ do { /* * Sort the pivot in the current partition... */ pivot = ind->nodes[left]; for (templ = left, tempr = right; templ < tempr;) { /* * Move left while left node <= pivot node... */ while ((templ < right) && index_compare(ind, ind->nodes[templ], pivot) <= 0) { templ ++; } /* * Move right while right node > pivot node... */ while ((tempr > left) && index_compare(ind, ind->nodes[tempr], pivot) > 0) { tempr --; } /* * Swap nodes if needed... */ if (templ < tempr) { temp = ind->nodes[templ]; ind->nodes[templ] = ind->nodes[tempr]; ind->nodes[tempr] = temp; } } /* * When we get here, the right (tempr) node is the new position for the * pivot node... */ if (index_compare(ind, pivot, ind->nodes[tempr]) > 0) { ind->nodes[left] = ind->nodes[tempr]; ind->nodes[tempr] = pivot; } /* * Recursively sort the left partition as needed... */ if (left < (tempr - 1)) { index_sort(ind, left, tempr - 1); } } while (right > (left = tempr + 1)); }