Beispiel #1
0
void
find_char_encoding(struct Node *root, int depth, char *ret)
{
	if(root->left==NULL && root->right==NULL) {
		printf("%c: %s\n", root->c, ret);
		return;
	}
	ret[depth] = '0';
	find_char_encoding(root->left, depth+1, ret);
	ret[depth] = '\0';
	ret[depth] = '1';
	find_char_encoding(root->right, depth+1, ret);
	ret[depth] = '\0';
}
Beispiel #2
0
int
main(int argc, char **argv)
{
	char buf[MAX_INPUT_LEN], enc[LEN*2];
	while(scanf("%s", buf) != EOF) {
		memset(enc, 0, sizeof(enc));
		init(buf, strlen(buf));
		struct Node *root = build_huffman_tree();
		find_char_encoding(root, 0, enc);
		free_huffman_tree(root);
	}
}
Beispiel #3
0
/* Table must be freed by function calling get_huffman_table.
 */
static char *tree2table(tree t) {
  char temp[257] = {'\0'};
  int i, n, c;
  char *table;
  if ((table = (char *)calloc(33153, sizeof(char))) == NULL) {
    fprintf(stderr, "tree2table: calloc error\n");
    exit(-1);
  }
  i = n = 0;
  for (c = 0; c < 256; c++) {
    find_char_encoding(t, temp, &i, c);
    strncpy(&table[n], temp, i);
    n = n + i;
  }
  table[n] = '\0';
  return table;
}