Exemplo 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;
}
Exemplo n.º 2
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");
}
Exemplo n.º 3
0
int max_heap_insert(int *a, int key)
{
	a[0]++;
	a[a[0]] = INT_MIN;
	heap_increase_key(a,a[0],key);
	
}
Exemplo n.º 4
0
HEAP
max_heap_insert(HEAP A, int key)
{
    int i;
    *(A.heap_size) = *(A.heap_size) + 1;
    A.arr[*(A.heap_size)] = -1111111;
    heap_increase_key(A, *(A.heap_size), key);
    return A;
}
Exemplo n.º 5
0
/**
 * @brief insert an element in a max heap
 *
 * This routine inserts an element in the heap. The element is a {key,data}
 * pair.
 *
 * @param[in] h The heap to operate on
 * @param[in] key The element key component
 * @param[in] data The element data component
 * @param[out] i The updated heap index for the {key, data} will be stored here.
 * @return HEAP_ERR_E
 */
HEAP_ERR_E heap_max_insert (HEAP_T* h, unsigned long key, void* data, unsigned long* i)
{
   if (h->type != DS_HEAP_MAX)
      return HEAP_ERR_WRONG_TYPE;

   heap_add (h, HEAP_NIL_KEY, data, i);
   heap_increase_key (h, h->heap_size-1, key);
   return HEAP_ERR_OK;
}
Exemplo n.º 6
0
void heap_insert(heap_t heap, int key)
{
    heap.heap_size += 1;
    if (heap.heap_size >= heap.size) {
	printf("Over flow\n");
	return;
    }
    heap.arr[heap_size] = -1;
    heap_increase_key(heap, heap_size, key);
}
Exemplo n.º 7
0
void
max_heap_insert(int *A, int key)
{
    /*
     * 把元素x插入集合A中
     */
    heapsize++;
    A[heapsize] = MIN;
    heap_increase_key(A, heapsize, key);
}
Exemplo n.º 8
0
void max_heap_insert(HeapT* h, SequenceT key) {
    assert(h != NULL);

    HeapImplT* heap = (HeapImplT*) h;

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

    ++heap->size;
    heap->data[heap->size - 1] = INVALID_NODE;
    heap_increase_key(h, heap->size - 1, key);
}
Exemplo n.º 9
0
void max_heap_insert (heap h, val_t v) {
	// allocate new heap space if needed to insert a new key
	if (h->size == h->data_length) {
		int extra = h->size/2;
		if ((h->data = (val_array) realloc (h->data, (h->size+extra)*sizeof(val_t))) == NULL) {
			printf ("could realloc heap memory\n");
			exit(1);
		}
		h->data_length += extra;
	}

	++h->size;
	set(h,h->size-1,-65562);
	heap_increase_key(h,h->size-1,v);
	return;
}
Exemplo n.º 10
0
void heap_insert(heap_t* heap, int key)
{
	heap->heap_size ++;
	if(heap->heap_size > heap->length)
	{
		int* items = malloc(sizeof(int) * heap->heap_size); // 内存泄露,由于items不是全局的,不好释放
		for(int i = 0; i < heap->length; i++)
		{
			items[i] = heap->elements[i];
		}
		items[heap->heap_size - 1] = key - 1;
		heap->elements = items;
		heap->length ++;
	}
	heap_increase_key(heap, heap->heap_size - 1, key);
}
Exemplo n.º 11
0
void max_heap_insert(int * A, int Asize, int * heapsize, int key)
{
    assert(*heapsize < Asize);
    A[(*heapsize)++] = INT_MIN;
    heap_increase_key(A, *heapsize, *heapsize - 1, key);
}
Exemplo n.º 12
0
Arquivo: heap.c Projeto: ai-ku/dist
void max_heap_insert(Heap h, Hnode value){
     h[0].val += 1;
     h[heap_size(h)].val = INT_MIN;    
     heap_increase_key(h, heap_size(h), value);
}
Exemplo n.º 13
0
void max_heap_insert (int A[], int key, int size){
  size++;
  A[size] = -99999;
  heap_increase_key(A, size, key);
  return;
}
Exemplo n.º 14
0
int main()
{
	printf("----------------------Heap---------------------\n");
	printf("\t0. max_heapify(heap_t*, int)\n");
	printf("\t1. build_max_heap(heap_t*)\n");
	printf("\t2. heap_extract_max(heap_t*)\n");
	printf("\t3. heap_sort(heap_t*)\n");
	printf("\t4. heap_increase_key(heap_t*, int, int)\n");
	printf("\t5. heap_insert(heap_t*, int)\n");
	printf("Please select (-1 to quit): ");
	int select;
	scanf("%d", &select);
	if(select == -1)
	{
		exit(0);
	}
	while(select != -1)
	{
		heap_t heap;
		int length;
		int heap_size;
		printf("Now create a heap\n");
		printf("Enter heap length: ");
		scanf("%d",&length);
		printf("Enter heap size: ");
		scanf("%d",&heap_size);
		printf("Enter elements: \n");
		int items[length];
		for(int i = 0; i < heap_size; i++)
		{
			scanf("%d",&items[i]); 
		}
		heap.length = length;
		heap.heap_size = heap_size;
		heap.elements = items;
		
		switch(select)
		{
			case 0:
				printf("Please enter index you want to heapify: ");
				int index;
				scanf("%d",&index);
				max_heapify(&heap, index);
				break;
			case 1:
				build_max_heap(&heap);
				break;
			case 2:
				build_max_heap(&heap);
				heap_extract_max(&heap);
				break;
			case 3:
				build_max_heap(&heap);
				heap_sort(&heap);
				break;
			case 4:
				build_max_heap(&heap);
				printf("Please enter index you want to increase: ");
				int i = getchar();
				printf("Please enter key: ");
				int key = getchar();
				heap_increase_key(&heap, i, key);
				break;
			case 5:
				build_max_heap(&heap);
				printf("Please enter elements you want to insert: ");
				int element = getchar();
				heap_insert(&heap,element);
				break;
			default:
				printf("Num error.");
		}
		for(int i = 0; i < heap.length; i++)
		{
			printf("%d ", heap.elements[i]);
		}

		printf("\n\n");
		printf("\t0. max_heapify(heap_t*, int)\n");
		printf("\t1. build_max_heap(heap_t*)\n");
		printf("\t2. heap_extract_max(heap_t*)\n");
		printf("\t3. heap_sort(heap_t*)\n");
		printf("\t4. heap_increase_key(heap_t*, int, int)\n");
		printf("\t5. heap_insert(heap_t*, int)\n");
		printf("Please select (-1 to quit): ");
		scanf("%d",&select);
	}
	printf("----------------------End---------------------\n");

	return 0;
}
void max_heap_insert(double* srcdata, int* len, double dValue){
	*len = *len + 1;
	srcdata[*len-1] = -1;
	heap_increase_key(srcdata,*len,*len,dValue);
}
Exemplo n.º 16
0
void heappush( Vector* V, cmp compare, void* item ) {
    push( V, item );
    heap_increase_key( V, compare, vector_size( V ) - 1, item );
}