int main(int argc, char ** argv) { unsigned char c; FILE * file; int freqs[CHARACTER_SET_SIZE] = {}; if (argc != 2) { fprintf(stderr, "Usage: huffman <filename>\n"); exit(1); // exit with error code } file = fopen(argv[1], "r"); assert(file != NULL); c = fgetc(file); while (!feof(file)) { freqs[c]++; c = fgetc(file); } fclose(file); huffman_tree * tree = huffman_tree_new(CHARACTER_SET_SIZE); int i; for (i = 0; i < CHARACTER_SET_SIZE; i++) { if (freqs[i] == 0) { freqs[i] = 1; } symbol * char_symbol = symbol_new_char(freqs[i], i); huffman_tree_insert(tree, i, char_symbol); } huffman_tree_merge(tree); huffman_tree_walk(tree); huffman_tree_free(tree); return 0; }
static void initialise_rbt(GtHuffman *huffman, const void *distr, GtDistrFunc distrfunc) { GtHuffmanTree *huffptr, GT_UNUSED *huffptr2; unsigned long i; bool nodecreated = false; huffman->numofsymbols = 0; huffman->rbt_root = gt_rbtree_new(huffman_tree_cmp, huffman_tree_delete, NULL); gt_assert(huffman->rbt_root); for (i = 0; i < huffman->totalnumofsymbols; i++) { if (distrfunc(distr, i) > 0) { huffptr = huffman_tree_new(i, distrfunc(distr, i)); huffptr2 = (GtHuffmanTree*)gt_rbtree_search(huffman->rbt_root, huffptr, &nodecreated); gt_assert(nodecreated && huffptr2); huffman->numofsymbols++; } } }
static int make_huffman_tree(GtHuffman *huffman) { GtHuffmanTree *n1 = NULL, *n2 = NULL, GT_UNUSED *n3 = NULL, *newnode = NULL; unsigned long i, symbol; unsigned long long freq; int GT_UNUSED deleted; bool nodecreated = false; if (huffman->numofsymbols == 0) huffman->roothuffmantree = NULL; else if (huffman->numofsymbols == 1) { huffman->roothuffmantree = gt_rbtree_root_key(huffman->rbt_root); huffman->roothuffmantree->code.code = 0; huffman->roothuffmantree->code.numofbits = 1; } else { for (i = 0; i < huffman->numofsymbols - 1; i++) { n1 = gt_rbtree_minimum_key(huffman->rbt_root); n1 = huffman_tree_ref(n1); deleted = gt_rbtree_erase(huffman->rbt_root, n1); gt_assert(deleted == 0); n2 = gt_rbtree_minimum_key(huffman->rbt_root); n2 = huffman_tree_ref(n2); deleted = gt_rbtree_erase(huffman->rbt_root, n2); gt_assert(deleted == 0); symbol = n1->symbol.symbol < n2->symbol.symbol ? n2->symbol.symbol : n1->symbol.symbol; freq = n1->symbol.freq + n2->symbol.freq; newnode = huffman_tree_new(symbol, freq); if (n1->symbol.freq < n2->symbol.freq) { newnode->leftchild = n2; newnode->rightchild = n1; } else { newnode->leftchild = n1; newnode->rightchild = n2; } gt_assert(huffman->rbt_root); n3 = gt_rbtree_search(huffman->rbt_root, newnode, &nodecreated); gt_assert(nodecreated && n3); } huffman->roothuffmantree = (GtHuffmanTree*)newnode; huffman->roothuffmantree->code.code = 0; huffman->roothuffmantree->code.numofbits = 0; } return 0; }
void huffman_tree_init(void) { huffman_tree *t0 = huffman_tree_new('H', NULL, NULL); huffman_tree *t100 = huffman_tree_new('G', NULL, NULL); huffman_tree *t10100 = huffman_tree_new('C', NULL, NULL); huffman_tree *t101010 = huffman_tree_new('A', NULL, NULL); huffman_tree *t1010110 = huffman_tree_new('<', NULL, NULL); huffman_tree *t10101110 = huffman_tree_new('9', NULL, NULL); huffman_tree *t10101111 = huffman_tree_new(':', NULL, NULL); huffman_tree *t1010111 = huffman_tree_new('\0', t10101110, t10101111); huffman_tree *t101011 = huffman_tree_new('\0', t1010110, t1010111); huffman_tree *t10101 = huffman_tree_new('\0', t101010, t101011); huffman_tree *t1010 = huffman_tree_new('\0', t10100, t10101); huffman_tree *t10110 = huffman_tree_new('B', NULL, NULL); huffman_tree *t1011100 = huffman_tree_new('>', NULL, NULL); huffman_tree *t1011101000 = huffman_tree_new('.', NULL, NULL); huffman_tree *t101110100100 = huffman_tree_new(')', NULL, NULL); huffman_tree *t101110100101 = huffman_tree_new('I', NULL, NULL); huffman_tree *t10111010010 = huffman_tree_new('\0', t101110100100, t101110100101); huffman_tree *t10111010011 = huffman_tree_new('+', NULL, NULL); huffman_tree *t1011101001 = huffman_tree_new('\0', t10111010010, t10111010011); huffman_tree *t101110100 = huffman_tree_new('\0', t1011101000, t1011101001); huffman_tree *t101110101 = huffman_tree_new('6', NULL, NULL); huffman_tree *t10111010 = huffman_tree_new('\0', t101110100, t101110101); huffman_tree *t10111011 = huffman_tree_new(';', NULL, NULL); huffman_tree *t1011101 = huffman_tree_new('\0', t10111010, t10111011); huffman_tree *t101110 = huffman_tree_new('\0', t1011100, t1011101); huffman_tree *t101111 = huffman_tree_new('#', NULL, NULL); huffman_tree *t10111 = huffman_tree_new('\0', t101110, t101111); huffman_tree *t1011 = huffman_tree_new('\0', t10110, t10111); huffman_tree *t101 = huffman_tree_new('\0', t1010, t1011); huffman_tree *t10 = huffman_tree_new('\0', t100, t101); huffman_tree *t110 = huffman_tree_new('F', NULL, NULL); huffman_tree *t1110 = huffman_tree_new('E', NULL, NULL); huffman_tree *t11110 = huffman_tree_new('D', NULL, NULL); huffman_tree *t1111100000 = huffman_tree_new('2', NULL, NULL); huffman_tree *t11111000010 = huffman_tree_new(',', NULL, NULL); huffman_tree *t11111000011 = huffman_tree_new('/', NULL, NULL); huffman_tree *t1111100001 = huffman_tree_new('\0', t11111000010, t11111000011); huffman_tree *t111110000 = huffman_tree_new('\0', t1111100000, t1111100001); huffman_tree *t111110001 = huffman_tree_new('8', NULL, NULL); huffman_tree *t11111000 = huffman_tree_new('\0', t111110000, t111110001); huffman_tree *t111110010 = huffman_tree_new('5', NULL, NULL); huffman_tree *t111110011 = huffman_tree_new('7', NULL, NULL); huffman_tree *t11111001 = huffman_tree_new('\0', t111110010, t111110011); huffman_tree *t1111100 = huffman_tree_new('\0', t11111000, t11111001); huffman_tree *t1111101 = huffman_tree_new('?', NULL, NULL); huffman_tree *t111110 = huffman_tree_new('\0', t1111100, t1111101); huffman_tree *t11111100 = huffman_tree_new('=', NULL, NULL); huffman_tree *t1111110100 = huffman_tree_new('3', NULL, NULL); huffman_tree *t111111010100 = huffman_tree_new('(', NULL, NULL); huffman_tree *t111111010101 = huffman_tree_new('*', NULL, NULL); huffman_tree *t11111101010 = huffman_tree_new('\0', t111111010100, t111111010101); huffman_tree *t11111101011000 = huffman_tree_new('%', NULL, NULL); huffman_tree *t11111101011001 = huffman_tree_new('&', NULL, NULL); huffman_tree *t1111110101100 = huffman_tree_new('\0', t11111101011000, t11111101011001); huffman_tree *t1111110101101 = huffman_tree_new('\'', NULL, NULL); huffman_tree *t111111010110 = huffman_tree_new('\0', t1111110101100, t1111110101101); huffman_tree *t111111010111 = huffman_tree_new('-', NULL, NULL); huffman_tree *t11111101011 = huffman_tree_new('\0', t111111010110, t111111010111); huffman_tree *t1111110101 = huffman_tree_new('\0', t11111101010, t11111101011); huffman_tree *t111111010 = huffman_tree_new('\0', t1111110100, t1111110101); huffman_tree *t1111110110 = huffman_tree_new('4', NULL, NULL); huffman_tree *t11111101110 = huffman_tree_new('0', NULL, NULL); huffman_tree *t11111101111 = huffman_tree_new('1', NULL, NULL); huffman_tree *t1111110111 = huffman_tree_new('\0', t11111101110, t11111101111); huffman_tree *t111111011 = huffman_tree_new('\0', t1111110110, t1111110111); huffman_tree *t11111101 = huffman_tree_new('\0', t111111010, t111111011); huffman_tree *t1111110 = huffman_tree_new('\0', t11111100, t11111101); huffman_tree *t1111111 = huffman_tree_new('@', NULL, NULL); huffman_tree *t111111 = huffman_tree_new('\0', t1111110, t1111111); huffman_tree *t11111 = huffman_tree_new('\0', t111110, t111111); huffman_tree *t1111 = huffman_tree_new('\0', t11110, t11111); huffman_tree *t111 = huffman_tree_new('\0', t1110, t1111); huffman_tree *t11 = huffman_tree_new('\0', t110, t111); huffman_tree *t1 = huffman_tree_new('\0', t10, t11); TREE = huffman_tree_new('\0', t0, t1); }