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); }
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; }
/** * 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; }