• Huffman编码


    题目是求霍夫曼树的最短和最长深度。霍夫曼原理不在此说明。

    参考:https://blog.csdn.net/Alfa_/article/details/53823649

    C++代码如下,仅供参考:

    #include <iostream>
    #include <ctime>
    #include <fstream>
    #include <sstream>
    #include <vector>
    #include <queue>
    #include <string>
    using namespace std;
    /***********霍夫曼树的节点***********/
    class Node
    {
    public:
        long weight;
        int data;
        int max;
        int min;
        Node *left;
        Node *right;
        Node *parent;
    };
    
    class mycompare
    {
        bool reverse;
    public:
        mycompare(const bool &re = true)
        {
            reverse = re;
        }
        bool operator() (Node *a, Node *b) const
        {
            //优先队列,从小到大排列mycompare,按照weight的从小到大排列
            if(reverse) return(a->weight > b->weight);
            else {
                return (a->weight < b->weight);
                //从大到小排列
            }
        }
    };
    
    class Huffman
    {
    public:
        Huffman()
        {
            ifstream fin("4.txt");
            string line;
            stringstream stream;
            int cnt = 0;
            if(getline(fin, line))
            {
                stream << line;
                stream >> length;
            }
    
            while(getline(fin, line))
            {
                long temp;
                Node *enode = new Node;
                stream.clear();
                stream << line;
                stream >> temp;
                enode->data = cnt;
                enode->weight = temp;
                enode->max = 0;
                enode->min = 0;
                mypq.push(enode);
    //            symbols.push_back(temp);
            }
        }
        int max(int a, int b)
        {
            return a > b ? a : b;
        }
    
        int min(int a, int b)
        {
            return a < b? a : b;
        }
        Node* CreateTree()
        {
            while(mypq.size() > 1)
            {
                Node *enode1 = new Node;
                Node *enode2 = new Node;
                Node *node = new Node;
                enode1 = mypq.top();
                mypq.pop();
                enode2 = mypq.top();
                mypq.pop();
                node->max = max( enode1->max, enode2->max ) + 1;
                node->min = min( enode1->min, enode2->min ) + 1;
                node->weight = enode1->weight + enode2->weight;
                node->left = enode1;
                enode1->parent = node;
                node->right = enode2;
                enode2->parent = node;
                mypq.push(node);        
            }
            
        //    Node root = new Node;
            root = mypq.top();
        //    mypq.pop();
            return root;
        }
    
    public:
        Node* root = new Node;    //根节点
    //    vector<long> symbols;
        int length;
        priority_queue<Node*, vector<Node*>, mycompare > mypq;//优先队列
        vector<Node*> huffmanTree;
    };
    
    
    int main()
    {
        Huffman _huffman;
        cout << "max:" << _huffman.CreateTree()->max << endl;
        cout << "min:" << _huffman.CreateTree()->min << endl;
        return 0;
    }
    The Safest Way to Get what you Want is to Try and Deserve What you Want.
  • 相关阅读:
    征战蓝桥 —— 2016年第七届 —— C/C++A组第5题——消除尾一
    UVA 10410 Tree Reconstruction (树重建)
    UVA 12166 Equilibrium Mobile (修改天平)(dfs字符串表示的二叉树)
    UVA 1600 Patrol Robot (巡逻机器人)(bfs)
    UVA 712 STrees(S树)
    UVA 536 Tree Recovery (二叉树重建)
    Stall Reservations【贪心】【堆】
    Sunscreen【贪心】
    Sunscreen【贪心】
    Sunscreen【贪心】
  • 原文地址:https://www.cnblogs.com/Shinered/p/9155937.html
Copyright © 2020-2023  润新知