文件压缩总结-哈夫曼树

详细源代码请移步下载:https://github.com/HsTime/file-campress
这里写图片描述这里写图片描述
项目:文件压缩流程图
这里写图片描述
建立小堆代码:

#pragma once

#include<iostream>
using namespace std;
#include<vector>
#include"huffman.h"

template<class T>
struct Less
{
    bool operator()(const T & l, const T& n)
    {
        return l < n;
    }
};
template<class T>
struct Greater
{
    bool operator()(const T& l, const T& n)
    {
        return l > n;
    }
};
template<class T, class Comper>
class Heap
{
public:
    Heap()
        :heap(NULL)
    {}
    Heap(T* arr, size_t n)
    {
        heap.reserve(n);
        for (size_t i = 0; i < n; i++)
        {
            heap.push_back(arr[i]);
        }
        int parent = (heap.size() - 2) / 2;
        while (parent >= 0)
        {
            _AdjustDown(parent, heap.size() - 1);
            parent--;
        }
    }
    T Top()
    {
        return heap[0];
    }
    Heap(const vector<T>& x)
    {
        heap.clear();
        heap.resize(x.size(), 0);
        for (size_t i = 0; i < x.size(); i++)
        {
            heap[i] = x[i];
        }
        int parent = (heap.size() - 2) / 2;
        while (parent >= 0)
        {
            _AdjustDown(parent, heap.size() - 1);
            parent--;
        }
    }

    void push(const T& x)
    {
        heap.push_back(x);
        _AdjistUp(heap.size() - 1);
    }

    void Pop()
    {
        swap(heap[0], heap[heap.size() - 1]);
        heap.pop_back();
        _AdjustDown(0, heap.size() - 1);
    }
    bool Empty()
    {
        return heap.empty();
    }
    size_t Size()
    {
        return heap.size();
    }
    void Print()
    {
        vector<int>::iterator it = heap.begin();
        while (it != heap.end())
        {
            cout << *it << " ";
            it++;
        }
        cout << endl;
    }

protected:
    void _AdjustDown(int root, int size)//Ïòϵ÷Õû
    {
        Comper comper;
        int parent = root;
        int child = root * 2 + 1;
        while (child <= size)
        {
            if (child < size && comper(heap[child + 1], heap[child]))
            {
                child++;
            }
            if (comper(heap[child], heap[parent]))
            {
                swap(heap[parent], heap[child]);
                parent = child;
                child = child * 2 + 1;
            }
            else
            {
                break;
            }
        }
    }

    void _AdjistUp(int pos)
    {
        Comper comper;
        int child = pos;
        int parent = (child - 1) / 2;

        while (child > 0)
        {
            if (comper(heap[child], heap[parent]))
            {
                swap(heap[child], heap[parent]);
                child = parent;
                parent = (parent - 1) / 2;
            }
            else
                break;
        }
    }
private:
    vector<T> heap;
};

压缩与解压缩:

#pragma once
#include"huffman.h"
#include<string>
#include<assert.h>

typedef unsigned long long TypeLong;

struct CharInfo
{
    unsigned char _ch; //字符
    TypeLong _count;//出现次数
    string _code;//Huffman编码

    CharInfo(TypeLong count = 0) 
        :_ch(0)
        , _count(count)
        , _code("")
    {}
    bool operator!=(const CharInfo& info)const
    {
        return this->_count != info._count;
    }
    bool operator<(const CharInfo& info)const
    {
        return this->_count < info._count;
    }
    CharInfo operator+(const CharInfo& info)const
    {
        return CharInfo(this->_count + info._count);
    }
};

struct CountInfo
{
    unsigned char _ch;        //字符
    TypeLong _count;          //字符出现的次数
};

class FileCompress
{
public:
    FileCompress();
    void CompressHuffCode(const char* filename);
    void UnCompressHuffCode(const char* filename);
    void PrintCode()const;

protected:
    static void GenerateHuffmanCode(HuffmanNode<CharInfo>* root, FileCompress& file, string& code);

protected:
    CharInfo _info[256];

};
FileCompress::FileCompress()
{
    size_t size = sizeof(this->_info) / sizeof(this->_info[0]);
    for (size_t i = 0; i < size; ++i)
    {
        this->_info[i]._ch = i;
        this->_info[i]._count = 0;
    }
}

