/* heap_extract_root -- retire le sommet du tas et renvoie sa valeur * (en fait un pointeur sur son contenu). * Complexité: O(ln(heap_size(h))) */ void *heap_extract_root(Heap *h) { void *tmp; assert((h != NULL) && (h->tab != NULL)); if (heap_size(h) == 0) return NULL; tmp = h->tab[0]; h->tab[0] = h->tab[h->size-1]; h->size -= 1; heap_shift_down(h, 0); return tmp; }
void *heapsort(Heap *H) { void **copy = H->arr; H->arr = malloc(sizeof(void *) * heap_size(H)); int i = 0; for(; i < heap_size(H); i++) { H->arr[i] = copy[i]; } int len = H->size; while(--H->size > 0) { void *tmp = H->arr[H->size]; H->arr[H->size] = H->arr[0]; H->arr[0] = tmp; heap_shift_down(H, 0); } H->size = len; void **tmp = H->arr; H->arr = copy; return tmp; }
void *heap_pop(Heap *H) { void *elem = H->arr[0]; H->arr[0] = H->arr[--H->size]; heap_shift_down(H, 0); return elem; }
Heap *heap_remove(Heap *H, int i, int free_key) { if(free_key) { free(H->arr[i]); } H->arr[i] = H->arr[--H->size]; return heap_shift_down(H, i); }