数据结构实训的时候写的一些东西,希望对你们有帮助, 如果转载请标明出处

  1. 头文件为
#ifndef TREEHEAD_H_INCLUDED
#define TREEHEAD_H_INCLUDED #include <stdio.h>
#include <stdlib.h>
#include <string.h> #define ERROR 0
#define OK 1
#define status int typedef struct HuffmanData
{
char *data;
int data2;
int weight;
struct HuffmanData* next;
}HFMData, *LHFMData; // Huffman辅助数组
typedef struct
{
char data[4];
int data2;
int weight;
int parent,lchild, rchild;
char *Code;
}HTNode, *HuffmanTree; void Choose();
// 输出*函数, n表示输出的个数,flag表示是否输入完之后回车0NO 1 YES
void print(int n, int flag);
status CreateHuffman();
void InitHfm(LHFMData d, int n); // 寻找中文
status SearchData(LHFMData d, char *data); // 进行huffman编码
status HfmEncoding(HuffmanTree &ht, int &flag);
// 寻找父节点为0 并且weight最小的两个元素;
status MinSearchData(HuffmanTree ht, int n, int &p1, int &p2); // 在进行编译的时候寻找相同的元素
int SearchEncoding(HuffmanTree hc, char *ch, int n); // 进行Huffman翻译
status Decoding(HuffmanTree ht, int flag); // 打印编码
status printCode(int flag); // 打印Huffman树
status TreePrint(HuffmanTree ht, int flag); #endif // TREEHEAD_H_INCLUDED

  2. main文件

  

#include "TreeHead.h"

int main()
{
Choose();
}

  3.解释文件

#include "TreeHead.h"

