利用哈夫曼樹實現檔案壓縮和解壓縮

2021-08-25 05:10:58 字數 3514 閱讀 9913

利用庫中的優先順序佇列實現哈夫曼樹,最後基於哈夫曼樹最終實現檔案壓縮。

描述:

1.統計檔案中字元出現的次數,利用優先順序佇列構建haffman樹,生成huffman編碼。

構造過程可以使用priority_queue輔助,每次pq.top()都可以取出權值(頻數)最小的節點。每取出兩個最小權值的節點,就new出乙個新的節點,左右孩子分別指向它們。然後把這個新節點push進優先佇列。

2.壓縮:利用haffman編碼對檔案進行壓縮,即在壓縮檔案中按順序存入每個字元的haffman編碼。

3.將檔案中出現的字元以及它們出現的次數寫入配置檔案中,以便後續壓縮使用。

4.減壓縮:利用配置檔案重構haffman樹,對檔案進行減壓縮。

#define _crt_secure_no_warnings 1

#pragma once

#include

#include

#include

using

namespace

std;

template

struct huffmantreenode

huffmantreenode*_pleft;

huffmantreenode*_pright;

huffmantreenode*_pparent;

w _weight;

};template

class huffmantree

huffmantree(w*array, size_t size, const w&invalid)

void _destroy(pnode&proot)

}~huffmantree()

pnode getroot()

private:

//構建哈夫曼樹

void _createhuffmantree(w*array, size_t size, const w&invalid)

};priority_queuevector

, ptrnodecompare>hp;

for (size_t i = 0; i < size; ++i)

}//空堆

if (hp.empty())

_proot = null;

while (hp.size()>1)

_proot = hp.top();

}public:

pnode _proot;

};

#define _crt_secure_no_warnings 1

#pragma once

#include"haffman.hpp"

#include

#include

using

namespace

std;

#include

typedef

long

long longtype;

struct charinfo

charinfo operator+(const charinfo&info)

bool

operator

};class filecompress

;public:

//建構函式

filecompress()

}//獲取哈夫曼編碼

void generatehuffmancode(node*root,string code)//code不能傳引用??

generatehuffmancode(root->_pleft, code+'0');

generatehuffmancode(root->_pright, code + '1');

}void compress(const

char *file)//file:原始檔

//2.生成huffmantree 及code

charinfo invalid;

invalid._count = 0;

huffmantreetree(_infos, 256, invalid);//引數:陣列,256個,無效值(出現0次)

string compressfile = file;//

compressfile += ".huffman";//?

file*fin = fopen(compressfile.c_str(),"wb");//開啟壓縮檔案

assert(fin);

string code;

generatehuffmancode(tree.getroot(), code);

寫入字元出現的資訊

//fwrite(_infos, sizeof(charinfo), 256, fin);

int writenum = 0;

int objsize = sizeof(tmpinfo);

for (rsize_t i = 0; i < 256; ++i)

}tmpinfo info;

info._count = -1;

fwrite(&info, objsize, 1, fin);//把info._count = -1寫進去作為結束標誌位

//3.壓縮

fseek(fout, 0, seek_set);//檔案指標、偏移量、參照位置

ch = fgetc(fout);

char value = 0;

size_t pos = 0;

while (ch != eof)

++pos;

if (pos == 8)

}ch = fgetc(fout);

}if (pos > 0)

fclose(fout);

fclose(fin);

}void uncompress(const

char *file)

int aaa = 0;

//重建huaffman樹

charinfo invalid;

invalid._count = 0;

huffmantreetree(_infos, 256, invalid);//引數:陣列,256個,無效值(出現0次)

node *root = tree.getroot();

node*cur = root;

longtype n = root->_weight._count;//所有葉子節點的和(原始檔字元的個數)

char ch = fgetc(fout);//從fout(壓縮檔案)讀字元

34 哈夫曼樹壓縮文字和解壓文字

利用輸入流和輸出流進行文字的讀出和寫入,注意 壓縮檔案時會用到物件流 objectoutputstream寫入檔案 解壓檔案時,也會用到物件流 objectinputstream,讀出檔案 壓縮檔案 public void zipfile string srcfile,string dstfile ...

檔案壓縮(哈夫曼樹實現)

專案描述 專案簡介 利用哈夫曼編碼的方式對檔案進行壓縮,並且對壓縮檔案可以解壓 開發環境 windows vs2013 專案概述 1.壓縮 a.讀取檔案,將每個字元,該字元出現的次數和權值構成哈夫曼樹 b.哈夫曼樹是利用小堆構成,字元出現次數少的節點指標存在堆頂,出現次數多的在堆底 c.每次取堆頂的...

哈夫曼樹壓縮 解壓縮檔案

tips 注意二進位制讀寫檔案回車為 r n 詳細分析改天再填坑。還有單純形演算法 github include include include include include include include using namespace std template struct huffnode ...