Ejemplo n.º 1
0
/******************************************************************************
 * MAIN
 ******************************************************************************/
int main(int argc, char * argv[])
{
    int A[MAX_NUM_NR];
    char * filename_unsorted = get_filename(argc, argv);
    char * filename_sorted = get_sorted_filename(filename_unsorted);

    int n = get_A(A, MAX_NUM_NR, filename_unsorted);

    print_A(A, n, "original:");
    heap_sort(A, n);
    print_A(A, n, "sorted:");

    assert(verify_A(A, n, filename_sorted));

    /* priority queue */
    int heapsize = n;
    build_max_heap(A, heapsize);
    ______________________________(A, n, heapsize, "===============\\nafter build_max_heap\\n===============");

    printf("heap_maximum: %d\n", heap_maximum(A, heapsize));

    printf("heap_extract_max: %d\n", heap_extract_max(A, &heapsize));
    print_A(A, heapsize, "after heap_extract_max");
    ______________________________(A, n, heapsize, "===============\\nafter heap_extract_max\\n===============");

    heap_increase_key(A, heapsize, 3, 88);
    print_A(A, heapsize, "after heap_increase_key");
    ______________________________(A, n, heapsize, "===============\\nafter heap_increase_key\\n===============");

    max_heap_insert(A, n, &heapsize, 97);
    print_A(A, heapsize, "after max_heap_insert");
    ______________________________(A, n, heapsize, "===============\\nafter max_heap_insert\\n===============");

    return 0;
}
Ejemplo n.º 2
0
SequenceT heap_extract_max(HeapT* h) {
    HeapImplT* heap = (HeapImplT*) h;
    assert(heap != NULL && heap->size != 0);

    SequenceT max = heap_maximum(h);
    heap->data[0] = heap->data[heap->size - 1];
    --heap->size;

    if (heap->capacity > INITIAL_CAPACITY &&
        heap->capacity >= (heap->size * GROWTH_FACTOR * GROWTH_FACTOR)
    ) {
        heap->capacity /= GROWTH_FACTOR;
        heap->data = realloc(heap->data, sizeof(SequenceT) * heap->capacity);
    }

    max_heapify(h, 0);
    return max;
}