void FileCompress::CompressHuffCode(const char* filename)
{
    assert(filename);
    FILE* fOut = fopen(filename, "rb");
    assert(fOut);
    //统计字符出现的次数
    char ch = fgetc(fOut);
    while (!feof(fOut))
    {
        ++this->_info[(unsigned char)ch]._count;
        ch = fgetc(fOut);
    }

    //建立哈夫曼树
    CharInfo invalid;
    invalid._count = 0;
    HuffmanTree<CharInfo> huffTree(this->_info, sizeof(this->_info) / sizeof(this->_info[0]), invalid);

    //生成哈夫曼编码
    string code;
    HuffmanNode<CharInfo>* root = huffTree.GethuffmanNode();
    GenerateHuffmanCode(root, *this, code);

    //生成压缩文件名及配置文件名
    string fileInName = (string)filename; //压缩文件名
    string fileConfig = fileInName;//配置文件名
    size_t last_ = fileInName.find_last_of('.');
    if (last_ < fileInName.size())
    {
        fileInName.erase(last_);
        fileConfig.erase(last_);
    }
    fileInName += ".huff";
    fileConfig += ".config";


    string tmp;
    CountInfo info;
    //生成压缩配置文件
    FILE* fConfig = fopen(fileConfig.c_str(), "wb");
    for (size_t i = 0; i < 256; ++i)
    {
        if (_info[i]._count)
        {
            info._ch = _info[i]._ch;
            info._count = _info[i]._count;
            fwrite(&info, sizeof(info), 1, fConfig);
        }
    }
    info._count = -1;
    fwrite(&info, sizeof(info), 1, fConfig);

    //对文件进行压缩
    FILE* fIn = fopen(fileInName.c_str(), "wb");
    assert(fIn);
    fseek(fOut, 0, SEEK_SET);
    int pos = 0;
    unsigned char putch = 0;
    ch = fgetc(fOut);
    while (!feof(fOut))
    {
        tmp = this->_info[(unsigned char)ch]._code;
        for (size_t i = 0; i < tmp.size(); ++i)
        {
            putch <<= 1;
            putch |= (tmp[i] - '0');
            if (++pos == 8)
            {
                fputc(putch, fIn);
                pos = 0;
                putch = 0;
            }
        }
        ch = fgetc(fOut);
    }
    if (pos > 0)
    {
        putch <<= (8 - pos);
        fputc(putch, fIn);
    }

    fclose(fOut);
    fclose(fIn);
    fclose(fConfig);
}

void FileCompress::GenerateHuffmanCode(HuffmanNode<CharInfo>* root, FileCompress& file, string& code)
{
    if (root == NULL)
    {
        return;
    }
    if (root->_left == NULL && root->_right == NULL)
    {
        file._info[root->_weight._ch]._code = code;
        return;
    }
    code.push_back('0');
    GenerateHuffmanCode(root->_left, file, code);
    code.pop_back();
    code.push_back('1');
    GenerateHuffmanCode(root->_right, file, code);
    code.pop_back();
}

