资源简介
哈夫曼编码译码,数据结构课程设计,C++语言
代码片段和文件信息
#include
#include
#include
#include
#include
using namespace std;
#define MAX 100
typedef struct HTNode{
char c;
int weight;
int parentlchildrchild;
}HTNode*HuffmanTree;
typedef char **HuffmanCode;
HuffmanTree HT;
HuffmanCode HC;
void Select(HuffmanTree HTint iint &s1int &s2) //Select sub-function
//choose parent is 0 and weight is smallest node from forest
{ int jk=1; //s1 is the least of HT[].weight
while(HT[k].parent!=0) //s2 is the second least of HT[].weight
k++;
s1=k;
for(j=1;j<=i;++j)
if(HT[j].parent==0&&HT[j].weight s1=j;
k=1;
while((HT[k].parent!=0||k==s1))
k++;
s2=k;
for(j=1;j<=i;++j)
if(HT[j].parent==0&&HT[j].weight s2=j;
} //Select() end
void CreatHuffmanTree(HuffmanTree &HT int n)
//creat a huffmantree which is HT
{
int s1 s2;
if(n <= 1) return ;
int m = 2 * n - 1;
HT = new HTNode[m+1];
for(int i = 1;i <= m;++i) {
HT[i].parent = 0; HT[i].lchild = 0; HT[i].rchild = 0;
}
//read char and weight from a file
/*ifstream infile(“data.txt“ios::in);
if(!infile) {
cerr<<“open error!“< exit(1);
}
for(int i = 1;i <= n;i++){
infile >>HT[i].c>> HT[i].weight;
}
infile.close();*/
cout << “Enter the char and weight“< for(int i = 1;i <= n;i++){
cin >>HT[i].c>> HT[i].weight;
}
for(int i = n+1;i <= m;++i) {
Select(HTi-1s1s2);
HT[s1].parent = i; HT[s2].parent = i;
HT[i].lchild = s1; HT[i].rchild = s2;
HT[i].weight = HT[s1].weight + HT[s2].weight;
}
}
void Savefile(HuffmanTree HTint n)
{
//save the weight into a file
ofstream outfile(“datas.txt“ios::out);
if(!outfile) {
cerr<<“open error!“< exit(1);
}
for(int i = 1;i <= n;i++){
outfile<< HT[i].weight<<“ “;
}
cout <<“the data of weight is already saved in files“< outfile.close();
}
void CreatHuffmanCode(HuffmanTree HT HuffmanCode &HC int n)
// Create the HuffmanCode
{
int startcf;
HC = new char*[n+1];
char *cd = new char[n];
cd[n-1] = ‘\0‘;
cout< for (int i = 1;i <= n;i++){
start = n-1;
c = i; f = HT[i].parent;
while (f != 0){
--start;
if(HT[f].lchild == c) cd[start] = ‘0‘;
else cd[start] = ‘1‘;
c = f; f = HT[f].parent;
}
HC[i] = new char[n - start];
strcpy(HC[i]&cd[start]);
cout << HT[i].c;
cout << “ ‘s Huffman code is: “< }
delete cd;
}
void EnCod
- 上一篇:c++项目详细简单适合初学者
- 下一篇:英飞凌BMS解决方案C语言程序源码及使用说明
相关资源
- GB2312编码和UTF-8互转c语言实现
- C++实现7.4汉明码编码
- 212类型的维特比译码在C语言中的实现
- 汉明码的编码译码的C语言实现
- 游程编码C语言实现
- 信息论实验报告包括求信息量、香农
- 算术编码源程序c++程序
- 算术编码纯C语言实现
- libx264最新稳定版本附WINDOWS下编译步骤
- 应用C/C++语言编写灰度图像霍夫曼编码
- 数据压缩LZW编码c++程序
- 数据压缩 算术编码 c++ 程序
- 文件加密基于赫夫曼编码的简单程序
- 基于huffman编码的文件解压缩程序(
- 哈夫曼树及其编码
- 唯一可译码源代码c语言 含报告
- 赫夫曼编码(C语言编写
- 对26个英文字母进行哈夫曼编码
- 信道编码C++
- lzw编码实现
- BCH(168)编解码,c++实现
- 交叉25码ITF25编码程序代码
- C++实现的JPEG压缩算法
- c/c++采用编码转换表实现gbk与unicode互
- 信息论与编码实验报告含代码 哈弗曼
- 算术编码C语言程序编码解码,非自适
- 霍夫曼编码的C语言实现
- 哈夫曼编码解码的实现及运行截图C语
- c++实现算术编码-数据压缩
- 哈夫曼编码译码器绝对可以运行c语言
评论
共有 条评论