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


C++ HuffmanTree::insert方法代码示例

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


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

示例1: assert

TableInfo phuffman::utility::BuildTable(InputIterator first, InputIterator last, Codes& table) {
    using namespace std;
    typedef DepthCounterNode Node;
    typedef multimap<size_t, Node*> HuffmanTree;
    typedef vector<Node*> Nodes;

    assert(distance(first, last) <= constants::MAXIMUM_DATABLOCK_SIZE);
    assert(table.size() >= constants::ALPHABET_SIZE);

    HuffmanTree tree;
    Nodes leafs;
    TableInfo info;

    // Initialize tree
    {
        Frequencies frequencies = CountFrequencies(first, last);
        Frequencies::const_iterator first = frequencies.begin(), last = frequencies.end();
        while (first != last) {
            Node* leaf = new Node(first->symbol);
            tree.insert(make_pair(first->frequency, leaf));
            leafs.push_back(leaf);
            ++first;
        }
    }

    // Build tree
    {
        for (size_t i=0, size=tree.size(); i<size-1; ++i) {
            HuffmanTree::iterator first = tree.begin(), second = tree.begin();
            ++second;
            size_t freq = first->first + second->first; // Calculate freq for a node
            Node* node = new Node(first->second, second->second); 
            ++second;
            tree.erase(first, second); // Remove two nodes with the smallest frequency
            tree.insert(make_pair(freq, node)); // Add node that points to previosly removed nodes
        }
        assert(tree.size() == 1);
    }

    // Count codelengths
    // In fact, codelengths are already counted in the 'depth' member of a node
    // There is only one exception: if the tree contains only one object, we need to set it's depth manually
    Node* root = tree.begin()->second;
    root->depth = 1;

    // Sort nodes by codelength
    sort(leafs.begin(), leafs.end(), TreeComparator);

    // Build table
    {
        Nodes::const_iterator first = leafs.begin(), last = leafs.end();
        Node *curNode = *first;
        info.maximum_codelength = curNode->depth;
        Code curCode = CodeMake(curNode->depth, 0);
        table[curNode->element] = curCode;
        ++first;
        while (first != last) {
            assert(curNode->depth >= curCode.codelength);
            curNode = *first;
            // If current codeword and next codeword have equal lengths
            if (curNode->depth == curCode.codelength) {
            // Just increase codeword by 1
                curCode.code += 1;
            }
            // Otherwise
            else {
                // Increase codeword by 1 and _after_ that shift codeword right
                curCode.code = (curCode.code + 1) >> (curNode->depth - curCode.codelength);
            }
            curCode.codelength = curNode->depth;
            table[curNode->element] = curCode;
            ++first;
        }
    }
    
    delete root;

    return info;
}
开发者ID:Kentzo,项目名称:phuffman,代码行数:79,代码来源:phuffman.hpp


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