当前位置: 首页>>代码示例>>C++>>正文


C++ HuffmanTree类代码示例

本文整理汇总了C++中HuffmanTree的典型用法代码示例。如果您正苦于以下问题:C++ HuffmanTree类的具体用法?C++ HuffmanTree怎么用?C++ HuffmanTree使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了HuffmanTree类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: HuffmanTree

HuffmanTree*
HuffmanEncoder::huffmanTreeFromFrequencyMap(uint64_t *FreqMap)
{
    priority_queue<HuffmanTree*,vector<HuffmanTree*>,HuffmanTreeCompare> forest{};

    for (int i = 0; i < 256; i++) {
        if (FreqMap[i] == 0) {
            continue;
        }
        HuffmanTree *tree = new HuffmanTree((char)i, FreqMap[i]);
        if (tree->GetWeight() == 0) {
            printf("Weight is 0!!!********************************************************8\n");
        }
        forest.push(tree);
    }

    printf("%ld trees in forest", forest.size());

    while (forest.size() > 1) {
        HuffmanTree *smallest = forest.top();
        forest.pop();
        HuffmanTree *secondSmallest = forest.top();
        forest.pop();
        HuffmanTree *tree = new HuffmanTree(smallest, secondSmallest);
        forest.push(tree);
    }

    return forest.top();
}
开发者ID:raemarks,项目名称:ParallelHuffmanEncoding,代码行数:29,代码来源:HuffmanEncoder.cpp

示例2: main

int main(int argc, char* argv[]) {
    using namespace ptlmuh006;

    if(argc != 4){
        std::cout << "Incorrect usage." << std::endl;
        std::cout << "Please use huffencode as follows:" << std::endl;
        std::cout << "huffencode <option> <inputFilename> <outputFilename>" << std::endl;
        std::cout << "\t___options:___" << std::endl;
        std::cout << "\t -c compress data from inputFilename to outputFilename" << std::endl;
        std::cout << "\t -x extract compressed data from inputFilename to outputFilename" << std::endl;

    }else{
        HuffmanTree* huff = new HuffmanTree;

        std::string option = argv[1];
        if(option == "-c"){
            huff->compress(argv[2], argv[3]);
            std::cout << "Compressed " << argv[2] << " into " << argv[3] << "." << std::endl;

        }else if(option == "-x"){
            huff->extract(argv[2], argv[3]);
            std::cout << "Extracted from " << argv[2] << " into " << argv[3] << "." << std::endl;
        }

        delete huff;
    }
    return 0;
}
开发者ID:muhummadPatel,项目名称:cs3022a3,代码行数:28,代码来源:driver.cpp

示例3: Test

void Test()
{
	int w[] = {5,29,7,8,14,23,3,11};
	HuffmanTree *ht = new HuffmanTree();
	ht->huffmanInit(w, 8);
	ht->printTree(15);
	ht->createHuffmanCode(8);
	ht->printCode(8);
}
开发者ID:cutterpoint,项目名称:DataStructureAndAlgorithm,代码行数:9,代码来源:main.cpp

示例4: main

int main(){
	ifstream file;
	file.open("Amazon_EC2.txt");
	if(!file.is_open()) cout << "File not found";

	//Create a vector of freqs
	//256 for ascii values initialized to 0
	vector<int> freq(256,0);

	int c;
	int count = 0;
	while(file.good()){
		count++;
		c = file.get();
		//End of file
		if(c < 0) continue;
		freq[c] = freq[c] + 1;
	}
	file.close();

	/*
	for(int i = 0; i < freq.size(); i++){
		if(freq[i] != 0){
			double ratio = ((double)freq[i]/(double)count)*100;
			cout << (char)i <<  " " << ratio << endl;
		}
	}
	*/

	//Instantiate tree
	HuffmanTree tree;
	//Call create to make the tree 
	tree.create(freq);

	//Create output file stream
	ofstream outputFile("compressedFile.txt",ios::out);	
	stream out(outputFile);

	//Open file to get letters to compress with path
	ifstream file2;
	file2.open("Amazon_EC2.txt");

	//Call compress to write bits of each letter to the file
	char z;
	while(file2.good()){
		z = (char)file2.get();	
		tree.compress(z, out);
	}

	file2.close();
	outputFile.close();
	cout << endl << "Outputfile Created: compressedFile.txt" << endl << endl;
	return 0;	
}
开发者ID:nayrk,项目名称:CS165,代码行数:54,代码来源:huffman.cpp

示例5: main

