zoukankan      html  css  js  c++  java
  • 数据结构实习 problem O Huffman Tree

    Huffman Tree

    题目描述

    对输入的英文大写字母进行统计概率 然后构建哈夫曼树,输出是按照概率降序排序输出Huffman编码。

    输入

    大写字母个数 n
    第一个字母 第二个字母 第三个字母 ... 第n个字母。

    输出

    字母1 出现次数 Huffman编码
    字母2 出现次数 Huffman编码
    字母3 出现次数 Huffman编码

    字母n 出现次数 Huffman编码

    样例输入

    10

    I I U U U I U N U U

    样例输出

    U 6 1

    I 3 01

    N 1 00

    分析:

    采用的是优先队列进行哈夫曼树的建立,然后采用的是构建新的节点,从根部进行遍历得到叶子节点的哈夫曼编码。

    出现重大错误:

    内存使用问题:
    由于在使用过程中已经将队列中全部给弹出了,之后又要使用该节点导致了内存已经泄露.

    因此要控制循环条件qu.size() > 1

    代码如下:

    #include <iostream>
    #include <cstring>
    #include <vector>
    #include <queue>
    #include <cstdio>
    #include <algorithm>
    
    using namespace std;
    const int maxn = 27;
    int rec[maxn];
    struct tree
    {
        char ch;
        int num;
        tree * lchild;
        tree * rchild;
    
        tree(char cc = '@',int nn = 0):ch(cc),num(nn),lchild(NULL),rchild(NULL) {}
    };
    
    struct cmp
    {
        bool operator()(const tree *a,const tree* b)const
        {
            return a->num > b->num;
        }
    };
    
    priority_queue<tree*,vector<tree*>,cmp> qu;
    
    void createTree()
    {
        for(int i = 0  ; i < 26 ; i++)
        {
            if(rec[i])
            {
                tree * node = new tree;
                node->num = rec[i];
                node->ch = (char)(i+'A');
                qu.push(node);
            }
        }
    
        while(qu.size() > 1)
        {
            tree * f = qu.top();
            qu.pop();
            tree * s = qu.top();
            qu.pop();
    
            tree * tmp = new tree();
            tmp->num = f->num + s->num;
            tmp->lchild = f,tmp->rchild = s;
            qu.push(tmp);
        }
    }
    struct printTree
    {
        int num;
        char ch;
        string code;
        ~printTree() {}
    };
    vector<printTree> PT;
    void print(tree* root, string res)
    {
        if(root == NULL)
            return ;
        if(root->ch != '@')
        {
            printTree pt;
            pt.num = root->num;
            pt.ch = root->ch;
            pt.code = res;
            PT.push_back(pt);
            return;
        }
        print(root->lchild,res+"0");
        print(root->rchild,res+"1");
        delete root->lchild;
        delete root->rchild;
    }
    bool cmp2(const printTree & a,const printTree & b)
    {
        return a.num > b.num;
    }
    int main()
    {
    //    freopen("in.txt","r",stdin);
        for(int i = 0 ; i < maxn ; i++)
            rec[i] = 0;
        int num;
        cin >> num;
        char ch;
        for(int i = 0 ; i < num ; i++)
        {
            cin >> ch;
            rec[ch-'A']++;
        }
        createTree();
        tree * root = qu.top();
        qu.pop();
        print(root,"");
        sort(PT.begin(),PT.end(),cmp2);
        for(size_t i = 0 ; i < PT.size(); i++)
        {
            cout << PT[i].ch << " " << PT[i].num << " " << PT[i].code << endl;
        }
        return 0;
    }
    
    
  • 相关阅读:
    My Tornado Particle Effect
    [zz] 海洋环境的光能传递
    一道算法题
    Alembic
    一些莫名其妙的东东
    Python Q&A
    <<Exceptional C++>> notes
    CG Rendering v.s. Browser Rendering
    Modo
    Katana
  • 原文地址:https://www.cnblogs.com/pprp/p/7765981.html
Copyright © 2011-2022 走看看