コード例 #1
0
void test_min_heap(void)
{
	BinaryHeap *heap;
	int *val;
	int i;

	heap = binary_heap_new(BINARY_HEAP_TYPE_MIN, int_compare);

	/* Push a load of values onto the heap */

	for (i=0; i<NUM_TEST_VALUES; ++i) {
		test_array[i] = i;
		assert(binary_heap_insert(heap, &test_array[i]) != 0);
	}

	/* Pop values off the heap and check they are in order */

	i = -1;
	while (binary_heap_num_entries(heap) > 0) {
		val = (int *) binary_heap_pop(heap);

		assert(*val == i + 1);
		i = *val;
	}

	/* Test popping from an empty heap */

	assert(binary_heap_num_entries(heap) == 0);
	assert(binary_heap_pop(heap) == BINARY_HEAP_NULL);

	binary_heap_free(heap);
}
コード例 #2
0
void test_max_heap(void)
{
	BinaryHeap *heap;
	int *val;
	int i;

	heap = binary_heap_new(BINARY_HEAP_TYPE_MAX, int_compare);

	/* Push a load of values onto the heap */

	for (i=0; i<NUM_TEST_VALUES; ++i) {
		test_array[i] = i;
		assert(binary_heap_insert(heap, &test_array[i]) != 0);
	}

	/* Pop values off the heap and check they are in order */

	i = NUM_TEST_VALUES;
	while (binary_heap_num_entries(heap) > 0) {
		val = (int *) binary_heap_pop(heap);

		assert(*val == i - 1);
		i = *val;
	}

	binary_heap_free(heap);
}
コード例 #3
0
void test_out_of_memory(void)
{
	BinaryHeap *heap;
	int *value;
	int values[] = { 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
	int i;

	/* Allocate a heap and fill to the default limit */

	heap = binary_heap_new(BINARY_HEAP_TYPE_MIN, int_compare);

	alloc_test_set_limit(0);

	for (i=0; i<16; ++i) {
		assert(binary_heap_insert(heap, &values[i]) != 0);
	}

	assert(binary_heap_num_entries(heap) == 16);

	/* Check that we cannot add new values */

	for (i=0; i<16; ++i) {
		assert(binary_heap_insert(heap, &values[i]) == 0);
		assert(binary_heap_num_entries(heap) == 16);
	}

	/* Check that we can read the values back out again and they
	 * are in the right order. */

	for (i=0; i<16; ++i) {
		value = binary_heap_pop(heap);
		assert(*value == i);
	}

	assert(binary_heap_num_entries(heap) == 0);

	binary_heap_free(heap);
}
コード例 #4
0
ファイル: heap_sort.c プロジェクト: pavolzetor/adt
void heap_sort(int *numbers, const int count, const compare_cb cmp) {
    struct BinaryHeap *heap = binary_heap_create(cmp);

    for (int i = 0; i < count; i++) {
        binary_heap_insert(heap, numbers[i]);
    }

    for (int i = 0; i < count; i++) {
        numbers[i] = binary_heap_root(heap);
        binary_heap_delete(heap);
    }

    binary_heap_destroy(heap);
}
コード例 #5
0
void test_binary_heap_insert(void)
{
	BinaryHeap *heap;
	int i;

	heap = binary_heap_new(BINARY_HEAP_TYPE_MIN, int_compare);

	for (i=0; i<NUM_TEST_VALUES; ++i) {
		test_array[i] = i;
		assert(binary_heap_insert(heap, &test_array[i]) != 0);
	}

	assert(binary_heap_num_entries(heap) == NUM_TEST_VALUES);

	binary_heap_free(heap);
}
コード例 #6
0
int main (int argc, char *argv[])
{
	int i;
	struct binary_heap *binary_heap;
	binary_heap = binary_heap_create(0, compare, NULL);
	if (binary_heap == NULL) {
		return -1;
	}
	for (i = 1; i < argc; i++) {
		binary_heap_insert(binary_heap, argv[i]);
	}
	printf("%s ", (char *) binary_heap_pop(binary_heap));
	printf("%s ", (char *) binary_heap_pop(binary_heap));
	printf("%s ", (char *) binary_heap_pop(binary_heap));
	binary_heap_destroy(binary_heap);
	return 0;
}
コード例 #7
0
/**
 * This function creates a Huffman tree using a heap. For each byte that has a frequency
 * greater than 0, a node is created that stores the byte and its frequency and gets
 * inserted to a heap. To construct the tree, two nodes with the highest priority
 * are extracted from the heap and a new node is created with the extracted nodes as
 * its left and right children. This is repeated until only one node is left in the heap 
 * which is the root of the Huffman tree.
 */
int huffman_tree_create(huffman_node** root, unsigned int frequencies[256]) {
    
    int retval = HUFFMAN_TREE_EMPTY;
    binary_heap* heap = NULL;
    int heap_creation_status = binary_heap_create(&heap, compare_huffman_nodes, huffman_node_destroy);
    if(heap_creation_status == BINARYHEAP_ALLOC_ERROR) {
        return HUFFMAN_ALLOC_ERROR;
    }
    
    int node_count = 0;
    for(unsigned int i = 0; i < 256; i++) {
        huffman_node* node;
        
        if(frequencies[i] != 0) {
            retval = huffman_node_create(&node);
            if(retval == HUFFMAN_ALLOC_ERROR) {
                binary_heap_destroy(&heap);
                return HUFFMAN_ALLOC_ERROR;
            }
            
            node->which_char = (unsigned char) i;
            node->frequency = frequencies[i];
            
            retval = binary_heap_insert(heap, node);
            if(retval == BINARYHEAP_ALLOC_ERROR) {
                huffman_node_destroy(node);
                binary_heap_destroy(&heap);
                return HUFFMAN_ALLOC_ERROR;
            }
            
            node_count++;
        }
    }
    
    for(int i = 0; i < node_count - 1; i++) {
        huffman_node* new_node = NULL;
        retval = huffman_node_create(&new_node);
       
        if(retval == BINARYHEAP_ALLOC_ERROR) {
            binary_heap_destroy(&heap);
            return HUFFMAN_ALLOC_ERROR;
        }
        
        void* data;
        huffman_node* left;
        huffman_node* right;
        
        binary_heap_extract(heap, &data);
        left = (huffman_node*) data;
        new_node->left = left;
        
        binary_heap_extract(heap, &data);
        right = (huffman_node*) data;
        new_node->right = right;

        left->parent = right->parent = new_node;
        
        new_node->frequency = left->frequency + right->frequency;
        new_node->is_leaf = 0;
        
        if(binary_heap_insert(heap, new_node) == BINARYHEAP_ALLOC_ERROR) {
            //this will destroy the right and left subtrees too
            huffman_node_destroy(new_node);

            binary_heap_destroy(&heap);
            return HUFFMAN_ALLOC_ERROR;
        }
    }
    
    void* root_huffman_node = NULL;

    int extraction_status = binary_heap_extract(heap, &root_huffman_node);
    if(extraction_status != BINARYHEAP_EMPTY) {
        retval = HUFFMAN_SUCCESS;
    }

    binary_heap_destroy(&heap);
    
    (*root) = (huffman_node*) root_huffman_node;
    return retval;
}