/****************************************************************************** * 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; }
void main(void) { int i; int arr[] = {4, 1, 3, 2, 16, 9, 10, 14, 8, 7}; int *heap_size = malloc(sizeof(int)); *heap_size = sizeof(arr) / sizeof(int) - 1; HEAP A = {arr, heap_size}; build_max_heap(A); heap_increase_key(A, 8, 15); for (i = 0; i <= *(A.heap_size); i++) printf("%d\t", A.arr[i]); printf("\n%d\n", *(A.heap_size)); heap_extract_max(A); printf("%d\n", *(A.heap_size)); printf("\n"); for (i = 0; i <= *(A.heap_size); i++) printf("%d\t", A.arr[i]); printf("\n"); A = max_heap_insert(A, 16); printf("%d\n", *(A.heap_size)); for (i = 0; i <= *(A.heap_size); i++) printf("%d\t", A.arr[i]); printf("\n"); A = heap_delete(A, 3); for (i = 0; i <= *(A.heap_size); i++) printf("%d\t", A.arr[i]); printf("\n"); }
int main(){ int i, size = 9; int t[] = {63, 73, 25, 19, 44, 2, 59, 38, 10}; printf("Array before creating priority queue:"); for (i = 0; i < size; i++) printf("%d\t",t[i]); build_maxheap(t,size-1); printf("\nNewly built Priority queue : "); for (i = 0; i < size; i++) printf("%d\t",t[i]); i = heap_extract_max(t, size-1); size--; printf("\nExtract maximum:%d\n",i); printf("Priority queue after extract maximum: "); for (i = 0; i < size; i++) printf("%d\t",t[i]); printf("\nEnter a number to be inserted into the priority queue:"); scanf("%d", &i); max_heap_insert(t, i, size-1); size++; printf("Priority queue after insert: "); for (i = 0; i < size; i++) printf("%d\t",t[i]); printf("\n"); return 0; }
int kth_largest(int max_heap[], size_t heap_sz, size_t k, int *res) { if (heap_sz == 0) { return 0; } struct heap_node aux_heap[k]; size_t aux_heap_sz = 0; max_heap_insert(aux_heap, aux_heap_sz, max_heap[0], 0); aux_heap_sz++; size_t i = 1; while (i < k) { if (aux_heap_sz == 0) { return 0; } size_t idx; int val; max_heap_extract(aux_heap, aux_heap_sz, &val, &idx); aux_heap_sz--; size_t left = idx*2+1; size_t right = left+1; if (left < heap_sz) { max_heap_insert(aux_heap, aux_heap_sz, max_heap[left], left); aux_heap_sz++; } if (right < heap_sz) { max_heap_insert(aux_heap, aux_heap_sz, max_heap[right], right); aux_heap_sz++; } i++; } if (aux_heap_sz == 0) { return 0; } else { *res = aux_heap[0].val; return 1; } }
void build_max_heap2(HeapT* h) { assert(h != NULL); HeapImplT* heap = (HeapImplT*) h; size_t size = heap->size, index = 0; heap->size = 0; for (; index < size; ++index) { max_heap_insert(h, heap->data[index]); } }
int main(void) { int i; int a[HEAP_SIZE+10]; a[0]=HEAP_SIZE; srand(time(NULL)); for(i=1;i<=HEAP_SIZE;i++) a[i] = rand()%100; print_heap(a); build_max_heap(a); //heap_increase_key(a,5,211); max_heap_insert(a,211); print_heap(a); //for(i=1;i<=HEAP_SIZE;i++) // printf("the largest number in the excess:%d\n",heap_extract_max(a)); return 0; }