int main( int argc, char* argv[] )
{
  if ( argc != 2 ) {
    cerr << "\n    usage: " << argv[0] << " file\n\n";
    return 1;
  }
  string exec = argv[0];
  string fileName = argv[1];
  bool   compressFlag;

  exec.replace(0,exec.find_last_of('/')+1,""); // Get 'basename' of arg[0]
  if (exec == "compress") compressFlag = true;
  else                    compressFlag = false;

  if (compressFlag) {	// Compress

    string compressedFile = fileName + ".hc";

    CountTable  ct(fileName.c_str());
    HuffmanTree ht;

    ct.createCharMap();
    //ct.printCharMap();
    ct.buildNodeList(ht);
    //ht.printNodeList();
    ht.buildTree();
    //ht.printTree();
    ht.genCharSeqTable();
    //ht.printCharSeqTable();

    CompressFile cf(fileName, compressedFile);
    //ht.printNodeList();
    cf.doit(ht);

  } else {		// Uncompress

    string compressedFile   = fileName + ".hc";
    string uncompressedFile = fileName + ".uc";

    //  CountTable  ct(fileName);
    HuffmanTree ht;
    UncompressFile uf(compressedFile, uncompressedFile);

    uf.buildNodeList(ht);
    //ht.printNodeList();
    ht.buildTree();
    //ht.printTree();
    //uf.printCompressedBits();
    uf.doit(ht);

 }

} // End Main()
开发者ID:timj-pdx,项目名称:huffman,代码行数:53,代码来源:main.cpp

示例6: main

int main()
{

    HuffmanTree H;
    char c[50];
    int n=0;
    H.Show(c,n);
    cout<<endl;
    cout<<"请输入01序列#结尾"<<endl;
    cin>>c;
    H.Translate(c);
    return 0;
}
开发者ID:WolfForMoon,项目名称:Local-C-Code,代码行数:13,代码来源:main.cpp

示例7: main

int main()
{
	ifstream image;
	image.open("/home/cxy229/image/data.dat");
    if(!image.good())     //文件未成功打开
    {
        cerr<<" error:: unable to open image! "<<endl;
        exit(1);
    }
    char aaaa[4]={'a','b','c','d'};
    int w[4]={5,6,7,8};
    HuffmanTree<char> test;
    test.setTree(w,aaaa,4,0);
    test.encode(test.getroot(),"");
    cout<<"d:"<<test.coding('d')<<endl;
	cout<<"c:"<<test.coding('c')<<endl;
    cout<<"b:"<<test.coding('b')<<endl;
    cout<<"a:"<<test.coding('a')<<endl;
    cout<<test.uncoding("010110");
    cout<<endl;

    imgCompressor img;
    img.run();
    return 0;
}
开发者ID:cxy229,项目名称:dshomework,代码行数:25,代码来源:dshw3_4_1_1.cpp

示例8: doit

void UncompressFile::doit(HuffmanTree& ht)
{
    if (!osPtr.is_open()) {     // Open file
        osPtr.open(outFile.c_str());
        if (!osPtr) {
            cerr << "Error: Could not open: '" << outFile << "' for writing" << endl;
            return;
        }
    }

    unsigned char c;

    while (true) {
        c = ht.getChar(getBit());
        if (c < (unsigned char)'\x80') {
            //      if ( c  > ' ' ) printf("'%c'\n", c);
            //      else            printf("'%02x'\n", c);
            osPtr.put(c);
            continue;
        } else if (c == (unsigned char)EOF) {
            break;
        } else if (c == (unsigned char)'\xf0') { // Not there yet
            continue;
        } else if (c == (unsigned char)'\xf1') {
            cout << "uncompress() : Internal Error\n";
            break;
        }
    }
    if ( isPtr.is_open()) isPtr.close();
    if ( osPtr.is_open()) osPtr.close();

} // End doit()
开发者ID:timj-pdx,项目名称:huffman,代码行数:32,代码来源:uncompress_file.cpp

示例9:

string
HuffmanEncoder::decodeBits(vector<bool> bits, vector<string> huffmanMap)
{
    HuffmanTree *tree = HuffmanEncoder::huffmanTreeFromMap(huffmanMap);

    vector<string> map = HuffmanEncoder::huffmanEncodingMapFromTree(tree);
    HuffmanEncoder::writeEncodingMapToFile(map, "encmap.map");

    HuffmanNode *n = tree->GetRoot();
    ostringstream result{};
    uint64_t last_index = 0;
    string stack;
    int num_decoded = 0;

    //tree->Print();

    for (auto it = bits.begin(); it != bits.end(); ++it) {
        bool bit = *it;
        if (bit == false) {
            stack += "0";
            n = ((HuffmanInternalNode*)n)->GetLeftChild();
        }
        else {
            stack += "1";
            n = ((HuffmanInternalNode*)n)->GetRightChild();
        }
        if (n->IsLeaf()) {
            result << ((HuffmanLeafNode*)n)->GetValue();
            num_decoded++;
            n = tree->GetRoot();
            stack = "";
        }
        last_index++;
    }

    /* TODO: perhaps the priority queue is different for each? That might
     * explain it. Compare frequencies, that can't be wrong. Issue is we have
     * different huffman maps on each run. Although, that might not be a problem
     * on write. The files are slightly different, make sure it's writing to a
     * good offset. Maybe try writing/reading garbage from that spot or
     * something, print out the first few chars, idk. Print where the offsets
     * and such are. Figure out exactly what is going where and if the way it's
     * getting compressed/decompressed differently is a problem. */


    return result.str();
}
开发者ID:raemarks,项目名称:ParallelHuffmanEncoding,代码行数:47,代码来源:HuffmanEncoder.cpp

