예제 #1
0
/*!
    compute the huffman code
    given a set of nonzero frequencies this procedure computes
    the code of each symbol. 
    /param freq[] frequency of each symbol (size n)
    /param n: number of symbols
    /param code_len[] code length of each symbol (size n, to be computed) 
    /param code_value[] numerical value of each symbol (size n, to be computed)
*/
void compute_huffman_code(int *freq, int n, int *code_len, int *code_value)
{
  int i, *heap;

  // start by checking that all symbols have freq!=0
  for(i=0;i<n;i++)
    assert(freq[i]>=0);

  if(n>2) {
    heap = (int *) malloc((2*n+1)*sizeof(int));
    if(heap==NULL) _huff_out_of_mem("compute_huffman_code");
    build_heap(heap,freq, n);             // build huffman heap
    build_huffman_tree(heap, n);          // build tree using the huffman heap
    compute_code_len(code_len, heap, n);  // compute code length of each symbol
    compute_code_values(code_value,code_len,n);
    free(heap);     
  }
  else if(n==2) {
    code_len[0]=code_len[1]=1;
    code_value[0]=0; code_value[1]=1;
  }
  else if(n==1)
    code_len[0]=code_value[0]=0;
  else
    _huff_fatal_error("Illegal number of symbols! (compute_huffman_code)");
}
int main (void)
{
    std::vector<HS> symbols;
    HS a, b, c, d, e;
    a.symbol='a';
    a.frequecy = 0.3;           //0.3

    b.symbol='b';
    b.frequecy = 0.1;           //0.4

    c.symbol='c';
    c.frequecy = 0.3;           //0.7

    d.symbol='d';
    d.frequecy = 0.15;          //0.85

    e.symbol='e';
    e.frequecy = 0.15;          //1.0


    symbols.push_back(a);
    symbols.push_back(b);
    symbols.push_back(c);
    symbols.push_back(d);
    symbols.push_back(e);

    auto root = build_huffman_tree(symbols);
    std::unordered_map<char, std::string> encoding;
    std::string current_encoding;
    compute_encoding(root, encoding, current_encoding);
    std::cout << encoding << std::endl;


    return 0;
}
예제 #3
0
파일: main.c 프로젝트: jmp3833/SE350
/*
 * Main driver program.
 * The arguments are ignored.
 */
int main ( int ac, char **av ) {
 
    read_file() ;
    initialize_ordered_list() ;
    build_huffman_tree();
    traverse_tree( ol_remove(), "" ) ;
    print_codes() ;

    exit(0) ;
}
예제 #4
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);
	}
}
예제 #5
0
파일: main.cpp 프로젝트: yuriks/huffman
int main(int argc, char *argv[])
{
#if defined(_WIN32) // && defined(_DEBUG)
	_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif


	if (argc != 4)
	{
		std::cerr << "Invalid number of arguments." << std::endl;
		std::cerr << "Usage: Huffman.exe -c/-u <infile> <outfile>" << std::endl;

		return 1;
	}

	std::ifstream in_file(argv[2], std::ios::in | std::ios::binary);
	if (!in_file)
	{
		std::cerr << "Couldn't open " << argv[2] << std::endl;
		return 2;
	}

	std::ofstream out_file(argv[3], std::ios::out | std::ios::binary | std::ios::trunc);
	if (!out_file)
	{
		std::cerr << "Couldn't open " << argv[3] << std::endl;
		return 3;
	}

	std::string arg(argv[1]);

	if (arg == "-c")
	{
		OFileBitstream stream(out_file);
		std::istreambuf_iterator<char> in_iter(in_file);

		std::cerr << "Construindo arvore..." << std::endl;
		Dictionary<unsigned char>* tree = build_huffman_tree(in_iter, std::istreambuf_iterator<char>());
		std::ifstream::pos_type size = in_file.tellg();
		in_file.seekg(0);
		std::cerr << "Comprimindo arquivo..." << std::endl;
		huffman_compress(tree, stream, in_iter, std::istreambuf_iterator<char>(), size);
		delete tree;
	} 
	else if (arg == "-u")
	{
		in_file.seekg(0, std::ios::end);
		unsigned long long size = in_file.tellg();
		in_file.seekg(0);

		IFileBitstream istream(in_file);
		std::cerr << "Lendo arvore..." << std::endl;
		Dictionary<unsigned char>* tree = readNode(istream);
		size -= in_file.tellg();
		std::cerr << "Descomprimindo arquivo..." << std::endl;
		huffman_uncompress(istream, out_file, tree, size);
		delete tree;
	}
	else if (arg == "--make-tree")
	{
		std::istreambuf_iterator<char> in_iter(in_file);

		Dictionary<unsigned char>* tree = build_huffman_tree(in_iter, std::istreambuf_iterator<char>());
		print_huffman_tree(tree, out_file);
		delete tree;
	}
	else if (arg == "--read-tree")
	{
		IFileBitstream istream(in_file);
		Dictionary<unsigned char>* tree = readNode(istream);
		print_huffman_tree(tree, out_file);
		delete tree;
	}

	out_file.close();
	in_file.close();


	return 0;
}