zoukankan      html  css  js  c++  java
  • 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.
  • 相关阅读:
    三、Gradle初级教程——Gradle除了签名打包还能配置jar包
    四、Android Studio使用——什么样的Project都能导入Studio
    二、Android Studio使用——导入jar包,运行、debug都不是问题
    android-studio安装过程详解
    一、Android Studio入门——Eclipse快捷键配置
    84、PullToRefresh使用详解
    ViewPagerindicator 源码解析
    83、android的消息处理机制(图+源码分析)——Looper,Handler,Message
    82、文字混动效果
    Bogart BogartPublic.vb
  • 原文地址:https://www.cnblogs.com/Shinered/p/9155937.html
Copyright © 2011-2022 走看看