資源簡介
壓縮包中包含實驗報告,運行視頻,是數(shù)據(jù)結(jié)構(gòu)實驗課程作業(yè),可以借鑒參考。其中功能包括輸入字母及頻率,然后生成相應(yīng)的哈夫曼編碼,然后編碼txt文件中的文本,輸出,并且會把輸出結(jié)果存入文件。重新打開控制臺,可以通過讀取文件重新建立哈夫曼樹,就很強(qiáng)
代碼片段和文件信息
#include“heap.h“
#include
#include
#include
#include
#include
using?namespace?std;
struct?HuffmanNode{
int?weight;//權(quán)值
char?data;//字母
int?num;//記錄編碼序號
HuffmanNode?*leftchild?*rightchild?*parent;
HuffmanNode(){?weight?=?0;?data?=?‘#‘;?num?=?-1;?leftchild?=?0;?rightchild?=?0;?parent?=?0;?}
bool?operator<=(HuffmanNode&?R){?return?weight?<=?R.weight;?}
bool?operator>(HuffmanNode&?R){?return?weight>R.weight;?}
};
class?HuffmanTree{
public:
void?Initialize();
void?Encoding()?{?Encoding(root);?}//將輸入的字符信息編碼
void?Decoding()?{?Decoding(root);?}
void?PrintCodefile();
void?PrintHuffmanTree();
void?ReadHuffmanTree();
int?IsInitialized();
HuffmanNode*?getRoot()?{?return?root;?}
protected:
int?count;//計算輸入的字符總數(shù)
HuffmanNode?*root;//樹根
vector>codeBlock;//二維向量存儲編碼,外向量為字母對應(yīng)的編碼序號,內(nèi)向量為字母對應(yīng)的編碼
void?mergeTree(HuffmanNode&?ht1?HuffmanNode&?ht2?HuffmanNode*&parent);//合并二叉樹
void?coding(HuffmanNode*t?ofstream&?outfile);
int?preOrder(HuffmanNode*t?char?x);
void?Encoding(HuffmanNode*?t);
void?Decoding(HuffmanNode*?t);
};
void?HuffmanTree::Initialize(){
ofstream?outfile(“hfmTreeData.txt“?ios::out);
MinHeap?hp;
HuffmanNode*parent?=?0?first?second?work;
cout?<“請輸入出現(xiàn)的字母種類個數(shù)(包括空格)“?< cin?>>?count;
outfile?< cin.get();//清除回車
for?(int?i?=?0;?i cout?<“請輸入第“?< cin.get(work.data);//為了能識別空格,使用cin.get來獲取字符
cin.get();//清除回車
cin?>>?work.weight;
outfile?< cin.get();//清除回車
work.leftchild?=?0;
work.rightchild?=?0;
work.parent?=?0;
hp.Insert(work);//插入最小堆
}
for?(int?i?=?0;?i hp.RemoveMin(first);//選擇根的權(quán)值最小的樹
hp.RemoveMin(second);//選擇根的權(quán)值次小的樹
mergeTree(first?second?parent);//合并
hp.Insert(*parent);//重新插入最小堆中
}
root?=?parent;//建立根結(jié)點
codeBlock.resize(50?vector(50?-1));//編碼數(shù)組初始化
ofstream?outfile2(“hfmTree.txt“?ios::out);
coding(root?outfile2);//編碼
outfile2.close();
cout?<“初始化哈夫曼樹成功“?<};
void?HuffmanTree::mergeTree(HuffmanNode&bt1?HuffmanNode&bt2?HuffmanNode*&parent){
parent?=?new?HuffmanNode;
parent->leftchild?=?new?HuffmanNode(bt1);?parent->rightchild?=?new?HuffmanNode(bt2);
parent->weight?=?bt1.weight?+?bt2.weight;//合并之和
bt1.parent?=?bt2.parent?=?parent;//雙親重新指向
}
void?HuffmanTree::coding(HuffmanNode*?t?ofstream&?outfile){
static?int?temp[20];//臨時保存編碼的數(shù)組
static?int?i?=?0;//記錄行進(jìn)位置
static?int?j?=?0;//記錄編碼序號
if?(t->data?==?‘#‘){
temp[i]?=?0;
i++;
coding(t->leftchild?outfile);
temp[i]?=?1;
i++;
coding(t->rightchild?outfile);
}
else?if?(t->data?!=?‘#‘){
//cout?<data?<“:“;
outfile?<data;
outfile?<“:“;
t->num?=?j;//保存編碼序號
for?(int?n?=?0;?n codeBlock[j][n]?=?temp[n];
//cout?< outfile?< }
j++;
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????182784??2018-01-03?00:01??20161380162林俊賢實驗5哈夫曼\20161380162林俊賢哈夫曼報告.doc
?????文件???????9108??2018-01-02?21:08??20161380162林俊賢實驗5哈夫曼\20161380162林俊賢哈夫曼源程序\20161380162林俊賢實驗5哈夫曼\20161380162林俊賢實驗5哈夫曼.cpp
?????文件???????4204??2018-01-02?20:52??20161380162林俊賢實驗5哈夫曼\20161380162林俊賢哈夫曼源程序\20161380162林俊賢實驗5哈夫曼\20161380162林俊賢實驗5哈夫曼.vcxproj
?????文件???????1094??2018-01-02?20:52??20161380162林俊賢實驗5哈夫曼\20161380162林俊賢哈夫曼源程序\20161380162林俊賢實驗5哈夫曼\20161380162林俊賢實驗5哈夫曼.vcxproj.filters
?????文件????????122??2018-01-02?23:56??20161380162林俊賢實驗5哈夫曼\20161380162林俊賢哈夫曼源程序\20161380162林俊賢實驗5哈夫曼\Codefile.txt
?????文件????????265??2018-01-02?23:57??20161380162林俊賢實驗5哈夫曼\20161380162林俊賢哈夫曼源程序\20161380162林俊賢實驗5哈夫曼\Debug\20161380.6F16703A.tlog\20161380162林俊賢實驗5哈夫曼.lastbuildstate
?????文件???????2444??2018-01-02?23:56??20161380162林俊賢實驗5哈夫曼\20161380162林俊賢哈夫曼源程序\20161380162林俊賢實驗5哈夫曼\Debug\20161380.6F16703A.tlog\cl.command.1.tlog
?????文件??????13384??2018-01-02?23:57??20161380162林俊賢實驗5哈夫曼\20161380162林俊賢哈夫曼源程序\20161380162林俊賢實驗5哈夫曼\Debug\20161380.6F16703A.tlog\CL.read.1.tlog
?????文件????????932??2018-01-02?23:57??20161380162林俊賢實驗5哈夫曼\20161380162林俊賢哈夫曼源程序\20161380162林俊賢實驗5哈夫曼\Debug\20161380.6F16703A.tlog\CL.write.1.tlog
?????文件???????4358??2018-01-02?23:56??20161380162林俊賢實驗5哈夫曼\20161380162林俊賢哈夫曼源程序\20161380162林俊賢實驗5哈夫曼\Debug\20161380.6F16703A.tlog\li
?????文件???????3142??2018-01-02?23:57??20161380162林俊賢實驗5哈夫曼\20161380162林俊賢哈夫曼源程序\20161380162林俊賢實驗5哈夫曼\Debug\20161380.6F16703A.tlog\li
?????文件????????878??2018-01-02?23:57??20161380162林俊賢實驗5哈夫曼\20161380162林俊賢哈夫曼源程序\20161380162林俊賢實驗5哈夫曼\Debug\20161380.6F16703A.tlog\li
?????文件????????909??2018-01-02?23:57??20161380162林俊賢實驗5哈夫曼\20161380162林俊賢哈夫曼源程序\20161380162林俊賢實驗5哈夫曼\Debug\20161380162林俊賢實驗5哈夫曼.log
?????文件?????586806??2018-01-02?23:56??20161380162林俊賢實驗5哈夫曼\20161380162林俊賢哈夫曼源程序\20161380162林俊賢實驗5哈夫曼\Debug\20161380162林俊賢實驗5哈夫曼.obj
?????文件?????404480??2018-01-02?23:56??20161380162林俊賢實驗5哈夫曼\20161380162林俊賢哈夫曼源程序\20161380162林俊賢實驗5哈夫曼\Debug\vc120.idb
?????文件?????462848??2018-01-02?23:56??20161380162林俊賢實驗5哈夫曼\20161380162林俊賢哈夫曼源程序\20161380162林俊賢實驗5哈夫曼\Debug\vc120.pdb
?????文件???????2133??2017-12-14?16:41??20161380162林俊賢實驗5哈夫曼\20161380162林俊賢哈夫曼源程序\20161380162林俊賢實驗5哈夫曼\heap.h
?????文件????????263??2018-01-02?23:57??20161380162林俊賢實驗5哈夫曼\20161380162林俊賢哈夫曼源程序\20161380162林俊賢實驗5哈夫曼\hfmTree.txt
?????文件????????189??2018-01-02?21:09??20161380162林俊賢實驗5哈夫曼\20161380162林俊賢哈夫曼源程序\20161380162林俊賢實驗5哈夫曼\hfmTreeData.txt
?????文件?????????29??2018-01-02?21:01??20161380162林俊賢實驗5哈夫曼\20161380162林俊賢哈夫曼源程序\20161380162林俊賢實驗5哈夫曼\ToBeTran.txt
?????文件????7602176??2018-01-03?00:01??20161380162林俊賢實驗5哈夫曼\20161380162林俊賢哈夫曼源程序\20161380162林俊賢實驗5哈夫曼.sdf
?????文件???????1054??2018-01-02?21:23??20161380162林俊賢實驗5哈夫曼\20161380162林俊賢哈夫曼源程序\20161380162林俊賢實驗5哈夫曼.sln
????..A..H.?????15360??2018-01-03?00:01??20161380162林俊賢實驗5哈夫曼\20161380162林俊賢哈夫曼源程序\20161380162林俊賢實驗5哈夫曼.v12.suo
?????文件?????157696??2018-01-02?23:56??20161380162林俊賢實驗5哈夫曼\20161380162林俊賢哈夫曼源程序\Debug\20161380162林俊賢實驗5哈夫曼.exe
?????文件?????736148??2018-01-02?23:56??20161380162林俊賢實驗5哈夫曼\20161380162林俊賢哈夫曼源程序\Debug\20161380162林俊賢實驗5哈夫曼.ilk
?????文件????1485824??2018-01-02?23:56??20161380162林俊賢實驗5哈夫曼\20161380162林俊賢哈夫曼源程序\Debug\20161380162林俊賢實驗5哈夫曼.pdb
?????文件??????????0??2018-01-02?23:55??20161380162林俊賢實驗5哈夫曼\20161380162林俊賢哈夫曼源程序\Debug\Codefile.txt
?????文件????????263??2018-01-02?23:55??20161380162林俊賢實驗5哈夫曼\20161380162林俊賢哈夫曼源程序\Debug\hfmTree.txt
?????文件????????189??2018-01-02?23:55??20161380162林俊賢實驗5哈夫曼\20161380162林俊賢哈夫曼源程序\Debug\hfmTreeData.txt
?????文件???12807828??2018-01-02?21:10??20161380162林俊賢實驗5哈夫曼\20161380162林俊賢實驗5哈夫曼.mp4
............此處省略10個文件信息
評論
共有 條評論