示例10: addCharacter

void addCharacter(HuffmanTree& T,int c,Writer& w)
{
	T.writeCode(T.getLeaf(c),w);
	if (T.getLeaf(c)==T.getLeaf(NYT))
	{
		//cout << c;
		w.writeNine(c);
		T.splitNYT(T.getLeaf(NYT),c);
	}
	T.updateBlockFirst();
	T.updateTree(T.getLeaf(c));
	//cout << (T.getRoot())->getWeight() << endl;
}
开发者ID:lolipopper,项目名称:EZCompressor,代码行数:13,代码来源:Compressor.cpp

示例11: main

int main(int argc, char* argv[]) {
    if (argc < 4) {
        cerr << "please provide an appropriate flag (-compress/-decompress) and two file names." << endl;
        return -1;
    }

    HuffmanTree tree;
    ifstream in(argv[2], ios::in | ios::binary);
    ofstream out(argv[3], ios::out | ios::binary);

    if (strcmp(argv[1], "-compress") == 0) {
        tree.encode(in, out);
    } else if (strcmp(argv[1], "-decompress") == 0) {
        tree.decode(in, out);
    }

    return 0;
}
开发者ID:gfv,项目名称:itmo-cpp-assignment-2,代码行数:18,代码来源:main.cpp

示例12: createTable

//-------------------------------------------------------------------------------------------------
bool Huffman::createTable(std::vector<unsigned int> symbols, std::vector<unsigned int> counts)
{
	HuffmanTree tree; // Tree for creating the table
	
	// Build the tree
	tree.buildTree(symbols, counts);
	
	// Get the table from the tree
	if(! tree.getTable((*m_pSymbols), (*m_pCodes)))
	{
		std::cerr << "Error in createTable: tree is empty" << std::cout;
		return false;
	}

	//Sort the table by code length for faster average lookup
	sortTable();

	return true;
}
开发者ID:Byron-Miles,项目名称:code-samples,代码行数:20,代码来源:Huffman.cpp

示例13: main

int main() {
	string line;
	ifstream f("Huffman.txt");
	getline(f, line);
	HuffmanTree* ht = new HuffmanTree(line);
	cout << "original message: " << line << "\n\n";
	ht->buildTable();
	ht->buildHeap();
	ht->displayFreqTable();
	ht->displayHuffTable();
	ht->writeEncodedString();
	ht->decodeHuffman();
	ht->writeDecodedString();
	ht->compressEncodedString();
	ht->decompress();

	system("pause");
	return 0;

}
开发者ID:prestdog,项目名称:Huffman-Tree,代码行数:20,代码来源:main.cpp

示例14: node

std::unordered_map<char, std::string>  HuffmanTree::compress_file(std::string inputFile, std::string outputFile) {
    // variables and typedef
    typedef HuffmanTree::HuffmanNode node_type;
    HuffmanTree tree;
    std::unordered_map<char, unsigned int> map;
    std::unordered_map<char, std::string> code_table;
    std::priority_queue<node_type,std::vector<node_type>, HuffmanTree::Compare> prior_q;
    // calling methods
    tree.create_map(map,inputFile);
    tree.fill_queue(prior_q,map);
    HuffmanTree::HuffmanNode node(tree.build_tree(prior_q));
    tree.generate_code_table(node,code_table,"");
    tree.generate_code_file(outputFile, code_table);
    std::string bit_string = tree.generate_bit_string(inputFile,code_table);
    tree.generate_binary_compressed_file(outputFile, bit_string);
    tree.generate_compressed_file(outputFile, bit_string);
    return code_table;
}
开发者ID:Kojey,项目名称:csc3022h_cpp_3,代码行数:18,代码来源:huffmanTree.cpp

示例15: while

HuffmanTree *HuffmanTreeFactory::BuildTree(u32 code_symbols)
{
	if (_heap.size() < 1) return 0;

	while (_heap.size() > 1)
	{
		HuffmanTreeNode *branch = new HuffmanTreeNode;

		CAT_OBJCLR(*branch);
		ProbabilityType probability_sum = 0.;

		// For each code symbol,
		for (u32 ii = 0; ii < code_symbols; ++ii)
		{
			HuffmanTreeNode *leaf = _heap.top();
			_heap.pop();

			branch->children[ii] = leaf;
			probability_sum += leaf->probability;

			if (_heap.empty())
				break;
		}

		branch->probability = probability_sum;

		// Push the combined branch back on the heap
		_heap.push(branch);
	}

	HuffmanTreeNode *root = _heap.top();
	_heap.pop();

	HuffmanTree *tree = _tree;
	tree->Initialize(code_symbols, root);
	_tree = 0;

	return tree;
}
开发者ID:Kiddinglife,项目名称:geco-game-engine,代码行数:39,代码来源:Huffman.cpp


注:本文中的HuffmanTree类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。