void FileCompress::UnCompressHuffCode(const char* filename)
{
    assert(filename);
    FILE* fOut = fopen(filename, "rb");
    assert(fOut);
    //读取文件,
    string fileConfig = (string)filename;
    string fileInName = fileConfig;
    size_t last_ = fileInName.find_last_of('.');
    if (last_ < fileInName.size())
    {
        fileConfig.erase(last_);
        fileInName.erase(last_);
    }
    fileConfig += ".config";
    /*fileInName += "_Com.JPG";*/
    fileInName += "_Com.mp3";
    /*fileInName += "_Com.txt";*/
    /*fileInName += "_Com.doc";*/

    FILE* fIn = fopen(fileInName.c_str(), "wb");
    assert(fIn);
    FILE* fConfig = fopen(fileConfig.c_str(), "rb");
    assert(fConfig);

    CountInfo info;
    //读配置信息
    while (1)
    {
        fread(&info, sizeof(CountInfo), 1, fConfig);
        if (info._count == -1)
        {
            break;
        }
        _info[(unsigned char)info._ch]._ch = info._ch;
        _info[(unsigned char)info._ch]._count = info._count;
    }

    //重建哈夫曼树
    HuffmanTree<CharInfo> tree(this->_info, sizeof(this->_info) / sizeof(this->_info[0]), 0);
    HuffmanNode<CharInfo>* root = tree.GethuffmanNode();
    HuffmanNode<CharInfo>* cur = root;
    unsigned char ch = fgetc(fOut);
    int pos = 7;
    if (ch  == 255)
    {
        if (info._ch != 0)
        while (_info[(unsigned char)info._ch]._count--)
        {
            {
                fputc(info._ch, fIn);
            }
        }
    }
    else
    {
        TypeLong countSum = root->_weight._count;
        while (countSum > 0)
        {
            while (pos >= 0)
            {
                if ((ch & (1 << pos)) == 0) //向左走
                {
                    cur = cur->_left;
                }
                else
                {
                    cur = cur->_right;
                }
                if (cur->_left == NULL && cur->_right == NULL)
                {
                    fputc(cur->_weight._ch, fIn);
                    //cout << cur->_weight._ch;

                    if (--countSum == 0)//将没有写的字符的次数减1
                        break;
                    cur = root;
                }
                --pos;
            }
            pos = 7;
            ch = fgetc(fOut);
        }
    }
    fclose(fIn);
    fclose(fOut);
    fclose(fConfig);
}

void FileCompress::PrintCode()const
{
    for (int i = 0; i < 256; ++i)
    {
        if (this->_info[i]._count != 0)
        {
            cout << this->_info[i]._ch << ":>" << this->_info[i]._code << endl;
        }
    }
}

(2)为什么要使用配置文件?
在项目中,将字符对应的编码转化为位,在unsigned char中填充位,填满后就写入到压缩文件中。
问题1:最后一个字节是不是很有可能没有填满,该如何判断他是否填满以及填了几个字符的编码?
问题2:若依次压缩一些文件,压缩完后再去解压,那么编码此时已经没有了,该如何解压?
上面的两个问题可通过配置文件解决,假如要压缩的文件叫xxx,那么可生成一个xxx.config的配置文件,在该配置文件中写入<文件的总长度>(恢复时知道应该应该恢复多少个字符),(字符以及其出现的次数,用于解压时重建哈夫曼树),利用该配置文件即可解决这两个问题。

  • 19
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
综合实验: 1. 问题描述 利用哈夫曼编码进行通信可以大大提高信道利用率,缩短信息传输时间,降低传输成本。这要求在发送端通过一个编码系统对待传输数据预先编码,在接收端将传来的数据进行译码(复原)。对于双工信道(即可以双向传输信息的信道),每端都需要一个完整的编/译码系统。试为这样的信息收发站编写一个哈夫曼码的编/译码系统。 2. 基本要求 一个完整的系统应具有以下功能: (1) I:初始化(Initialization)。从终端读入字符集大小n,以及n个字符和n个权值,建立哈夫曼树,并将它存于文件hfmTree中。 (2) E:编码(Encoding)。利用已建好的哈夫曼树(如不在内存,则从文件hfmTree中读入),对文件ToBeTran中的正文进行编码,然后将结果存入文件CodeFile中。 (3) D:译码(Decoding)。利用已建好的哈夫曼树文件CodeFile中的代码进行译码,结果存入文件Textfile中。 (4) P:印代码文件(Print)。将文件CodeFile以紧凑格式显示在终端上,每行50个代码。同时将此字符形式的编码文件写入文件CodePrin中。 (5) T:印哈夫曼树(Tree printing)。将已在内存中的哈夫曼树以直观的方式(比如树)显示在终端上,同时将此字符形式的哈夫曼树写入文件TreePrint 中。 3. 测试数据 用下表给出的字符集和频度的实际统计数据建立哈夫曼树,并实现以下报文的编码和译码:“THIS PROGRAME IS MY FAVORITE”。 字符 A B C D E F G H I J K L M 频度 186 64 13 22 32 103 21 15 47 57 1 5 32 20 字符 N O P Q R S T U V W X Y Z 频度 57 63 15 1 48 51 80 23 8 18 1 16 1

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值