void Choose()
{
HuffmanTree ht;
int n, flag = 0;
do
{
printf("\t\t\t");print(32,1);
printf("\t\t\t*");
printf(" Huffman 缂?璇戠爜鍣?鏀寔涓枃) *\n");
printf("\t\t\t* 浣跨敤鍓嶈鍏堝皢闇€瑕佺紪鐮佺殑鏂囦欢 *\n\t\t\t* 鍐欏叆鍒癉atafile.txt鏂囦欢涓? *\n");
printf("\t\t\t");print(32,1);
printf("\t\t\t*\t ");
printf("1.寤虹珛HuffmanTree *\n");
printf("\t\t\t*\t ");
printf("2.杩涜Huffman缂栫爜 *\n");
printf("\t\t\t*\t ");
printf("3.杩涜Huffman璇戠爜 *\n");
printf("\t\t\t*\t ");
printf("4.鎵撳嵃浠g爜鏂囦欢 *\n");
printf("\t\t\t*\t ");
printf("5.鎵撳嵃HuffmanTree *\n");
printf("\t\t\t*\t ");
printf("6.閫€鍑? *\n");
printf("\t\t\t");print(32,1);
printf("\t\t\t\t璇疯緭鍏ヤ綘鐨勯€夋嫨锛?);
scanf("%d",&n);
switch(n)
{
case 1:CreateHuffman();break;
case 2:
{
HfmEncoding(ht, flag);
break;
}
case 3:Decoding(ht, flag);break;
case 4:printCode(flag);break;
case 5:TreePrint(ht, flag);break;
case 6: break;
default:
{
printf("\t\t\t");print(30,1);
printf("\t\t\t*\t");
printf(" 杈撳叆鏈夎\t *\n");
printf("\t\t\t");print(30,1);
}
}
system("pause");
system("cls"); }while(n!=6);
}
status CreateHuffman()
{
char c, data[4];
int i, flag = 0;
LHFMData d1, d2, d3;
d1 = (LHFMData)malloc(sizeof(HFMData)*128);
InitHfm(d1, 128);
d2 = (LHFMData)malloc(sizeof(HFMData));
d2->next = NULL;
d2->weight = 0;
if(!d1 || !d2)
{
return ERROR;
}
FILE *fp;
if( (fp=fopen("Datafile.txt","r")) == NULL )
{
system("pause");
return ERROR;
}
while((c = fgetc(fp))!= EOF)
{
i = c;
if(i>0)
{
d1[i].data = (char*)malloc(sizeof(char));
*d1[i].data = c;
d1[i].data2 = i;
d1[i].weight++;
}
else if(i < 0)
{ data[0] = c;
c = fgetc(fp);
data[1] = c;
c = fgetc(fp);
data[2] = c;
data[3] = '\0';
if(!SearchData(d2, data))
{
d2->weight++;
d3 = (LHFMData)malloc(sizeof(HFMData));
d3->data = (char*)malloc(sizeof(char)*3);
strcpy(d3->data, data);
d3->weight = 1;
d3->next = d2->next;
d2->next = d3;
}
}
else if(c == '\n')
{
d1[95].data = (char*)malloc(sizeof(char));
*d1[95].data = c;
d1[95].weight++;
}
}
fclose(fp);
fp = fopen("hfmTree.txt", "w");
for(int i = 0; i < 128; i++)
{
if(d1[i].weight != 0)
{
fprintf(fp,"%d\t%d\n", d1[i].data2, d1[i].weight);
}
}
d3 = d2->next;
while(d3)
{
fprintf(fp, "%s\t%d\n", d3->data, d3->weight);
d3 = d3->next;
}
fclose(fp);
printf("\t\t\t");print(31,1);
printf("\t\t\t*\t");
printf(" 鍒涘缓瀹屾瘯\t *\n");
printf("\t\t\t*");
printf("缁撴灉宸茬粡瀛樺湪hfmTree.txt鏂囦欢涓?\n");
printf("\t\t\t");print(31,1); }
void InitHfm(LHFMData d, int n)
{
for(int i = 0; i < n; i++)
{
d[i].weight = 0;
d[i].data2 = -1;
d[i].next = NULL;
}
} // 杩涜huffman缂栫爜
status HfmEncoding(HuffmanTree &ht, int &flag)
{
int p1, p2;
char ch, data[4];
int n = 0, i = 0, m;
FILE *fp;
if((fp = fopen("hfmTree.txt", "r")) == NULL)
{
return ERROR;
}
while((ch = fgetc(fp))!= EOF)
{
if(ch == '\n')
{
n++;
}
}
//HTNode ht[2*n];
ht = (HuffmanTree)malloc(sizeof(HTNode)*(2*n));
m = n;
flag = m;
// 鍒濆鍖栦竴涓媓t
for(int i = 1; i < 2*n; i++)
{
ht[i].lchild = 0;
ht[i].parent = 0;
ht[i].rchild = 0;
ht[i].weight = 0;
} fseek(fp, 0, SEEK_SET);
i = 1;
while((ch = fgetc(fp))!= EOF)
{
if(ch < 0)
{
data[0] = ch;
ch = fgetc(fp);
data[1] = ch;
ch = fgetc(fp);
data[2] = ch;
data[3] = '\0';
strcpy(ht[i].data, data);
fscanf(fp, "%d", &n);
ht[i].weight = n;
fgetc(fp);
i++;
}
else
{
fseek(fp, -1, SEEK_CUR);
fscanf(fp, "%d %d", &ht[i].data2,&n);
ht[i].weight = n;
fgetc(fp);
i++;
}
}
fclose(fp); for(n = m+1; n < 2*m; n++)
{
MinSearchData(ht, m, p1, p2);
ht[n].lchild = p1;
ht[n].rchild = p2;
ht[n].weight = ht[p1].weight+ht[p2].weight;
ht[p1].parent = n;
ht[p2].parent = n;
}
char encode[m+1];
int p, k;
for(i = 1; i <= m; i++)
{
n = m;
k = i;
encode[n--] = '\0';
while(ht[k].parent != 0)
{
p = ht[k].parent;
if(ht[p].lchild == k)
{
encode[n--] = '1';
}
else
{
encode[n--] = '0';
}
k = p;
}
ht[i].Code = (char*)malloc(sizeof(char)*(m-n));
strcpy(ht[i].Code, &encode[n+1]);
}
fp = fopen("file.txt", "w");
fprintf(fp, "Data\tWeight\tParent\tLchild\tRchild\tHuffmanCode\n"); for(i = 1; i <= m; i++)
{
if(ht[i].data[0] >= 32)
{
fprintf(fp,"%c\t%d\t%d\t%d\t%d\t%s\n",ht[i].data2,ht[i].weight, ht[i].parent, ht[i].lchild,ht[i].rchild, ht[i].Code);
}
else if(ht[i].data[0] < 0)
{
fprintf(fp,"%s\t%d\t%d\t%d\t%d\t%s\n",ht[i].data,ht[i].weight, ht[i].parent, ht[i].lchild,ht[i].rchild, ht[i].Code);
}
else
{
fprintf(fp,"(绌哄瓧绗?\t%d\t%d\t%d\t%d\t%s\n",ht[i].weight, ht[i].parent, ht[i].lchild,ht[i].rchild, ht[i].Code); } }
fclose(fp); FILE *fpr;
if((fp = fopen("Datafile.txt", "r")) == NULL)
{
return ERROR;
}
fpr = fopen("CodeFile.txt", "w"); while((ch = fgetc(fp))!= EOF)
{
i = ch;
if(i >= 0)
{
data[0] = ch;
data[1] = '\0';
k = SearchEncoding(ht, data, m);
fprintf(fpr, "%s", ht[k].Code);
}
else if(i < 0)
{ data[0] = ch;
ch = fgetc(fp);
data[1] = ch;
ch = fgetc(fp);
data[2] = ch;
data[3] = '\0';
k = SearchEncoding(ht, data, m);
fprintf(fpr, "%s", ht[k].Code);
}
}
fclose(fp);
fclose(fpr);
printf("\t\t\t");print(32,1);
printf("\t\t\t*\t");
printf(" 缂栫爜瀹屾瘯\t *\n");
printf("\t\t\t*");
printf("缁撴灉宸茬粡瀛樺湪CodeFile.txt鏂囦欢涓?\n");
printf("\t\t\t");print(32,1); } // 鍦ㄨ繘琛岀紪璇戠殑鏃跺€欏鎵剧浉鍚岀殑鍏冪礌
int SearchEncoding(HuffmanTree hc, char *ch, int n)
{
int i;
for(i = 1; i <= n; i++)
{
if(ch[0] < 0)
{
if(!strcmp(hc[i].data, ch))
{
return i;
}
}
else
{
if(hc[i].data2 == ch[0])
{
return i;
}
} }
return 0;
} // 瀵绘壘鐖惰妭鐐逛负0 骞朵笖weight鏈€灏忕殑涓や釜鍏冪礌锛?
status MinSearchData(HuffmanTree ht, int n, int &p1, int &p2)
{
int i = 0;
p1 = 0, p2 = 0;
for( i = 0; i < 2*n; i++)
{
if(ht[i].parent == 0)
{
if(p1 == 0)
{
p1 = i;
}
else
{
p2 = i;
break;
} }
}
if(p2 == 0)
{
return OK;
}
if(ht[p1].weight > ht[p2].weight)
{
p2 = p1;
p1 = i;
}
for(i++;i < 2*n; i++)
{
if(ht[i].parent == 0)
{
if(ht[i].weight!=0)
{
if(ht[i].weight > ht[p1].weight)
{
if(ht[i].weight < ht[p2].weight)
{
p2 = i;
}
}
else
{
p2 = p1;
p1 = i;
}
}
}
}
} status SearchData(LHFMData d, char *data)
{
LHFMData p;
p = d->next;
while(p)
{
if(!strcmp(p->data, data))
{
p->weight++;
d->weight++;
return OK;
}
p = p->next;
}
return ERROR;
} // 鎵撳嵃缂栫爜
status printCode(int flag)
{
int n = 0;
char ch;
if(flag == 0)
{
printf("\t\t\t");print(30,1);
printf("\t\t\t*\t");
printf("璇峰厛缂栬瘧锛?锛? *\n");
printf("\t\t\t");print(30,1);
return ERROR;
}
FILE *fp;
if((fp = fopen("CodeFile.txt", "r")) == NULL)
{
return ERROR;
}
printf("Huffman Code : ");
while((ch = fgetc(fp))!= EOF)
{
printf("%c", ch);
n++;
if(n == 50)
{
printf("\n\t\t");
n = 0;
}
}
printf("\n"); } // 杩涜Huffman缈昏瘧
status Decoding(HuffmanTree ht, int flag)
{
char ch;
int n ;
if(flag == 0)
{
printf("\t\t\t");print(30,1);
printf("\t\t\t*\t");
printf("璇峰厛缂栬瘧锛?锛? *\n");
printf("\t\t\t");print(30,1);
return ERROR;
}
/*for(int n = 1; n < 2*flag; n++)
{
printf("%d %d %d\n", ht[n].parent, ht[n].lchild, ht[n].rchild);
}*/
FILE *fp,*fpr;
if((fp = fopen("CodeFile.txt", "r")) == NULL)
{
return ERROR;
}
fpr = fopen("TextFile.txt", "w");
while((ch = fgetc(fp))!= EOF)
{
n = flag*2-1;
do
{
if(ch == '1')
{
n = ht[n].lchild;
}
else
{
n = ht[n].rchild;
}
if(ht[n].lchild == 0 && ht[n].rchild == 0)
{
if(ht[n].data[0] < 0)
{
fprintf(fpr, "%s", ht[n].data);
}
else
{
fprintf(fpr, "%c", ht[n].data2);
}
break;
}
}while((ch = fgetc(fp))!= EOF);
}
fclose(fpr);
printf("\t\t\t");print(32,1);
printf("\t\t\t*\t");
printf(" 瑙g爜瀹屾瘯\t *\n");
printf("\t\t\t*");
printf("缁撴灉宸茬粡瀛樺湪TextFile.txt鏂囦欢涓?\n");
printf("\t\t\t");print(32,1);
} // 杈撳嚭*鍑芥暟锛?n琛ㄧず杈撳嚭鐨勪釜鏁帮紝flag琛ㄧず鏄惁杈撳叆瀹屼箣鍚庡洖杞?NO 1 YES
void print(int n, int flag)
{
for(int m = 1; m <= n; m++)
{
printf("*");
}
if(flag == 1)
{
printf("\n");
}
} status TreePrint(HuffmanTree ht, int flag)
{
system("cls");
if(flag == 0)
{
printf("\t\t\t");print(30,1);
printf("\t\t\t*\t");
printf("璇峰厛缂栬瘧锛?锛? *\n");
printf("\t\t\t");print(30,1);
return ERROR;
} printf("\t\t");
print(65, 1);
printf("\t\t* \t\t\t鎵撳嵃HuffmanTree \t\t\t*\n");
printf("\t\t");
print(65, 1);
printf("\t\t* \t\t姹夊瓧鏄剧ず鏈夎锛岀敤锛堟眽瀛楋級鏉ヤ唬鏇挎眽瀛? \t\t*\n");
printf("\t\t");
print(65, 1);
printf("\t\t* 鏁版嵁\t 鏉冮噸\t 宸﹀瀛怽t 鍙冲瀛怽t Huffman Code *\n");
for(int i = 1; i < flag*2; i++)
{
if(i<=flag)
{
if(ht[i].data[0] < 0)
{ printf("\t\t*姹夊瓧\t %d\t %d\t\t %d\t\t ", ht[i].weight,ht[i].lchild,ht[i].rchild);
if(strlen(ht[i].Code) <= 3)
{
printf("%s\t\t*\n", ht[i].Code);
}
else
{
printf("%s\t*\n", ht[i].Code);
}
}
else
{ printf("\t\t* %c\t %d\t %d\t\t %d\t\t ", ht[i].data2, ht[i].weight,ht[i].lchild,ht[i].rchild);
if(strlen(ht[i].Code) <= 3)
{
printf("%s\t\t*\n", ht[i].Code);
}
else
{
printf("%s\t*\n", ht[i].Code);
}
} }
else
{
printf("\t\t*\t %d\t %d\t\t %d\t\t\t\t*\n", ht[i].weight,ht[i].lchild,ht[i].rchild);
} }
printf("\t\t");
print(65, 1);
system("pause");
}

  

Huffman编/译码器(能够实现中英文)的更多相关文章

  1. 数据结构实训——哈夫曼(Huffman)编/译码器

    题目4.哈夫曼(Huffman)编/译码器(限1人完成) [问题描述] 利用哈夫曼编码进行通信可以大大提高信道利用率,缩短信息传输时间,降低传输成本.但是,这要求在发送端通过一个编码系统对待传数据预先 ...

  2. JPEG标准推荐的亮度、色度DC、AC Huffman编码表

    JPEG 标准推荐的亮度.色度DC.AC Huffman 编码表 博主在完成数字图像处理大作业时利用搜索引擎查找了很久完整的四张Huffman 编码表(亮度AC Huffman编码表.亮度DC Huf ...

  3. huffman编解码英文文本[Python]

    对英文文本的字母进行huffman编码,heapq优先队列构建huffman树 python huffman.py source.txt result.txt import sys import he ...

  4. jpeg huffman coding table

    亮度DC系数的取值范围及序号:                                                               序号(size) 取值范围 0 0  1 - ...

  5. python实现Huffman编码

    一.问题 利用二叉树的结构对Huffman树进行编码,实现最短编码 二.解决 # 构建节点类 class TreeNode: def __init__(self, data): "" ...

  6. Huffman编码和解码

    一.Huffman树 定义: 给定n个权值作为n个叶子结点,构造一棵二叉树,若该树的带权路径达到最小,这样的二叉树称为最优二叉树,也称为霍夫曼树(Huffman树). 特点:     Huffman树 ...

  7. TTL和CMOS

    reprint from:http://blog.csdn.net/hemeinvyiqiluoben/article/details/9253249 TTL和COMS电平匹配以及电平转换的方法 一. ...

  8. TTL电平和CMOS电平总结

    TTL电平和CMOS电平总结 1,TTL电平:          输出高电平>2.4V,输出低电平<0.4V.在室温下,一般输出高电平是3.5V,输出低电平是0.2V.最小输入高电平和低电 ...

  9. TTL和COMS电平匹配以及电平转换的方法

    一.TTL TTL集成电路的主要型式为晶体管-晶体管逻辑门(transistor-transistor logic gate),TTL大部分都采用5V电源.1.输出高电平Uoh和输出低电平UolUoh ...

随机推荐

  1. Hive(三)【DDL 数据定义】

    目录 一.DDL数据定义 1.库的DDL 1.1创建数据库 1.2查询数据库 1.3查看数据库详情 1.4切换数据库 1.5修改数据库 1.6删除数据库 2.表的DDL 2.1创建表 2.2管理表(内 ...

  2. 通信方案软件设计(环形动态申请内存,支持USART+IIC+SPI+CAN协议

    1 <STM32进阶之串口环形缓冲区实现>中讲得比较清楚(链接) 2 amobbs中讲的方法有点复杂,以下是链接和参考源码: 通信方案软件设计(环形动态申请内存,支持USART+IIC+S ...

  3. 【分布式】Zookeeper伪集群安装部署

    zookeeper:伪集群安装部署 只有一台linux主机,但却想要模拟搭建一套zookeeper集群的环境.可以使用伪集群模式来搭建.伪集群模式本质上就是在一个linux操作系统里面启动多个zook ...

  4. swift设置导航栏item颜色和状态栏颜色

    //swift设置导航栏item颜色和状态栏颜色 let dict:Dictionary =[NSForegroundColorAttributeName:UIColor.hrgb("333 ...

  5. Mysql不锁表备份之Xtrabackup的备份与恢复

    一.Xtrabackup介绍 MySQL冷备.热备.mysqldump都无法实现对数据库进行增量备份.如果数据量较大我们每天进行完整备份不仅耗时且影响性能.而Percona-Xtrabackup就是为 ...

  6. dbeaver可视化工具-连接clickhouse

    下载地址 https://dbeaver.io/download/ 本次下载的是ZIP包,这种方式方便移动/备份软件 此软件可连接多种数据库,这里是连接clickhouse 新建连接 点击 上面的倒三 ...

  7. 【Services】【Web】【Nginx】静态下载页面的安装与配置

    1. 拓扑 F5有自动探活机制,如果一台机器宕机,请求会转发到另外一台,省去了IPVS漂移的麻烦 F5使用轮询算法,向两台服务器转发请求,实现了负载均衡 2. 版本: 2.1 服务器版本:RHEL7. ...

  8. 用graphviz可视化决策树

    1.安装graphviz. graphviz本身是一个绘图工具软件,下载地址在:http://www.graphviz.org/.如果你是linux,可以用apt-get或者yum的方法安装.如果是w ...

  9. ts配置项

    { "compilerOptions": { /* 基本选项 */ "target": "es5", // 指定 ECMAScript 目标 ...

  10. Android App加固原理与技术历程

    App为什么会被破解入侵 随着黑客技术的普及化平民化,App,这个承载我们移动数字工作和生活的重要工具,不仅是黑客眼中的肥肉,也获得更多网友的关注.百度一下"App破解"就有529 ...